/** * Create service with name * * @param ServiceLocatorInterface $controllers * @param string $name * @param string $requestedName * @return RestController * @throws ServiceNotCreatedException if listener specified is not a ListenerAggregate */ public function createServiceWithName(ServiceLocatorInterface $controllers, $name, $requestedName) { $services = $controllers->getServiceLocator(); $config = $services->get('Config'); $config = $config['zf-rest'][$requestedName]; if ($services->has($config['listener'])) { $listener = $services->get($config['listener']); } else { $listener = new $config['listener'](); } if (!$listener instanceof ListenerAggregateInterface) { throw new ServiceNotCreatedException(sprintf('%s expects that the "listener" reference a service that implements Zend\\EventManager\\ListenerAggregateInterface; received %s', __METHOD__, is_object($listener) ? get_class($listener) : gettype($listener))); } $resourceIdentifiers = array(get_class($listener)); if (isset($config['resource_identifiers'])) { if (!is_array($config['resource_identifiers'])) { $config['resource_identifiers'] = (array) $config['resource_identifiers']; } $resourceIdentifiers = array_merge($resourceIdentifiers, $config['resource_identifiers']); } $events = $services->get('EventManager'); $events->attach($listener); $events->setIdentifiers($resourceIdentifiers); $resource = new Resource(); $resource->setEventManager($events); $identifier = $requestedName; if (isset($config['identifier'])) { $identifier = $config['identifier']; } $controllerClass = isset($config['controller_class']) ? $config['controller_class'] : 'ZF\\Rest\\RestController'; $controller = new $controllerClass($identifier); if (!$controller instanceof RestController) { throw new ServiceNotCreatedException(sprintf('"%s" must be an implementation of ZF\\Rest\\RestController', $controllerClass)); } $controller->setEventManager($events); $controller->setResource($resource); $this->setControllerOptions($config, $controller); if (isset($config['entity_class'])) { $listener->setEntityClass($config['entity_class']); } if (isset($config['collection_class'])) { $listener->setCollectionClass($config['collection_class']); } return $controller; }