public function testCreation() { $moduleManagerMock = $this->getMockBuilder('Zend\\ModuleManager\\ModuleManager')->disableOriginalConstructor()->getMock(); $this->serviceManager->setService('ModuleManager', $moduleManagerMock); $this->assertTrue($this->controllerManager->has('Modules\\Controller\\Console\\List')); $controller = $this->controllerManager->get('Modules\\Controller\\Console\\List'); $this->assertInstanceOf('Modules\\Controller\\Console\\ListController', $controller); $this->assertAttributeInstanceOf('ComposerLockParser\\ComposerInfo', 'composerInfo', $controller); $this->assertAttributeEquals($moduleManagerMock, 'moduleManager', $controller); $this->assertAttributeInstanceOf('Zend\\View\\Renderer\\PhpRenderer', 'renderer', $controller); $this->assertAttributeInstanceOf('Modules\\ViewModel\\Console\\ListViewModel', 'viewModel', $controller); }
/** * Dispatch another controller * * @param string $name Controller name; either a class name or an alias used in the controller manager * @param null|array $params Parameters with which to seed a custom RouteMatch object for the new controller * @return mixed * @throws Exception\DomainException if composed controller does not define InjectApplicationEventInterface * or Locator aware; or if the discovered controller is not dispatchable */ public function dispatch($name, array $params = null) { $event = clone $this->getEvent(); $controller = $this->controllers->get($name); if ($controller instanceof InjectApplicationEventInterface) { $controller->setEvent($event); } // Allow passing parameters to seed the RouteMatch with & copy matched route name if ($params !== null) { $routeMatch = new RouteMatch($params); $routeMatch->setMatchedRouteName($event->getRouteMatch()->getMatchedRouteName()); $event->setRouteMatch($routeMatch); } if ($this->numNestedForwards > $this->maxNestedForwards) { throw new Exception\DomainException("Circular forwarding detected: greater than {$this->maxNestedForwards} nested forwards"); } $this->numNestedForwards++; // Detach listeners that may cause problems during dispatch: $sharedEvents = $event->getApplication()->getEventManager()->getSharedManager(); $listeners = $this->detachProblemListeners($sharedEvents); $return = $controller->dispatch($event->getRequest(), $event->getResponse()); // If we detached any listeners, reattach them now: $this->reattachProblemListeners($sharedEvents, $listeners); $this->numNestedForwards--; return $return; }
/** * Dispatch another controller * * @param string $name Controller name; either a class name or an alias used in the controller manager * @param null|array $params Parameters with which to seed a custom RouteMatch object for the new controller * @return mixed * @throws Exception\DomainException if composed controller does not define InjectApplicationEventInterface * or Locator aware; or if the discovered controller is not dispatchable */ public function dispatch($name, array $params = null) { $event = clone $this->getEvent(); if (is_array($params) && isset($params[ModuleRouteListener::MODULE_NAMESPACE])) { $module = $params[ModuleRouteListener::MODULE_NAMESPACE]; if (strpos($name, $module) !== 0) { if (!isset($params[ModuleRouteListener::ORIGINAL_CONTROLLER])) { $params[ModuleRouteListener::ORIGINAL_CONTROLLER] = $name; } $name = $module . '\\' . str_replace(' ', '', ucwords(str_replace('-', ' ', $name))); } } $controller = $this->controllers->get($name); if ($controller instanceof InjectApplicationEventInterface) { $controller->setEvent($event); } // Allow passing parameters to seed the RouteMatch with & copy matched route name if ($params !== null) { $routeMatch = new RouteMatch($params); $routeMatch->setMatchedRouteName($event->getRouteMatch()->getMatchedRouteName()); $event->setRouteMatch($routeMatch); } if ($this->numNestedForwards > $this->maxNestedForwards) { throw new Exception\DomainException("Circular forwarding detected: greater than {$this->maxNestedForwards} nested forwards"); } $this->numNestedForwards++; // Detach listeners that may cause problems during dispatch: $sharedEvents = $event->getApplication()->getEventManager()->getSharedManager(); $listeners = $this->detachProblemListeners($sharedEvents); $return = $controller->dispatch($event->getRequest(), $event->getResponse()); // If we detached any listeners, reattach them now: $this->reattachProblemListeners($sharedEvents, $listeners); $this->numNestedForwards--; return $return; }
/** * @param \Zend\Mvc\Controller\ControllerManager $controllerManager * @param \Zend\ServiceManager\ServiceLocatorInterface $serviceLocator * @param array $routeParams */ protected function mockConfiguration($controllerManager, $serviceLocator, $routeParams = array('action' => 'list')) { $prophet = new Prophet(); $controllerKey = 'Phpro\\SmartCrud\\Controller\\CrudController'; $serviceKey = 'Phpro\\SmartCrud\\Service\\AbstractSmartService'; $viewBuilderKey = 'Phpro\\SmartCrud\\View\\Model\\ViewModelBuilder'; $controllerManager->getServiceLocator()->willReturn($serviceLocator); // Mock config $serviceLocator->has('Config')->willReturn(true); $serviceLocator->get('Config')->willReturn(array(AbstractCrudControllerFactory::FACTORY_NAMESPACE => array('custom-controller' => array(AbstractCrudControllerFactory::CONFIG_CONTROLLER => $controllerKey, AbstractCrudControllerFactory::CONFIG_IDENTIFIER => 'id', AbstractCrudControllerFactory::CONFIG_SMART_SERVICE => $serviceKey), 'fault-controller' => array(AbstractCrudControllerFactory::CONFIG_CONTROLLER => 'invalid-controller'), 'fault-service' => array(AbstractCrudControllerFactory::CONFIG_SMART_SERVICE => 'invalid-service')))); // Mock controller $controller = $prophet->prophesize('\\Phpro\\SmartCrud\\Controller\\CrudController'); $controllerManager->has($controllerKey)->willReturn(true); $controllerManager->get($controllerKey)->willReturn($controller); $controllerManager->has('invalid-controller')->willReturn(false); // Mock route $this->mockRouteMatch($serviceLocator, $routeParams); // Mock service $serviceKey = $serviceKey . '::' . $routeParams['action']; $service = $prophet->prophesize('\\Phpro\\SmartCrud\\Service\\AbstractSmartService'); $serviceLocator->has($serviceKey)->willReturn(true); $serviceLocator->get($serviceKey)->willReturn($service); $serviceLocator->has('invalid-service::' . $routeParams['action'])->willReturn(false); $viewBuilder = $prophet->prophesize('\\Phpro\\SmartCrud\\View\\Model\\ViewModelBuilder'); $serviceLocator->has($viewBuilderKey)->willReturn(true); $serviceLocator->get($viewBuilderKey)->willReturn($viewBuilder); $this->setServiceLocator($serviceLocator); }
/** * Inject the class name of the controller, if it can be resolved. * * @param RpcServiceEntity $service */ protected function injectControllerClass(RpcServiceEntity $service) { $controllerServiceName = $service->controllerServiceName; if (!$this->controllerManager->has($controllerServiceName)) { return; } $controller = $this->controllerManager->get($controllerServiceName); $service->exchangeArray(['controller_class' => get_class($controller)]); }
/** * Inject the class name of the controller, if it can be resolved. * * @param DoctrineRpcServiceEntity $service */ protected function injectControllerClass(DoctrineRpcServiceEntity $service) { $controllerServiceName = $service->controllerServiceName; if (!$this->controllerManager->has($controllerServiceName)) { // @codeCoverageIgnoreStart return; // @codeCoverageIgnoreEnd } $controller = $this->controllerManager->get($controllerServiceName); $service->exchangeArray(array('controller_class' => get_class($controller))); }
public function testWillFetchDiDependenciesFromControllerLoaderServiceManager() { $controllerName = __NAMESPACE__ . '\\TestAsset\\ControllerWithDependencies'; // rewriting since controller loader does not have the correct config, but is already fetched $config = new ArrayObject(array('di' => array('instance' => array($controllerName => array('parameters' => array('injected' => 'stdClass'))), 'allowed_controllers' => array($controllerName)))); $this->services->setAllowOverride(true); $this->services->setService('Config', $config); $this->loader = $this->services->get('ControllerLoader'); $testService = new \stdClass(); $this->services->setService('stdClass', $testService); // invalid controller exception (because we're not getting a \Zend\Stdlib\DispatchableInterface after all) $controller = $this->loader->get($controllerName); $this->assertSame($testService, $controller->injectedValue); }
/** * @param $controllerName * @param array $controller */ private function createPath($controllerName, array $controller) { foreach ($controller as $action => $template) { try { /** @var AbstractActionController $controllerClass */ $controllerClass = $this->controllerManager->get($controllerName); $controllerClass->setEvent($this->application->getMvcEvent()); $viewModel = $controllerClass->{$action}(); } catch (\Exception $e) { $viewModel = null; } $this->createPathFromViewModel($viewModel, $controllerName, $action); } }
/** * Retrieve a service from the manager by name * * Allows passing an array of options to use when creating the instance. * createFromInvokable() will use these and pass them to the instance * constructor if not null and a non-empty array. * * @param string $name * @param array $options * @param bool $usePeeringServiceManagers * * @return object * * @throws Exception\ServiceNotFoundException * @throws Exception\ServiceNotCreatedException * @throws Exception\RuntimeException * @SuppressWarnings(PHPMD.LongVariable) */ public function get($name, $options = array(), $usePeeringServiceManagers = true) { $controller = null; if ($this->container->has($name)) { $controller = $this->container->get($name); $this->initialize($controller); } elseif (parent::has($name, true, $usePeeringServiceManagers)) { $controller = parent::get($name, $options, $usePeeringServiceManagers); } if (!$controller) { throw new Exception\ServiceNotFoundException("Unable to locate service '{$name}'"); } elseif (!$controller instanceof DispatchableInterface) { throw new Exception\RuntimeException("Service '{$name}' is not a Controller."); } return $controller; }
/** * * @param ControllerManager $sl * @param Contentinum\Options\PageOptions $pageOptions * @param ContentinumComponents\Mapper\Worker $worker */ protected function formFactory($sl, $pageOptions, $worker) { if (false !== ($formName = $pageOptions->getApp('form'))) { if (false !== ($targetEntities = $pageOptions->getApp('targetentities'))) { if (is_array($targetEntities) && !empty($targetEntities)) { foreach ($targetEntities as $key => $tEntity) { $worker->addTargetEntities($key, $tEntity); } } } $formFactory = new $formName($worker); $decorators = $sl->get($pageOptions->getApp('formdecorators')); $decorators = $decorators->default->toArray(); if (false != ($formAttribs = $pageOptions->getApp('formattributes'))) { $decorators['deco-form']['form-attributtes'] = array_merge($decorators['deco-form']['form-attributtes'], $formAttribs); } $formFactory->setDecorators($decorators); $formFactory->setServiceLocator($sl); return $formFactory; } else { return false; } }