-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added ConstantsExtension & PhpExtension (moved from nette/di)
- Loading branch information
Showing
5 changed files
with
108 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.'); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
e1efceb
There was a problem hiding this comment.
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
e1efceb
There was a problem hiding this comment.
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.