1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sonata Project package. |
5
|
|
|
* |
6
|
|
|
* (c) Thomas Rabaix <[email protected]> |
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 Sonata\AdminBundle\Controller; |
13
|
|
|
|
14
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
15
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\ControllerTrait; |
16
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
17
|
|
|
use Symfony\Component\HttpFoundation\Response; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Polyfills the FrameworkBundle's original ControllerTrait. |
21
|
|
|
* |
22
|
|
|
* NEXT_MAJOR: Remove this file. |
23
|
|
|
* |
24
|
|
|
* @author Christian Kraus <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
trait PolyfillControllerTrait |
27
|
|
|
{ |
28
|
|
|
public function __call($methodName, $arguments) |
29
|
|
|
{ |
30
|
|
|
$this->proxyToController($methodName, $arguments); |
|
|
|
|
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function render($view, array $parameters = [], Response $response = null) |
34
|
|
|
{ |
35
|
|
|
return $this->__call('render', [$view, $parameters, $response]); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @internal |
40
|
|
|
*/ |
41
|
|
|
final protected function proxyToControllerClass($methodName, $arguments) |
42
|
|
|
{ |
43
|
|
|
if (!method_exists(Controller::class, $methodName)) { |
44
|
|
|
throw new \LogicException('Call to undefined method '.__CLASS__.'::'.$methodName); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$controller = new PolyfillProxyContainer($this->container); |
|
|
|
|
48
|
|
|
|
49
|
|
|
return $controller->proxyCall($methodName, $arguments); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
class PolyfillProxyContainer extends Controller |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
public function __construct(ContainerInterface $container) |
56
|
|
|
{ |
57
|
|
|
$this->setContainer($container); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function proxyCall($method, $arguments) |
61
|
|
|
{ |
62
|
|
|
return call_user_func_array([$this, $method], $arguments); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if (!trait_exists(ControllerTrait::class)) { |
67
|
|
|
class_alias(PolyfillControllerTrait::class, ControllerTrait::class); |
68
|
|
|
} |
69
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.