Skip to content

Commit

Permalink
Implemented new possibilities for image resources. REMOVE feature.
Browse files Browse the repository at this point in the history
  • Loading branch information
spalax committed Jul 29, 2013
1 parent 86a0679 commit a2e43c4
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 55 deletions.
37 changes: 1 addition & 36 deletions config/module/router.config.php
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'
)
);
47 changes: 47 additions & 0 deletions config/module/router/images.config.php
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'
)
)
)
)
)
)
);
9 changes: 0 additions & 9 deletions src/Zf2FileUploader/Controller/AbstractCreateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@

abstract class AbstractCreateController extends AbstractController
{
const EVENT_INVALID_DATA_PRE_HANDLER = 'uploader.create.invalid.data.preDecorator';
const EVENT_INVALID_DATA_POST_HANDLER = 'uploader.create.invalid.data.postDecorator';

/**
* @var boolean
*/
Expand Down Expand Up @@ -50,10 +47,6 @@ public function dispatch(Request $request, Response $response = null)


if (!$this->getDataResourceCreator()->isValid()) {
$this->getEventManager()->trigger(self::EVENT_INVALID_DATA_PRE_HANDLER,
$this,
array($this->getDataResourceCreator()));

if ($this->disableDefaultErrorHandler) {
$response = $response ?: new HttpResponse();
$this->getEventManager()->clearListeners(MvcEvent::EVENT_DISPATCH);
Expand All @@ -63,8 +56,6 @@ public function dispatch(Request $request, Response $response = null)
$this->getEventManager()->clearListeners(MvcEvent::EVENT_DISPATCH);
$this->getEvent()->setResult(new UploaderModel($this->getDataResourceCreator()));
}

$this->getEventManager()->trigger(self::EVENT_INVALID_DATA_POST_HANDLER, $this);
}

parent::dispatch($request, $response);
Expand Down
56 changes: 56 additions & 0 deletions src/Zf2FileUploader/Controller/AbstractRemoveController.php
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 src/Zf2FileUploader/Controller/Images/RemoveController.php
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function remove(ResourceInterface $resource)
/* @var $file \DirectoryIterator */
foreach ($directory as $file) {
if ($file->isDir()) continue;
if (preg_match('/^'.$resource->getToken().'.+/', $file->getBasename())) {
if (preg_match('/^'.$resource->getToken().'.+\..*?/', $file->getBasename())) {
if (!unlink($file->getRealPath())) {
$result = false;
}
Expand Down
10 changes: 1 addition & 9 deletions src/Zf2FileUploader/Service/Image/SaveService.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ class SaveService implements SaveServiceInterface
*/
protected $persister;

/**
* @var TranslatorInterface
*/
protected $translator;

/**
* @var ImageDecoratorInterface
*/
Expand All @@ -37,16 +32,13 @@ class SaveService implements SaveServiceInterface
* @param ImagePersisterInterface $persister
* @param ImageDecoratorInterface $decorator
* @param CleanerStrategyInterface $cleaner
* @param TranslatorInterface $translator
*/
public function __construct(ImagePersisterInterface $persister,
ImageDecoratorInterface $decorator = null,
CleanerStrategyInterface $cleaner,
TranslatorInterface $translator)
CleanerStrategyInterface $cleaner)
{
$this->persister = $persister;
$this->decorator = $decorator;
$this->translator = $translator;
$this->cleaner = $cleaner;
}

Expand Down
28 changes: 28 additions & 0 deletions src/Zf2FileUploader/View/Model/RemoveModel.php
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;
}
}

0 comments on commit a2e43c4

Please sign in to comment.