From 8033b3b4af0f714e5cbd9160f1b2e06258daad0c Mon Sep 17 00:00:00 2001 From: Andrey Kutejko Date: Sun, 17 Mar 2019 11:24:01 +0100 Subject: [PATCH] rework boot --- composer.json | 2 +- index.php | 2 +- ipf.php | 54 +++++++++++++++++++++++++++----------- t/ImageProcessorTest.php | 1 + t/ProjectMock.php | 13 +++++++++ t/ProjectTest.php | 18 +++++-------- t/project/htdocs/index.php | 3 +-- 7 files changed, 63 insertions(+), 30 deletions(-) create mode 100644 t/ProjectMock.php diff --git a/composer.json b/composer.json index 301c27e..21a8f44 100644 --- a/composer.json +++ b/composer.json @@ -44,6 +44,6 @@ "minimum-stability": "dev", "scripts": { "lint": "phan", - "test": "phpunit --bootstrap t/project/htdocs/index.php t/" + "test": "phpunit t/" } } diff --git a/index.php b/index.php index 5f92447..30b3637 100644 --- a/index.php +++ b/index.php @@ -5,4 +5,4 @@ $here = dirname(__FILE__); $project_root = $here . '/..'; require_once $project_root . '/vendor/autoload.php'; -IPF::run($project_root, $here); +IPF::boot($project_root, $here); diff --git a/ipf.php b/ipf.php index 68e5125..8f4b15b 100644 --- a/ipf.php +++ b/ipf.php @@ -5,20 +5,32 @@ use Pimple\Container; final class IPF { - public static function run($project_root, $document_root) + public static function boot($project_root, $document_root) { - IPF::configure($project_root, $document_root); + $settings = IPF::configure($project_root, $document_root); + $container = self::createContainer($settings); + self::run($container); + } + /** + * @param IPF_Settings $settings + * @return Container + */ + public static function createContainer(IPF_Settings $settings) + { $container = new Container(); - $container['ipf_version'] = '0.7'; - $container['settings'] = IPF::$settings; + $container['settings'] = $settings; $container->register(new BootstrapProvider()); + self::$container = $container; + return $container; + } - if (IPF::get('debug')) { - error_reporting(E_ALL); - } - + /** + * @param Container $container + */ + private static function run(Container $container) + { if (php_sapi_name() === 'cli') { $container['cli']->run(); } else { @@ -30,9 +42,6 @@ final class IPF } } - /** @var IPF_Settings */ - public static $settings = null; - private static function checkSettings(IPF_Settings $settings) { $db = $settings->get('database'); @@ -55,12 +64,17 @@ final class IPF die('Specify site url routes'); } + /** + * @param string $project_root + * @param string $document_root + * @return IPF_Settings + */ public static function configure($project_root, $document_root) { $project_root = realpath($project_root); $document_root = realpath($document_root); - self::$settings = IPF_Settings::create() + $settings = IPF_Settings::create() ->apply(array( 'app_base' => '', 'debug' => false, @@ -79,17 +93,27 @@ final class IPF ->applyFile($project_root.'/project/settings.php') ->tryApplyFile($project_root.'/project/settings_local.php'); - self::checkSettings(self::$settings); + IPF::checkSettings($settings); + + if ($settings->get('debug')) { + error_reporting(E_ALL); + } - date_default_timezone_set(self::$settings->get('time_zone')); + date_default_timezone_set($settings->get('time_zone')); + + return $settings; } private function __construct() {} + private function __clone() {} + /** @var Container */ + private static $container; + public static function get($name, $default=null) { - return self::$settings->get($name, $default); + return self::$container['settings']->get($name, $default); } public static function getUploadPath() diff --git a/t/ImageProcessorTest.php b/t/ImageProcessorTest.php index 24d618b..8920efe 100644 --- a/t/ImageProcessorTest.php +++ b/t/ImageProcessorTest.php @@ -7,6 +7,7 @@ class Image_Processor_Test extends PHPUnit_Framework_TestCase protected function setUp() { + $container = (new ProjectMock())->createProject(); $this->root = IPF::getUploadPath(); IPF_Utils::removeDirectories($this->root . '/thumbs'); } diff --git a/t/ProjectMock.php b/t/ProjectMock.php new file mode 100644 index 0000000..a36e4b1 --- /dev/null +++ b/t/ProjectMock.php @@ -0,0 +1,13 @@ +appList(); - $this->assertEquals(array('IPF_Session', 'IPF_Auth', 'IPF_Admin'), array_keys($apps)); + $container = (new ProjectMock())->createProject(); + + $apps = $container['apps']; + $this->assertEquals(array('session', 'auth', 'admin'), array_keys($apps)); } public function testMiddlewares() { + $container = (new ProjectMock())->createProject(); + $middlewares = array(); - $middleware = $this->callMethod(IPF_Project::getInstance(), 'chainMiddlewares'); + $middleware = $container['pipeline']; while ($middleware != null) { $middlewares[] = $middleware; $middleware = $this->getProperty($middleware, 'next'); @@ -32,12 +36,4 @@ class Project_Test extends PHPUnit_Framework_TestCase $property->setAccessible(true); return $property->getValue($obj); } - - protected function callMethod($obj, $method, $args=array()) - { - $cls = new ReflectionClass($obj); - $method = $cls->getMethod($method); - $method->setAccessible(true); - return $method->invokeArgs($obj, $args); - } } diff --git a/t/project/htdocs/index.php b/t/project/htdocs/index.php index c74b8de..05780bc 100644 --- a/t/project/htdocs/index.php +++ b/t/project/htdocs/index.php @@ -2,5 +2,4 @@ $here = dirname(__FILE__); $project_root = $here . '/..'; require_once $project_root . '/../../vendor/autoload.php'; -IPF::configure($project_root, $here); -IPF_Project::getInstance(); +IPF::boot($project_root, $here); -- 2.49.0