Created
July 17, 2013 10:38
-
-
Save GeeH/6019494 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
... | |
public function getServiceConfig() | |
{ | |
return array( | |
'abstract_factories' => array( | |
'Application\\Factory\\ServiceFactory', | |
), | |
); | |
} | |
... |
This file contains hidden or 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
<?php | |
namespace Application\Factory; | |
use Application\Service\AbstractService; | |
use Zend\ServiceManager\AbstractFactoryInterface; | |
use Zend\ServiceManager\ServiceLocatorInterface; | |
class ServiceFactory implements AbstractFactoryInterface | |
{ | |
/** | |
* @param ServiceLocatorInterface $serviceLocator | |
* @param $name | |
* @param $requestedName | |
* @return bool | |
*/ | |
public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) | |
{ | |
if(strpos($requestedName, 'Application\\Service') === 0) { | |
if(class_exists($requestedName)) { | |
return true; | |
} | |
} | |
return false; | |
} | |
/** | |
* @param ServiceLocatorInterface $serviceLocator | |
* @param $name | |
* @param $requestedName | |
* @return AbstractService | |
*/ | |
public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) | |
{ | |
/** @var AbstractService $service */ | |
$service = new $requestedName($serviceLocator->get('Doctrine\ORM\EntityManager')); | |
$service->setEntityName(str_replace('Service', '', basename(str_replace('\\', '/', $requestedName)))); | |
return $service; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment