-
-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathApplicationExtension.php
More file actions
144 lines (117 loc) · 4.19 KB
/
ApplicationExtension.php
File metadata and controls
144 lines (117 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
/**
* This file is part of the Nette Framework (http://nette.org)
* Copyright (c) 2004 David Grudl (http://davidgrudl.com)
*/
namespace Nette\Bridges\ApplicationDI;
use Nette,
Nette\Application\UI;
/**
* Application extension for Nette DI.
*
* @author David Grudl
*/
class ApplicationExtension extends Nette\DI\CompilerExtension
{
public $defaults = array(
'debugger' => TRUE,
'errorPresenter' => 'Nette:Error',
'catchExceptions' => NULL, // true|false|smart
'mapping' => NULL,
'scanDirs' => array(),
'scanComposer' => NULL,
'scanFilter' => 'Presenter',
);
/** @var bool */
private $debugMode;
public function __construct($debugMode = FALSE, array $scanDirs = NULL)
{
$this->defaults['scanDirs'] = (array) $scanDirs;
$this->defaults['scanComposer'] = class_exists('Composer\Autoload\ClassLoader');
$this->defaults['catchExceptions'] = !$debugMode;
$this->debugMode = $debugMode;
}
public function loadConfiguration()
{
$config = $this->validateConfig($this->defaults);
$container = $this->getContainerBuilder();
$container->addExcludedClasses(array('Nette\Application\UI\Control'));
$application = $container->addDefinition($this->prefix('application'))
->setClass('Nette\Application\Application')
->addSetup('$catchExceptions', array($config['catchExceptions'] === 'smart' ? NULL : $config['catchExceptions']))
->addSetup('$errorPresenter', array($config['errorPresenter']));
if ($config['debugger']) {
$application->addSetup('Nette\Bridges\ApplicationTracy\RoutingPanel::initializePanel');
}
$presenterFactory = $container->addDefinition($this->prefix('presenterFactory'))
->setClass('Nette\Application\IPresenterFactory')
->setFactory('Nette\Application\PresenterFactory', array(1 => $this->debugMode));
if ($config['mapping']) {
$presenterFactory->addSetup('setMapping', array($config['mapping']));
}
$container->addDefinition($this->prefix('linkGenerator'))
->setFactory('Nette\Application\LinkGenerator', array(
1 => new Nette\DI\Statement('@Nette\Http\Request::getUrl'),
));
if ($this->name === 'application') {
$container->addAlias('application', $this->prefix('application'));
$container->addAlias('nette.presenterFactory', $this->prefix('presenterFactory'));
}
}
public function beforeCompile()
{
$container = $this->getContainerBuilder();
$all = array();
foreach ($container->findByType('Nette\Application\IPresenter') as $def) {
$all[$def->getClass()] = $def;
}
$counter = 0;
foreach ($this->findPresenters() as $class) {
if (empty($all[$class])) {
$all[$class] = $container->addDefinition($this->prefix(++$counter))->setClass($class);
}
}
foreach ($all as $def) {
$def->setInject(TRUE)->setAutowired(FALSE)->addTag('nette.presenter', $def->getClass());
if (is_subclass_of($def->getClass(), 'Nette\Application\UI\Presenter')) {
$def->addSetup('$invalidLinkMode', array(
$this->debugMode ? UI\Presenter::INVALID_LINK_WARNING : UI\Presenter::INVALID_LINK_SILENT
));
}
}
}
/** @return string[] */
private function findPresenters()
{
$config = $this->getConfig();
$classes = array();
if ($config['scanDirs']) {
$robot = new Nette\Loaders\RobotLoader;
$robot->setCacheStorage(new Nette\Caching\Storages\DevNullStorage);
$robot->addDirectory($config['scanDirs']);
$robot->acceptFiles = '*' . $config['scanFilter'] . '*.php';
$robot->rebuild();
$classes = array_keys($robot->getIndexedClasses());
}
if ($config['scanComposer']) {
$rc = new \ReflectionClass('Composer\Autoload\ClassLoader');
$classFile = dirname($rc->getFileName()) . '/autoload_classmap.php';
if (is_file($classFile)) {
$this->getContainerBuilder()->addDependency($classFile);
$classes = array_merge($classes, array_keys(call_user_func(function($path) {
return require $path;
}, $classFile)));
}
}
$presenters = array();
foreach (array_unique($classes) as $class) {
if (strpos($class, $config['scanFilter']) !== FALSE && class_exists($class)
&& ($rc = new \ReflectionClass($class)) && $rc->implementsInterface('Nette\Application\IPresenter')
&& !$rc->isAbstract()
) {
$presenters[] = $rc->getName();
}
}
return $presenters;
}
}