{"action":"closed","issue":{"url":"https://api.github.com/repos/olegkrivtsov/using-zend-framework-3-book/issues/10","repository_url":"https://api.github.com/repos/olegkrivtsov/using-zend-framework-3-book","labels_url":"https://api.github.com/repos/olegkrivtsov/using-zend-framework-3-book/issues/10/labels{/name}","comments_url":"https://api.github.com/repos/olegkrivtsov/using-zend-framework-3-book/issues/10/comments","events_url":"https://api.github.com/repos/olegkrivtsov/using-zend-framework-3-book/issues/10/events","html_url":"https://github.com/olegkrivtsov/using-zend-framework-3-book/issues/10","id":192686429,"number":10,"title":"ControllerFactory","user":{"login":"naprstek","id":14281228,"avatar_url":"https://avatars.githubusercontent.com/u/14281228?v=3","gravatar_id":"","url":"https://api.github.com/users/naprstek","html_url":"https://github.com/naprstek","followers_url":"https://api.github.com/users/naprstek/followers","following_url":"https://api.github.com/users/naprstek/following{/other_user}","gists_url":"https://api.github.com/users/naprstek/gists{/gist_id}","starred_url":"https://api.github.com/users/naprstek/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/naprstek/subscriptions","organizations_url":"https://api.github.com/users/naprstek/orgs","repos_url":"https://api.github.com/users/naprstek/repos","events_url":"https://api.github.com/users/naprstek/events{/privacy}","received_events_url":"https://api.github.com/users/naprstek/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2016-11-30T21:36:15Z","updated_at":"2016-12-06T02:51:41Z","closed_at":"2016-12-06T02:51:41Z","body":"I found somewhere abstract controller factory which use reflection to instantiate controllers. It may simplify an application.\r\n\r\nuse Interop\\Container\\ContainerInterface;\r\nuse ReflectionClass;\r\nuse Zend\\ServiceManager\\Factory\\AbstractFactoryInterface;\r\n\r\nclass AbstractControllerFactory implements AbstractFactoryInterface\r\n{\r\n    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)\r\n    {\r\n        // Construct a new ReflectionClass object for the requested action\r\n        $reflection = new ReflectionClass($requestedName);\r\n        // Get the constructor\r\n        $constructor = $reflection->getConstructor();\r\n\r\n        if (is_null($constructor)) {\r\n            // There is no constructor, just return a new class\r\n            return new $requestedName;\r\n        }\r\n\r\n        // Get the parameters\r\n        $parameters = $constructor->getParameters();\r\n        $dependencies = [];\r\n        foreach ($parameters as $parameter) {\r\n            // Get the parameter class\r\n            $class = $parameter->getClass();\r\n            // Get the class from the container\r\n            $dependencies[] = $container->get($class->getName());\r\n        }\r\n\r\n        // Return the requested class and inject its dependencies\r\n        return $reflection->newInstanceArgs($dependencies);\r\n    }\r\n\r\n    public function canCreate(ContainerInterface $container, $requestedName)\r\n    {\r\n        // Only accept Controller classes\r\n        list( $module, ) = explode('\\\\', __NAMESPACE__, 2);\r\n        return strstr($requestedName, $module . '\\Controller') !== false;\r\n    }\r\n}\r\n\r\nand then in module.config.php just list abstract factory:\r\n\r\n    'controllers' => [\r\n        'abstract_factories' => [\r\n            AbstractControllerFactory::class,\r\n        ]\r\n    ],\r\n"}}
{"actor":{"display_login":"olegkrivtsov"}}