初始化载入配置

Testing Is Documentation

tests/Kernel/Bootstrap/LoadOptionTest.php初始化载入配置 - 图1

QueryPHP 在内核执行过程中会执行初始化,分为 4 个步骤,载入配置、载入语言包、注册异常运行时和遍历服务提供者注册服务。

内核初始化,包括 \Leevel\Kernel\IKernel::bootstrap\Leevel\Kernel\IKernelConsole::bootstrap 均会执行上述 4 个步骤。

Uses

  1. <?php
  2. use Leevel\Di\Container;
  3. use Leevel\Di\IContainer;
  4. use Leevel\Filesystem\Helper;
  5. use Leevel\Kernel\App as Apps;
  6. use Leevel\Kernel\Bootstrap\LoadOption;
  7. use Leevel\Kernel\IApp;

基本使用方法

fixture 定义

环境变量 tests/Kernel/Bootstrap/app/.env

  1. # Environment production、testing and development
  2. ENVIRONMENT = development
  3. # Debug
  4. DEBUG = true
  5. # Encryption key
  6. AUTH_KEY = 7becb888f518b20224a988906df51e05

配置文件 tests/Kernel/Bootstrap/app/option/app.php

  1. <?php
  2. /*
  3. * This file is part of the ************************ package.
  4. * _____________ _______________
  5. * ______/ \__ _____ ____ ______ / /_ _________
  6. * ____/ __ / / / / _ \/ __`\/ / __ \/ __ \/ __ \___
  7. * __/ / / / /_/ / __/ / \ / /_/ / / / / /_/ /__
  8. * \_\ \_/\____/\___/_/ / / .___/_/ /_/ .___/
  9. * \_\ /_/_/ /_/
  10. *
  11. * The PHP Framework For Code Poem As Free As Wind. <Query Yet Simple>
  12. * (c) 2010-2020 http://queryphp.com All rights reserved.
  13. *
  14. * For the full copyright and license information, please view the LICENSE
  15. * file that was distributed with this source code.
  16. */
  17. return [
  18. 'environment' => getenv('ENVIRONMENT'),
  19. 'debug' => false,
  20. ];

配置文件 tests/Kernel/Bootstrap/app/option/demo.php

  1. <?php
  2. /*
  3. * This file is part of the ************************ package.
  4. * _____________ _______________
  5. * ______/ \__ _____ ____ ______ / /_ _________
  6. * ____/ __ / / / / _ \/ __`\/ / __ \/ __ \/ __ \___
  7. * __/ / / / /_/ / __/ / \ / /_/ / / / / /_/ /__
  8. * \_\ \_/\____/\___/_/ / / .___/_/ /_/ .___/
  9. * \_\ /_/_/ /_/
  10. *
  11. * The PHP Framework For Code Poem As Free As Wind. <Query Yet Simple>
  12. * (c) 2010-2020 http://queryphp.com All rights reserved.
  13. *
  14. * For the full copyright and license information, please view the LICENSE
  15. * file that was distributed with this source code.
  16. */
  17. return [
  18. 'foo' => 'bar',
  19. ];
  1. public function testBaseUse(): void
  2. {
  3. $bootstrap = new LoadOption();
  4. $container = Container::singletons();
  5. $app = new App3($container, $appPath = __DIR__.'/app');
  6. $this->assertInstanceof(IContainer::class, $container);
  7. $this->assertInstanceof(Container::class, $container);
  8. $this->assertInstanceof(IApp::class, $app);
  9. $this->assertInstanceof(Apps::class, $app);
  10. $this->assertSame($appPath.'/bootstrap/option.php', $app->optionCachedPath());
  11. $this->assertFalse($app->isCachedOption());
  12. $this->assertSame($appPath.'/option', $app->optionPath());
  13. $this->assertNull($bootstrap->handle($app, true));
  14. $option = $container->make('option');
  15. $this->assertSame('development', $option->get('environment'));
  16. $this->assertSame('bar', $option->get('demo\\foo'));
  17. }

RUNTIME_ENVIRONMENT 载入自定义环境变量文件

设置 RUNTIME_ENVIRONMENT 环境变量可以载入自定义环境变量文件。

fixture 定义

