Version 1
[yaffs-website] / vendor / symfony / http-kernel / Event / FilterControllerEvent.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\HttpKernel\Event;
13
14 use Symfony\Component\HttpKernel\HttpKernelInterface;
15 use Symfony\Component\HttpFoundation\Request;
16
17 /**
18  * Allows filtering of a controller callable.
19  *
20  * You can call getController() to retrieve the current controller. With
21  * setController() you can set a new controller that is used in the processing
22  * of the request.
23  *
24  * Controllers should be callables.
25  *
26  * @author Bernhard Schussek <bschussek@gmail.com>
27  */
28 class FilterControllerEvent extends KernelEvent
29 {
30     /**
31      * The current controller.
32      */
33     private $controller;
34
35     public function __construct(HttpKernelInterface $kernel, $controller, Request $request, $requestType)
36     {
37         parent::__construct($kernel, $request, $requestType);
38
39         $this->setController($controller);
40     }
41
42     /**
43      * Returns the current controller.
44      *
45      * @return callable
46      */
47     public function getController()
48     {
49         return $this->controller;
50     }
51
52     /**
53      * Sets a new controller.
54      *
55      * @param callable $controller
56      *
57      * @throws \LogicException
58      */
59     public function setController($controller)
60     {
61         // controller must be a callable
62         if (!is_callable($controller)) {
63             throw new \LogicException(sprintf('The controller must be a callable (%s given).', $this->varToString($controller)));
64         }
65
66         $this->controller = $controller;
67     }
68
69     private function varToString($var)
70     {
71         if (is_object($var)) {
72             return sprintf('Object(%s)', get_class($var));
73         }
74
75         if (is_array($var)) {
76             $a = array();
77             foreach ($var as $k => $v) {
78                 $a[] = sprintf('%s => %s', $k, $this->varToString($v));
79             }
80
81             return sprintf('Array(%s)', implode(', ', $a));
82         }
83
84         if (is_resource($var)) {
85             return sprintf('Resource(%s)', get_resource_type($var));
86         }
87
88         if (null === $var) {
89             return 'null';
90         }
91
92         if (false === $var) {
93             return 'false';
94         }
95
96         if (true === $var) {
97             return 'true';
98         }
99
100         return (string) $var;
101     }
102 }