Skip to content

Commit

Permalink
Use service trait
Browse files Browse the repository at this point in the history
  • Loading branch information
mikemix committed Jul 17, 2015
1 parent a24ba51 commit efba921
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
5 changes: 4 additions & 1 deletion src/Controller/CacheClearController.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
<?php
namespace mxdiModule\Controller;

use mxdiModule\Traits\ServiceTrait;
use Zend\Cache\Storage\FlushableInterface;
use Zend\Cache\Storage\StorageInterface;
use Zend\Mvc\Controller\AbstractConsoleController;

class CacheClearController extends AbstractConsoleController
{
use ServiceTrait;

/** @var StorageInterface */
protected $storage;

Expand Down Expand Up @@ -38,7 +41,7 @@ public function indexAction()
*/
private function flushOne($fqcn)
{
$this->storage->removeItem(md5($fqcn));
$this->storage->removeItem($this->getHash($fqcn));
return 0;
}

Expand Down
4 changes: 3 additions & 1 deletion src/Service/DiFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@

use mxdiModule\ServiceManager\DiAbstractFactory;
use mxdiModule\Service\Exception\CannotCreateService;
use mxdiModule\Traits\ServiceTrait;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorAwareTrait;

final class DiFactory implements ServiceLocatorAwareInterface
{
use ServiceLocatorAwareTrait;
use ServiceTrait;

/** @var DiAbstractFactory */
private $diAbstractFactory;
Expand Down Expand Up @@ -39,7 +41,7 @@ public function __invoke($fqcn)
*/
public function get($fqcn)
{
$serviceName = strtolower(preg_replace('/[^a-zA-Z0-9]/', '', $fqcn));
$serviceName = $this->getCanonicalName($fqcn);

if ($this->diAbstractFactory->canCreateServiceWithName($this->serviceLocator, $serviceName, $fqcn)) {
return $this->diAbstractFactory->createServiceWithName($this->serviceLocator, $serviceName, $fqcn);
Expand Down
9 changes: 6 additions & 3 deletions src/ServiceManager/DiAbstractFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace mxdiModule\ServiceManager;

use mxdiModule\Service\ExtractorInterface;
use mxdiModule\Traits\ServiceTrait;
use Zend\Cache\Storage\Adapter\AbstractAdapter;
use Zend\Cache\Storage\StorageInterface;
use Zend\ServiceManager\AbstractFactoryInterface;
Expand All @@ -11,6 +12,8 @@

class DiAbstractFactory implements AbstractFactoryInterface
{
use ServiceTrait;

/** @var array */
protected $config;

Expand Down Expand Up @@ -46,7 +49,7 @@ public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator
}

$this->initializeCache($serviceLocator);
$this->setChangeSet($this->cache->getItem(md5($name)));
$this->setChangeSet($this->cache->getItem($this->getHash($name)));

if ($this->getChangeSet() instanceof ChangeSet) {
// Positive result available via cache
Expand All @@ -61,13 +64,13 @@ public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator

if ($this->getChangeSet()->isAnnotated()) {
// Service is annotated to cache results
$this->cache->setItem(md5($name), $this->getChangeSet());
$this->cache->setItem($this->getHash($name), $this->getChangeSet());
return true;
}

// Service is not annotated
// Cache false for it
$this->cache->setItem(md5($name), false);
$this->cache->setItem($this->getHash($name), false);
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion test/Controller/CacheClearControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function testFlushOne()

$storage->expects($this->once())
->method('removeItem')
->with($this->equalTo(md5($fqcn)));
->with($this->equalTo(md5('appservicetestservice')));

$this->plugin->expects($this->atLeastOnce())
->method('__invoke')
Expand Down

0 comments on commit efba921

Please sign in to comment.