Completed
Pull Request — master (#138)
by Paul
03:43 queued 19s
created

ControllerResolver::instantiateController()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6667
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
/**
3
 * This file is part of the PPI Framework.
4
 *
5
 * @copyright   Copyright (c) 2011-2013 Paul Dragoonis <[email protected]>
6
 * @license     http://opensource.org/licenses/mit-license.php MIT
7
 *
8
 * @link        http://www.ppi.io
9
 */
10
11
namespace PPI\Framework\Module\Controller;
12
13
use Psr\Log\LoggerInterface;
14
use Symfony\Component\HttpKernel\Controller\ControllerResolver as BaseControllerResolver;
15
use Zend\ServiceManager\ServiceLocatorAwareInterface;
16
use Zend\ServiceManager\ServiceLocatorInterface;
17
18
/**
19
 * ControllerResolver.
20
 *
21
 * @see Symfony\Bundle\FrameworkBundle\Controller\ControllerResolver
22
 *
23
 * @author     Fabien Potencier <[email protected]>
24
 * @author     Vítor Brandão <[email protected]>
25
 * @author     Paul Dragoonis <[email protected]>
26
 */
27
class ControllerResolver extends BaseControllerResolver
28
{
29
30
    protected $request;
31
32
    /**
33
     * Constructor.
34
     *
35
     * @param ServiceLocatorInterface $serviceManager A ServiceLocatorInterface instance
36
     * @param ControllerNameParser $parser A ControllerNameParser instance
37
     * @param LoggerInterface $logger A LoggerInterface instance
38
     */
39
    public function __construct(ServiceLocatorInterface $serviceManager, ControllerNameParser $parser, LoggerInterface $logger = null)
40
    {
41
        $this->serviceManager = $serviceManager;
0 ignored issues
show
Bug introduced by
The property serviceManager does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
42
        $this->parser = $parser;
0 ignored issues
show
Bug introduced by
The property parser does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
43
        parent::__construct($logger);
44
    }
45
46
    /**
47
     * Returns a callable for the given controller.
48
     *
49
     * @param string $controller A Controller string
50
     *
51
     * @throws \LogicException           When the name could not be parsed
52
     * @throws \InvalidArgumentException When the controller class does not exist
53
     *
54
     * @return mixed A PHP callable
55
     */
56
    protected function createController($controller)
57
    {
58
59
        if (false === strpos($controller, '::')) {
60
            $count = substr_count($controller, ':');
61
            if (2 == $count) {
62
                // controller in the a:b:c notation then
63
                $controller = $this->parser->parse($controller);
64
            } elseif (1 == $count) {
65
                // controller in the service:method notation
66
                list($service, $method) = explode(':', $controller, 2);
67
68
                return array($this->container->get($service), $method);
0 ignored issues
show
Bug introduced by
The property container does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
69
            } elseif ($this->container->has($controller) && method_exists($service = $this->container->get($controller), '__invoke')) {
70
                return $service;
71
            } else {
72
                throw new \LogicException(sprintf('Unable to parse the controller name "%s".', $controller));
73
            }
74
        }
75
76
        return parent::createController($controller);
77
    }
78
79
    /**
80
     * @param string $class
81
     * @return object
82
     */
83
    protected function instantiateController($class)
84
    {
85
        $controller = parent::instantiateController($class);
86
87
        if ($controller instanceof ServiceLocatorAwareInterface) {
88
            $controller->setServiceLocator($this->serviceManager);
89
        }
90
        return $controller;
91
    }
92
93
}
94