PluginProbe
Packeta / 1.6.2
Packeta v1.6.2
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
packeta / deps / nette / bootstrap / src / Bootstrap / Configurator.php

Configurator.php in Packeta 1.6.2, at deps/nette/bootstrap/src/Bootstrap/Configurator.php

258 lines 11.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * This file is part of the Nette Framework (https://nette.org)
5 * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6 */
7 declare (strict_types=1);
8 namespace Packetery\Nette\Bootstrap;
9
10 use Packetery\Composer\Autoload\ClassLoader;
11 use Packetery\Latte;
12 use Packetery\Nette;
13 use Packetery\Nette\DI;
14 use Packetery\Tracy;
15 /**
16 * Initial system DI container generator.
17 */
18 class Configurator
19 {
20 use \Packetery\Nette\SmartObject;
21 public const COOKIE_SECRET = 'nette-debug';
22 /** @var callable[] function (Configurator $sender, DI\Compiler $compiler); Occurs after the compiler is created */
23 public $onCompile = [];
24 /** @var array */
25 public $defaultExtensions = ['application' => [\Packetery\Nette\Bridges\ApplicationDI\ApplicationExtension::class, ['%debugMode%', ['%appDir%'], '%tempDir%/cache/nette.application']], 'cache' => [\Packetery\Nette\Bridges\CacheDI\CacheExtension::class, ['%tempDir%']], 'constants' => Extensions\ConstantsExtension::class, 'database' => [\Packetery\Nette\Bridges\DatabaseDI\DatabaseExtension::class, ['%debugMode%']], 'decorator' => \Packetery\Nette\DI\Extensions\DecoratorExtension::class, 'di' => [\Packetery\Nette\DI\Extensions\DIExtension::class, ['%debugMode%']], 'extensions' => \Packetery\Nette\DI\Extensions\ExtensionsExtension::class, 'forms' => \Packetery\Nette\Bridges\FormsDI\FormsExtension::class, 'http' => [\Packetery\Nette\Bridges\HttpDI\HttpExtension::class, ['%consoleMode%']], 'inject' => \Packetery\Nette\DI\Extensions\InjectExtension::class, 'latte' => [\Packetery\Nette\Bridges\ApplicationDI\LatteExtension::class, ['%tempDir%/cache/latte', '%debugMode%']], 'mail' => \Packetery\Nette\Bridges\MailDI\MailExtension::class, 'php' => Extensions\PhpExtension::class, 'routing' => [\Packetery\Nette\Bridges\ApplicationDI\RoutingExtension::class, ['%debugMode%']], 'search' => [\Packetery\Nette\DI\Extensions\SearchExtension::class, ['%tempDir%/cache/nette.search']], 'security' => [\Packetery\Nette\Bridges\SecurityDI\SecurityExtension::class, ['%debugMode%']], 'session' => [\Packetery\Nette\Bridges\HttpDI\SessionExtension::class, ['%debugMode%', '%consoleMode%']], 'tracy' => [Tracy\Bridges\Nette\TracyExtension::class, ['%debugMode%', '%consoleMode%']]];
26 /** @var string[] of classes which shouldn't be autowired */
27 public $autowireExcludedClasses = [\ArrayAccess::class, \Countable::class, \IteratorAggregate::class, \stdClass::class, \Traversable::class];
28 /** @var array */
29 protected $staticParameters;
30 /** @var array */
31 protected $dynamicParameters = [];
32 /** @var array */
33 protected $services = [];
34 /** @var array of string|array */
35 protected $configs = [];
36 public function __construct()
37 {
38 $this->staticParameters = $this->getDefaultParameters();
39 }
40 /**
41 * Set parameter %debugMode%.
42 * @param bool|string|array $value
43 * @return static
44 */
45 public function setDebugMode($value)
46 {
47 if (\is_string($value) || \is_array($value)) {
48 $value = static::detectDebugMode($value);
49 } elseif (!\is_bool($value)) {
50 throw new \Packetery\Nette\InvalidArgumentException(\sprintf('Value must be either a string, array, or boolean, %s given.', \gettype($value)));
51 }
52 $this->staticParameters['debugMode'] = $value;
53 $this->staticParameters['productionMode'] = !$this->staticParameters['debugMode'];
54 // compatibility
55 return $this;
56 }
57 public function isDebugMode() : bool
58 {
59 return $this->staticParameters['debugMode'];
60 }
61 /**
62 * Sets path to temporary directory.
63 * @return static
64 */
65 public function setTempDirectory(string $path)
66 {
67 $this->staticParameters['tempDir'] = $path;
68 return $this;
69 }
70 /**
71 * Sets the default timezone.
72 * @return static
73 */
74 public function setTimeZone(string $timezone)
75 {
76 \date_default_timezone_set($timezone);
77 @\ini_set('date.timezone', $timezone);
78 // @ - function may be disabled
79 return $this;
80 }
81 /**
82 * Alias for addStaticParameters()
83 * @return static
84 */
85 public function addParameters(array $params)
86 {
87 return $this->addStaticParameters($params);
88 }
89 /**
90 * Adds new static parameters.
91 * @return static
92 */
93 public function addStaticParameters(array $params)
94 {
95 $this->staticParameters = DI\Config\Helpers::merge($params, $this->staticParameters);
96 return $this;
97 }
98 /**
99 * Adds new dynamic parameters.
100 * @return static
101 */
102 public function addDynamicParameters(array $params)
103 {
104 $this->dynamicParameters = $params + $this->dynamicParameters;
105 return $this;
106 }
107 /**
108 * Add instances of services.
109 * @return static
110 */
111 public function addServices(array $services)
112 {
113 $this->services = $services + $this->services;
114 return $this;
115 }
116 protected function getDefaultParameters() : array
117 {
118 $trace = \debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS);
119 $last = \end($trace);
120 $debugMode = static::detectDebugMode();
121 $loaderRc = \class_exists(ClassLoader::class) ? new \ReflectionClass(ClassLoader::class) : null;
122 return ['appDir' => isset($trace[1]['file']) ? \dirname($trace[1]['file']) : null, 'wwwDir' => isset($last['file']) ? \dirname($last['file']) : null, 'vendorDir' => $loaderRc ? \dirname($loaderRc->getFileName(), 2) : null, 'debugMode' => $debugMode, 'productionMode' => !$debugMode, 'consoleMode' => \PHP_SAPI === 'cli'];
123 }
124 public function enableTracy(string $logDirectory = null, string $email = null) : void
125 {
126 if (!\class_exists(Tracy\Debugger::class)) {
127 throw new \Packetery\Nette\NotSupportedException('Tracy not found, do you have `tracy/tracy` package installed?');
128 }
129 Tracy\Debugger::$strictMode = \true;
130 Tracy\Debugger::enable(!$this->staticParameters['debugMode'], $logDirectory, $email);
131 Tracy\Bridges\Nette\Bridge::initialize();
132 if (\class_exists(Latte\Bridges\Tracy\BlueScreenPanel::class)) {
133 Latte\Bridges\Tracy\BlueScreenPanel::initialize();
134 }
135 }
136 /**
137 * Alias for enableTracy()
138 */
139 public function enableDebugger(string $logDirectory = null, string $email = null) : void
140 {
141 $this->enableTracy($logDirectory, $email);
142 }
143 /**
144 * @throws \Packetery\Nette\NotSupportedException if RobotLoader is not available
145 */
146 public function createRobotLoader() : \Packetery\Nette\Loaders\RobotLoader
147 {
148 if (!\class_exists(\Packetery\Nette\Loaders\RobotLoader::class)) {
149 throw new \Packetery\Nette\NotSupportedException('RobotLoader not found, do you have `nette/robot-loader` package installed?');
150 }
151 $loader = new \Packetery\Nette\Loaders\RobotLoader();
152 $loader->setTempDirectory($this->getCacheDirectory() . '/nette.robotLoader');
153 $loader->setAutoRefresh($this->staticParameters['debugMode']);
154 if (isset($this->defaultExtensions['application'])) {
155 $this->defaultExtensions['application'][1][1] = null;
156 $this->defaultExtensions['application'][1][3] = $loader;
157 }
158 return $loader;
159 }
160 /**
161 * Adds configuration file.
162 * @param string|array $config
163 * @return static
164 */
165 public function addConfig($config)
166 {
167 $this->configs[] = $config;
168 return $this;
169 }
170 /**
171 * Returns system DI container.
172 */
173 public function createContainer() : DI\Container
174 {
175 $class = $this->loadContainer();
176 $container = new $class($this->dynamicParameters);
177 foreach ($this->services as $name => $service) {
178 $container->addService($name, $service);
179 }
180 $container->initialize();
181 return $container;
182 }
183 /**
184 * Loads system DI container class and returns its name.
185 */
186 public function loadContainer() : string
187 {
188 $loader = new DI\ContainerLoader($this->getCacheDirectory() . '/nette.configurator', $this->staticParameters['debugMode']);
189 return $loader->load([$this, 'generateContainer'], [
190 $this->staticParameters,
191 \array_keys($this->dynamicParameters),
192 $this->configs,
193 \PHP_VERSION_ID - \PHP_RELEASE_VERSION,
194 // minor PHP version
195 \class_exists(ClassLoader::class) ? \filemtime((new \ReflectionClass(ClassLoader::class))->getFilename()) : null,
196 ]);
197 }
198 /**
199 * @internal
200 */
201 public function generateContainer(DI\Compiler $compiler) : void
202 {
203 $loader = $this->createLoader();
204 $loader->setParameters($this->staticParameters);
205 foreach ($this->configs as $config) {
206 if (\is_string($config)) {
207 $compiler->loadConfig($config, $loader);
208 } else {
209 $compiler->addConfig($config);
210 }
211 }
212 $compiler->addConfig(['parameters' => DI\Helpers::escape($this->staticParameters)]);
213 $compiler->setDynamicParameterNames(\array_keys($this->dynamicParameters));
214 $builder = $compiler->getContainerBuilder();
215 $builder->addExcludedClasses($this->autowireExcludedClasses);
216 foreach ($this->defaultExtensions as $name => $extension) {
217 [$class, $args] = \is_string($extension) ? [$extension, []] : $extension;
218 if (\class_exists($class)) {
219 $args = DI\Helpers::expand($args, $this->staticParameters);
220 $compiler->addExtension($name, (new \ReflectionClass($class))->newInstanceArgs($args));
221 }
222 }
223 \Packetery\Nette\Utils\Arrays::invoke($this->onCompile, $this, $compiler);
224 }
225 protected function createLoader() : DI\Config\Loader
226 {
227 return new DI\Config\Loader();
228 }
229 protected function getCacheDirectory() : string
230 {
231 if (empty($this->staticParameters['tempDir'])) {
232 throw new \Packetery\Nette\InvalidStateException('Set path to temporary directory using setTempDirectory().');
233 }
234 $dir = $this->staticParameters['tempDir'] . '/cache';
235 \Packetery\Nette\Utils\FileSystem::createDir($dir);
236 return $dir;
237 }
238 /********************* tools ****************d*g**/
239 /**
240 * Detects debug mode by IP addresses or computer names whitelist detection.
241 * @param string|array $list
242 */
243 public static function detectDebugMode($list = null) : bool
244 {
245 $addr = $_SERVER['REMOTE_ADDR'] ?? \php_uname('n');
246 $secret = \is_string($_COOKIE[self::COOKIE_SECRET] ?? null) ? $_COOKIE[self::COOKIE_SECRET] : null;
247 $list = \is_string($list) ? \preg_split('#[,\\s]+#', $list) : (array) $list;
248 if (!isset($_SERVER['HTTP_X_FORWARDED_FOR']) && !isset($_SERVER['HTTP_FORWARDED'])) {
249 $list[] = '127.0.0.1';
250 $list[] = '::1';
251 $list[] = '[::1]';
252 // workaround for PHP < 7.3.4
253 }
254 return \in_array($addr, $list, \true) || \in_array("{$secret}@{$addr}", $list, \true);
255 }
256 }
257 \class_exists(\Packetery\Nette\Configurator::class);
258