Skip to content

Instantly share code, notes, and snippets.

@GeeH
Created July 17, 2013 10:38
Show Gist options
  • Save GeeH/6019494 to your computer and use it in GitHub Desktop.
Save GeeH/6019494 to your computer and use it in GitHub Desktop.
...
public function getServiceConfig()
{
return array(
'abstract_factories' => array(
'Application\\Factory\\ServiceFactory',
),
);
}
...
<?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