Skip to content

Commit

Permalink
added ConstantsExtension & PhpExtension (moved from nette/di)
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Dec 31, 2020
1 parent 8ff4ddb commit e1efceb
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 3 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
],
"require": {
"php": ">=7.2 <8.1",
"nette/di": "^3.0",
"nette/di": "^3.0.3",
"nette/utils": "^3.0"
},
"suggest": {
Expand Down
4 changes: 2 additions & 2 deletions src/Bootstrap/Configurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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%']],
Expand All @@ -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%']],
Expand Down
26 changes: 26 additions & 0 deletions src/Bootstrap/Extensions/ConstantsExtension.php
Original file line number Diff line number Diff line change
@@ -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]);
}
}
}
52 changes: 52 additions & 0 deletions src/Bootstrap/Extensions/PhpExtension.php
Original file line number Diff line number Diff line change
@@ -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.');
}
}
}
}
27 changes: 27 additions & 0 deletions tests/Bootstrap/ConstantsExtension.phpt
Original file line number Diff line number Diff line change
@@ -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);

2 comments on commit e1efceb

@mabar
Copy link
Contributor

@mabar mabar commented on e1efceb Dec 31, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it mean you plan to delete these from nette/di?
They are useful even without Nette\Bootstrap

@dg
Copy link
Member Author

@dg dg commented on e1efceb Dec 31, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably yes, but in the distant future.

Please sign in to comment.