From e1efcebf01dd5aaf1090b56435cbabe5f8b6e825 Mon Sep 17 00:00:00 2001 From: David Grudl <david@grudl.com> Date: Wed, 10 Jun 2020 18:30:20 +0200 Subject: [PATCH] added ConstantsExtension & PhpExtension (moved from nette/di) --- composer.json | 2 +- src/Bootstrap/Configurator.php | 4 +- .../Extensions/ConstantsExtension.php | 26 ++++++++++ src/Bootstrap/Extensions/PhpExtension.php | 52 +++++++++++++++++++ tests/Bootstrap/ConstantsExtension.phpt | 27 ++++++++++ 5 files changed, 108 insertions(+), 3 deletions(-) create mode 100644 src/Bootstrap/Extensions/ConstantsExtension.php create mode 100644 src/Bootstrap/Extensions/PhpExtension.php create mode 100644 tests/Bootstrap/ConstantsExtension.phpt diff --git a/composer.json b/composer.json index 527b165..65560a3 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ ], "require": { "php": ">=7.2 <8.1", - "nette/di": "^3.0", + "nette/di": "^3.0.3", "nette/utils": "^3.0" }, "suggest": { diff --git a/src/Bootstrap/Configurator.php b/src/Bootstrap/Configurator.php index e6051f0..3317962 100644 --- a/src/Bootstrap/Configurator.php +++ b/src/Bootstrap/Configurator.php @@ -31,7 +31,7 @@ class Configurator public $defaultExtensions = [ 'application' => [Nette\Bridges\ApplicationDI\ApplicationExtension::class, ['%debugMode%', ['%appDir%'], '%tempDir%/cache/nette.application']], 'cache' => [Nette\Bridges\CacheDI\CacheExtension::class, ['%tempDir%']], - 'constants' => Nette\DI\Extensions\ConstantsExtension::class, + 'constants' => Extensions\ConstantsExtension::class, 'database' => [Nette\Bridges\DatabaseDI\DatabaseExtension::class, ['%debugMode%']], 'decorator' => Nette\DI\Extensions\DecoratorExtension::class, 'di' => [Nette\DI\Extensions\DIExtension::class, ['%debugMode%']], @@ -41,7 +41,7 @@ class Configurator 'inject' => Nette\DI\Extensions\InjectExtension::class, 'latte' => [Nette\Bridges\ApplicationDI\LatteExtension::class, ['%tempDir%/cache/latte', '%debugMode%']], 'mail' => Nette\Bridges\MailDI\MailExtension::class, - 'php' => Nette\DI\Extensions\PhpExtension::class, + 'php' => Extensions\PhpExtension::class, 'routing' => [Nette\Bridges\ApplicationDI\RoutingExtension::class, ['%debugMode%']], 'search' => [Nette\DI\Extensions\SearchExtension::class, ['%tempDir%/cache/nette.search']], 'security' => [Nette\Bridges\SecurityDI\SecurityExtension::class, ['%debugMode%']], diff --git a/src/Bootstrap/Extensions/ConstantsExtension.php b/src/Bootstrap/Extensions/ConstantsExtension.php new file mode 100644 index 0000000..1343371 --- /dev/null +++ b/src/Bootstrap/Extensions/ConstantsExtension.php @@ -0,0 +1,26 @@ +<?php + +/** + * This file is part of the Nette Framework (https://nette.org) + * Copyright (c) 2004 David Grudl (https://davidgrudl.com) + */ + +declare(strict_types=1); + +namespace Nette\Bootstrap\Extensions; + +use Nette; + + +/** + * Constant definitions. + */ +final class ConstantsExtension extends Nette\DI\CompilerExtension +{ + public function loadConfiguration() + { + foreach ($this->getConfig() as $name => $value) { + $this->initialization->addBody('define(?, ?);', [$name, $value]); + } + } +} diff --git a/src/Bootstrap/Extensions/PhpExtension.php b/src/Bootstrap/Extensions/PhpExtension.php new file mode 100644 index 0000000..a3e90ea --- /dev/null +++ b/src/Bootstrap/Extensions/PhpExtension.php @@ -0,0 +1,52 @@ +<?php + +/** + * This file is part of the Nette Framework (https://nette.org) + * Copyright (c) 2004 David Grudl (https://davidgrudl.com) + */ + +declare(strict_types=1); + +namespace Nette\Bootstrap\Extensions; + +use Nette; + + +/** + * PHP directives definition. + */ +final class PhpExtension extends Nette\DI\CompilerExtension +{ + public function getConfigSchema(): Nette\Schema\Schema + { + return Nette\Schema\Expect::arrayOf('scalar'); + } + + + public function loadConfiguration() + { + foreach ($this->getConfig() as $name => $value) { + if ($value === null) { + continue; + + } elseif ($name === 'include_path') { + $this->initialization->addBody('set_include_path(?);', [str_replace(';', PATH_SEPARATOR, $value)]); + + } elseif ($name === 'ignore_user_abort') { + $this->initialization->addBody('ignore_user_abort(?);', [$value]); + + } elseif ($name === 'max_execution_time') { + $this->initialization->addBody('set_time_limit(?);', [$value]); + + } elseif ($name === 'date.timezone') { + $this->initialization->addBody('date_default_timezone_set(?);', [$value]); + + } elseif (function_exists('ini_set')) { + $this->initialization->addBody('ini_set(?, ?);', [$name, $value === false ? '0' : (string) $value]); + + } elseif (ini_get($name) !== (string) $value) { + throw new Nette\NotSupportedException('Required function ini_set() is disabled.'); + } + } + } +} diff --git a/tests/Bootstrap/ConstantsExtension.phpt b/tests/Bootstrap/ConstantsExtension.phpt new file mode 100644 index 0000000..c01fbc7 --- /dev/null +++ b/tests/Bootstrap/ConstantsExtension.phpt @@ -0,0 +1,27 @@ +<?php + +declare(strict_types=1); + +use Nette\DI; +use Tester\Assert; + + +require __DIR__ . '/../bootstrap.php'; + + +$compiler = new DI\Compiler; +$compiler->setClassName('Container'); +$compiler->addExtension('constants', new Nette\Bootstrap\Extensions\ConstantsExtension); +$compiler->addConfig([ + 'constants' => [ + 'a' => 'hello', + 'A' => 'WORLD', + ], +]); +eval($compiler->compile()); + +$container = new Container; +$container->initialize(); + +Assert::same('hello', a); +Assert::same('WORLD', A);