环境变量 tests/Kernel/Bootstrap/app/.fooenv

  1. # Environment production、testing and development
  2. ENVIRONMENT = testing
  3. # Debug
  4. DEBUG = true
  5. # Encryption key
  6. AUTH_KEY = 7becb888f518b20224a988906df51e05
  1. public function testWithRuntimeEnv(): void
  2. {
  3. putenv('RUNTIME_ENVIRONMENT=fooenv');
  4. $bootstrap = new LoadOption();
  5. $container = Container::singletons();
  6. $app = new App3($container, $appPath = __DIR__.'/app');
  7. $this->assertInstanceof(IContainer::class, $container);
  8. $this->assertInstanceof(Container::class, $container);
  9. $this->assertInstanceof(IApp::class, $app);
  10. $this->assertInstanceof(Apps::class, $app);
  11. $this->assertSame($appPath.'/bootstrap/fooenv.php', $app->optionCachedPath());
  12. $this->assertFalse($app->isCachedOption());
  13. $this->assertSame($appPath.'/option', $app->optionPath());
  14. $this->assertNull($bootstrap->handle($app, true));
  15. $option = $container->make('option');
  16. $this->assertSame('testing', $option->get('environment'));
  17. $this->assertSame('bar', $option->get('demo\\foo'));
  18. }

配置支持缓存

配置文件支持缓存,通过缓存可以降低开销提高性能,适合生产环境。

fixture 定义

配置缓存文件 tests/Kernel/Bootstrap/app/assert/option.php

  1. <?php
  2. /*
  3. * This file is part of the ************************ package.
  4. * _____________ _______________
  5. * ______/ \__ _____ ____ ______ / /_ _________
  6. * ____/ __ / / / / _ \/ __`\/ / __ \/ __ \/ __ \___
  7. * __/ / / / /_/ / __/ / \ / /_/ / / / / /_/ /__
  8. * \_\ \_/\____/\___/_/ / / .___/_/ /_/ .___/
  9. * \_\ /_/_/ /_/
  10. *
  11. * The PHP Framework For Code Poem As Free As Wind. <Query Yet Simple>
  12. * (c) 2010-2020 http://queryphp.com All rights reserved.
  13. *
  14. * For the full copyright and license information, please view the LICENSE
  15. * file that was distributed with this source code.
  16. */
  17. return [
  18. 'app' => [
  19. 'environment' => 'development',
  20. 'debug' => false,
  21. ':env' => [
  22. 'environment' => 'development',
  23. 'debug' => true,
  24. 'app_auth_key' => '7becb888f518b20224a988906df51e05',
  25. 'foo' => null,
  26. ],
  27. ':deferred_providers' => [
  28. 0 => [
  29. ],
  30. 1 => [
  31. ],
  32. ],
  33. ':composer' => [
  34. 'providers' => [
  35. ],
  36. 'ignores' => [
  37. ],
  38. 'commands' => [
  39. ],
  40. 'options' => [
  41. ],
  42. 'i18ns' => [
  43. ],
  44. 'metas' => [
  45. ],
  46. ],
  47. ],
  48. 'demo' => [
  49. 'foo' => 'bar',
  50. ],
  51. ];
  1. public function testLoadCached(): void
  2. {
  3. $bootstrap = new LoadOption();
  4. $container = Container::singletons();
  5. $app = new App3($container, $appPath = __DIR__.'/app');
  6. $this->assertInstanceof(IContainer::class, $container);
  7. $this->assertInstanceof(Container::class, $container);
  8. $this->assertInstanceof(IApp::class, $app);
  9. $this->assertInstanceof(Apps::class, $app);
  10. $this->assertSame($appPath.'/bootstrap/option.php', $app->optionCachedPath());
  11. $this->assertFalse($app->isCachedOption());
  12. $this->assertSame($appPath.'/option', $app->optionPath());
  13. mkdir($appPath.'/bootstrap', 0777, true);
  14. file_put_contents($appPath.'/bootstrap/option.php', file_get_contents($appPath.'/assert/option.php'));
  15. $this->assertTrue($app->isCachedOption());
  16. $this->assertNull($bootstrap->handle($app, true));
  17. $option = $container->make('option');
  18. $this->assertSame('development', $option->get('environment'));
  19. $this->assertSame('bar', $option->get('demo\\foo'));
  20. $this->assertNull($option->get(':env.foo'));
  21. $this->assertTrue($option->get(':env.debug'));
  22. }