-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented new possibilities for image resources. REMOVE feature.
- Loading branch information
Showing
8 changed files
with
196 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,6 @@ | ||
<?php | ||
return array( | ||
'routes' => array( | ||
'uploader' => array( | ||
'type' => 'literal', | ||
'options' => array( | ||
'route' => '/uploader' | ||
), | ||
'may_terminate' => false, | ||
'child_routes' => array ( | ||
'list' => array( | ||
'type' => 'method', | ||
'options' => array( | ||
'verb' => 'get', | ||
'defaults' => array( | ||
'controller' => 'Zf2FileUploader\Controller\Images\ListController' | ||
) | ||
) | ||
), | ||
'create' => array( | ||
'type' => 'method', | ||
'options' => array( | ||
'verb' => 'post', | ||
'defaults' => array( | ||
'controller' => 'Zf2FileUploader\Controller\Images\CreateController' | ||
) | ||
) | ||
), | ||
'remove' => array( | ||
'type' => 'method', | ||
'options' => array( | ||
'verb' => 'delete', | ||
'defaults' => array( | ||
'controller' => 'Zf2FileUploader\Controller\Images\RemoveController' | ||
) | ||
) | ||
) | ||
) | ||
) | ||
'images' => include __DIR__.'/router/images.config.php' | ||
) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
return array( | ||
'type' => 'literal', | ||
'options' => array( | ||
'route' => '/images' | ||
), | ||
'may_terminate' => false, | ||
'child_routes' => array ( | ||
'list' => array( | ||
'type' => 'method', | ||
'options' => array( | ||
'verb' => 'get', | ||
'defaults' => array( | ||
'controller' => 'Zf2FileUploader\Controller\Images\ListController' | ||
) | ||
) | ||
), | ||
'create' => array( | ||
'type' => 'method', | ||
'options' => array( | ||
'verb' => 'post', | ||
'defaults' => array( | ||
'controller' => 'Zf2FileUploader\Controller\Images\CreateController' | ||
) | ||
) | ||
), | ||
'image' => array( | ||
'type' => 'segment', | ||
'options' => array( | ||
'route' => '/:token', | ||
'constraints' => array( 'token' => '[a-zA-Z0-9]{45}' ) | ||
), | ||
'may_terminate' => false, | ||
'child_routes' => array( | ||
'remove' => array( | ||
'type' => 'method', | ||
'options' => array( | ||
'verb' => 'delete', | ||
'defaults' => array( | ||
'controller' => 'Zf2FileUploader\Controller\Images\RemoveController' | ||
) | ||
) | ||
) | ||
) | ||
) | ||
) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/Zf2FileUploader/Controller/AbstractRemoveController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
namespace Zf2FileUploader\Controller; | ||
|
||
use Doctrine\ORM\EntityRepository; | ||
use Zend\Mvc\Controller\AbstractController; | ||
use Zend\Mvc\MvcEvent; | ||
use Zend\Stdlib\RequestInterface as Request; | ||
use Zend\Stdlib\ResponseInterface as Response; | ||
use Zend\Http\PhpEnvironment\Response as HttpResponse; | ||
use Zf2FileUploader\Entity\ResourceInterface as EntityResourceInterface; | ||
use Zf2FileUploader\View\Model\UploaderModel; | ||
|
||
abstract class AbstractRemoveController extends AbstractController | ||
{ | ||
const REMOVE_ROUTE_PARAM = 'token'; | ||
|
||
/** | ||
* @var EntityResourceInterface | ||
*/ | ||
protected $resourceEntity = null; | ||
|
||
/** | ||
* @return EntityResourceInterface | ||
*/ | ||
protected function getResource() | ||
{ | ||
return $this->resourceEntity; | ||
} | ||
|
||
/** | ||
* @return EntityRepository | ||
*/ | ||
abstract protected function getRepository(); | ||
|
||
/** | ||
* Dispatch a request | ||
* | ||
* @events dispatch.pre, dispatch.post | ||
* @param Request $request | ||
* @param null|Response $response | ||
* @return Response|mixed | ||
*/ | ||
public function dispatch(Request $request, Response $response = null) | ||
{ | ||
$this->resourceEntity = $this->getRepository()->findOneByToken($this->params() | ||
->fromRoute(static::REMOVE_ROUTE_PARAM)); | ||
|
||
if (is_null($this->resourceEntity)) { | ||
$response = $response ?: new HttpResponse(); | ||
$this->getEventManager()->clearListeners(MvcEvent::EVENT_DISPATCH); | ||
$response->setStatusCode(404); | ||
} | ||
|
||
parent::dispatch($request, $response); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
src/Zf2FileUploader/Controller/Images/RemoveController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
namespace Zf2FileUploader\Controller\Images; | ||
|
||
use Doctrine\ORM\EntityManager; | ||
use Doctrine\ORM\EntityRepository; | ||
use Zend\Mvc\MvcEvent; | ||
use Zf2FileUploader\Controller\AbstractRemoveController; | ||
use Zf2FileUploader\Options\ImageResourceOptionsInterface; | ||
use Zf2FileUploader\Resource\Handler\Remover\RemoverInterface; | ||
use Zf2FileUploader\View\Model\RemoveModel; | ||
use Zf2FileUploader\View\Model\UploaderModel; | ||
|
||
class RemoveController extends AbstractRemoveController | ||
{ | ||
/** | ||
* @var RemoverInterface | ||
*/ | ||
protected $remover; | ||
|
||
/** | ||
* @var EntityRepository | ||
*/ | ||
protected $repository; | ||
|
||
/** | ||
* @param EntityManager $entityManager | ||
* @param ImageResourceOptionsInterface $options | ||
* @param RemoverInterface $remover | ||
*/ | ||
public function __construct(EntityManager $entityManager, | ||
ImageResourceOptionsInterface $options, | ||
RemoverInterface $remover) | ||
{ | ||
$this->remover = $remover; | ||
$this->repository = $entityManager->getRepository($options->getImageEntityClass()); | ||
} | ||
|
||
/** | ||
* @return EntityRepository | ||
*/ | ||
protected function getRepository() | ||
{ | ||
return $this->repository; | ||
} | ||
|
||
/** | ||
* @param MvcEvent $e | ||
* @return mixed | void | ||
*/ | ||
public function onDispatch(MvcEvent $e) | ||
{ | ||
$removeModel = new RemoveModel(); | ||
|
||
if (!$this->remover->remove($this->getResource())) { | ||
$removeModel->fail(); | ||
} else { | ||
$removeModel->success(); | ||
} | ||
|
||
$e->setResult($removeModel); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
namespace Zf2FileUploader\View\Model; | ||
|
||
use Zend\View\Model\JsonModel; | ||
|
||
class RemoveModel extends JsonModel | ||
{ | ||
const STATUS_SUCCESS = 1; | ||
const STATUS_FAILED = 0; | ||
|
||
/** | ||
* @return UploaderModel | ||
*/ | ||
public function success() | ||
{ | ||
$this->setVariable('status', self::STATUS_SUCCESS); | ||
return $this; | ||
} | ||
|
||
/** | ||
* @return UploaderModel | ||
*/ | ||
public function fail() | ||
{ | ||
$this->setVariable('status', self::STATUS_FAILED); | ||
return $this; | ||
} | ||
} |