Completed
Push — develop ( 3062c0...09456b )
by Mathias
07:45
created

LazyControllerFactory::getClassName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 1
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;
0 ignored issues
show
Coding Style Compatibility introduced by
The method __invoke() contains an exit expression.

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.

Loading history...
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