1: <?php
2: /**
3: * PHP version 5
4: * @package generalDriver
5: * @author Christian Schiffler <c.schiffler@cyberspectrum.de>
6: * @author Stefan Heimes <stefan_heimes@hotmail.com>
7: * @author Tristan Lins <tristan.lins@bit3.de>
8: * @copyright The MetaModels team.
9: * @license LGPL.
10: * @filesource
11: */
12:
13: namespace DcGeneral\Contao\Dca\Populator;
14:
15: use DcGeneral\Contao\Dca\Definition\ExtendedDca;
16: use DcGeneral\Controller\ControllerInterface;
17: use DcGeneral\EnvironmentInterface;
18: use DcGeneral\EnvironmentPopulator\AbstractEventDrivenEnvironmentPopulator;
19: use DcGeneral\View\ViewInterface;
20:
21: /**
22: * Class ExtendedLegacyDcaPopulator.
23: *
24: * This class only populates the environment with the extended information available via the ExtendedDca data definition
25: * section.
26: *
27: * @package DcGeneral\Contao\Dca\Populator
28: */
29: class ExtendedLegacyDcaPopulator extends AbstractEventDrivenEnvironmentPopulator
30: {
31: const PRIORITY = 100;
32:
33: /**
34: * Create a view instance in the environment if none has been defined yet.
35: *
36: * @param EnvironmentInterface $environment The environment to populate.
37: *
38: * @return void
39: *
40: * @internal
41: */
42: protected function populateView(EnvironmentInterface $environment)
43: {
44: // Already populated, get out then.
45: if ($environment->getView())
46: {
47: return;
48: }
49:
50: $definition = $environment->getDataDefinition();
51:
52: // If we encounter an extended definition, that one may override.
53: if (!$definition->hasDefinition(ExtendedDca::NAME))
54: {
55: return;
56: }
57:
58: /** @var ExtendedDca $extendedDefinition */
59: $extendedDefinition = $definition->getDefinition(ExtendedDca::NAME);
60: $class = $extendedDefinition->getViewClass();
61:
62: if (!$class)
63: {
64: return;
65: }
66:
67: $viewClass = new \ReflectionClass($class);
68:
69: /** @var ViewInterface $view */
70: $view = $viewClass->newInstance();
71:
72: $view->setEnvironment($environment);
73: $environment->setView($view);
74: }
75:
76: /**
77: * Create a controller instance in the environment if none has been defined yet.
78: *
79: * @param EnvironmentInterface $environment The environment to populate.
80: *
81: * @return void
82: *
83: * @internal
84: */
85: public function populateController(EnvironmentInterface $environment)
86: {
87: // Already populated, get out then.
88: if ($environment->getController())
89: {
90: return;
91: }
92:
93: $definition = $environment->getDataDefinition();
94:
95: // If we encounter an extended definition, that one may override.
96: if (!$definition->hasDefinition(ExtendedDca::NAME))
97: {
98: return;
99: }
100:
101: /** @var ExtendedDca $extendedDefinition */
102: $extendedDefinition = $definition->getDefinition(ExtendedDca::NAME);
103: $class = $extendedDefinition->getControllerClass();
104:
105: if (!$class)
106: {
107: return;
108: }
109:
110: $controllerClass = new \ReflectionClass($class);
111:
112: /** @var ControllerInterface $controller */
113: $controller = $controllerClass->newInstance();
114:
115: $controller->setEnvironment($environment);
116: $environment->setController($controller);
117: }
118:
119: /**
120: * {@inheritDoc}
121: */
122: public function populate(EnvironmentInterface $environment)
123: {
124: $this->populateView($environment);
125: $this->populateController($environment);
126: }
127: }
128: