/** * @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); }
/** * Redirect with Handling against url. * * @param string $url * * @return Response */ public function toUrl($url) { $allow_not_routed_url = isset($this->config['allow_not_routed_url']) ? $this->config['allow_not_routed_url'] : false; $exclude_urls = isset($this->config['options']['exclude_urls']) ? $this->config['options']['exclude_urls'] : []; $exclude_hosts = isset($this->config['options']['exclude_hosts']) ? $this->config['options']['exclude_hosts'] : []; $uriTargetHost = (new Uri($url))->getHost(); if (true === $allow_not_routed_url || in_array($url, $exclude_urls) || in_array($uriTargetHost, $exclude_hosts)) { return parent::toUrl($url); } $controller = $this->getController(); $request = $controller->getRequest(); $current_uri = $request->getRequestUri(); $request->setUri($url); $uriTarget = (new Uri($url))->__toString(); if ($current_uri === $uriTarget) { $this->getEventManager()->trigger('redirect-same-url'); return; } $mvcEvent = $this->getEvent(); $routeMatch = $mvcEvent->getRouteMatch(); $currentRouteMatchName = $routeMatch->getMatchedRouteName(); $router = $mvcEvent->getRouter(); $uriCurrentHost = (new Uri($router->getRequestUri()))->getHost(); if (($routeToBeMatched = $router->match($request)) && ($uriTargetHost === null || $uriCurrentHost === $uriTargetHost) && (($routeToBeMatchedRouteName = $routeToBeMatched->getMatchedRouteName()) !== $currentRouteMatchName && ($this->manager->has($routeToBeMatched->getParam('controller')) || $routeToBeMatched->getParam('middleware') !== false) || $routeToBeMatched->getParam('action') != $routeMatch->getParam('action') || $routeToBeMatchedRouteName === $currentRouteMatchName)) { return parent::toUrl($url); } $default_url = isset($this->config['default_url']) ? $this->config['default_url'] : '/'; return parent::toUrl($default_url); }
public function testWillInstantiateControllersFromDiAbstractFactoryWhenWhitelisted() { $config = new ArrayObject(array('di' => array('instance' => array('alias' => array('my-controller' => 'stdClass')), 'allowed_controllers' => array('my-controller')))); $this->services->setAllowOverride(true); $this->services->setService('Config', $config); $this->loader = $this->services->get('ControllerLoader'); $this->assertTrue($this->loader->has('my-controller')); // invalid controller exception (because we're getting an \stdClass after all) $this->setExpectedException('Zend\\Mvc\\Exception\\InvalidControllerException'); $this->loader->get('my-controller'); }
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); }
/** * 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)]); }
/** * Override: do not use peering service managers * * @param string|array $name * @param bool $checkAbstractFactories * @param bool $usePeeringServiceManagers * @return bool * @SuppressWarnings(PHPMD.LongVariable) */ public function has($name, $checkAbstractFactories = true, $usePeeringServiceManagers = false) { if (is_string($name) && $this->container->has($name)) { return true; } elseif (parent::has($name, $checkAbstractFactories, $usePeeringServiceManagers)) { return true; } return false; }
/** * 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))); }