1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* YAWIK |
4
|
|
|
* |
5
|
|
|
* @filesource |
6
|
|
|
* @license MIT |
7
|
|
|
* @copyright 2013 - 2016 Cross Solution <http://cross-solution.de> |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Core\Factory\Controller; |
11
|
|
|
|
12
|
|
|
use Core\EventManager\EventManager; |
13
|
|
|
use Core\Repository\RepositoryService; |
14
|
|
|
use Interop\Container\ContainerInterface; |
15
|
|
|
use Zend\Form\FormElementManager\FormElementManagerV3Polyfill as FormElementManager; |
16
|
|
|
use Zend\ModuleManager\ModuleManagerInterface; |
17
|
|
|
use Zend\Mvc\I18n\Translator; |
18
|
|
|
use Zend\ServiceManager\Exception\ServiceNotCreatedException; |
19
|
|
|
use Zend\ServiceManager\Factory\AbstractFactoryInterface; |
20
|
|
|
use Zend\Validator\ValidatorPluginManager; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Install module main controller. |
24
|
|
|
* |
25
|
|
|
* @author Anthonius Munthi <[email protected]> |
26
|
|
|
* @since 0.29 |
27
|
|
|
*/ |
28
|
|
|
class LazyControllerFactory implements AbstractFactoryInterface |
29
|
|
|
{ |
30
|
|
|
protected $aliases = [ |
31
|
|
|
FormElementManager::class => 'FormElementManager', |
32
|
|
|
ValidatorPluginManager::class => 'ValidatorManager', |
33
|
|
|
Translator::class => 'translator', |
34
|
|
|
ModuleManagerInterface::class => 'ModuleManager', |
35
|
|
|
EventManager::class => 'Core/EventManager', |
36
|
|
|
RepositoryService::class => 'repositories', |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
public function canCreate( ContainerInterface $container, $requestedName ) |
40
|
|
|
{ |
41
|
|
|
return strstr( $requestedName,'\Controller') !== false; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function __invoke( ContainerInterface $container, $requestedName, array $options = null ) |
45
|
|
|
{ |
46
|
|
|
$className = $this->getClassName($requestedName); |
47
|
|
|
$class = new \ReflectionClass($className); |
48
|
|
|
|
49
|
|
|
if( $constructor = $class->getConstructor() ) |
50
|
|
|
{ |
51
|
|
|
if( $params = $constructor->getParameters() ) |
52
|
|
|
{ |
53
|
|
|
$constructorArgs = []; |
54
|
|
|
foreach( $params as $p ) |
55
|
|
|
{ |
56
|
|
|
$serviceName = ''; |
57
|
|
|
if( $p->getClass() ) { |
58
|
|
|
$serviceName = $p->getClass()->getName(); |
59
|
|
|
if (array_key_exists($serviceName, $this->aliases)) { |
60
|
|
|
$serviceName = $this->aliases[$serviceName]; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
else{ |
64
|
|
|
if( $p->getName() == 'config' ){ |
65
|
|
|
$serviceName = 'config'; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if(!$container->has($serviceName)){ |
70
|
|
|
throw new ServiceNotCreatedException(sprintf( |
71
|
|
|
'Can\'t create constructor argument "%s" for service "%s"', |
72
|
|
|
$p->getName(),$requestedName |
73
|
|
|
)); |
74
|
|
|
} |
75
|
|
|
try { |
76
|
|
|
$constructorArgs[] = $container->get($serviceName); |
77
|
|
|
} |
78
|
|
|
catch (\Exception $x) { |
79
|
|
|
echo __CLASS__ . " couldn't create an instance of {$p->getName()} to satisfy the constructor for $requestedName."; |
80
|
|
|
exit; |
|
|
|
|
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
return $class->newInstanceArgs($constructorArgs); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return new $className; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Generate class name |
92
|
|
|
* |
93
|
|
|
* @param string $requestedName |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
|
|
private function getClassName($requestedName) |
97
|
|
|
{ |
98
|
|
|
$exp = explode('/',$requestedName); |
99
|
|
|
|
100
|
|
|
$className = array_shift($exp).'\\Controller\\'.implode('\\',$exp).'Controller'; |
101
|
|
|
if(!class_exists($className)){ |
102
|
|
|
throw new ServiceNotCreatedException( |
103
|
|
|
sprintf( |
104
|
|
|
'Can\'t find correct controller class for "%s"', |
105
|
|
|
$requestedName |
106
|
|
|
) |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $className; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
An exit expression should only be used in rare cases. For example, if you write a short command line script.
In most cases however, using an
exit
expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.