]> git.andy128k.dev Git - ipf.git/commitdiff
rework boot
authorAndrey Kutejko <andy128k@gmail.com>
Sun, 17 Mar 2019 10:24:01 +0000 (11:24 +0100)
committerAndrey Kutejko <andy128k@gmail.com>
Sun, 17 Mar 2019 10:24:01 +0000 (11:24 +0100)
composer.json
index.php
ipf.php
t/ImageProcessorTest.php
t/ProjectMock.php [new file with mode: 0644]
t/ProjectTest.php
t/project/htdocs/index.php

index 301c27e7fce3da6df980314504e7ba3324e294c9..21a8f4455f8599da80d987814bd771f1bd586b46 100644 (file)
@@ -44,6 +44,6 @@
   "minimum-stability": "dev",
   "scripts": {
     "lint": "phan",
-    "test": "phpunit --bootstrap t/project/htdocs/index.php t/"
+    "test": "phpunit t/"
   }
 }
index 5f92447ce228746fd632bc658cd212a4347c194f..30b3637003db429d4cb8627e6f574c3afad4f072 100644 (file)
--- 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 68e512549412ac6a3702a2e3c858e81cd5c1e563..8f4b15b57d654c4396f6b599d13978da5c719e1a 100644 (file)
--- 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()
index 24d618b330e53222489f4b8f43a48911815c590d..8920efebd4b9bac7fbc30056a1a29a1d5b5568be 100644 (file)
@@ -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 (file)
index 0000000..a36e4b1
--- /dev/null
@@ -0,0 +1,13 @@
+<?php
+
+class ProjectMock
+{
+    public function createProject()
+    {
+        $project_root = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'project';
+        $document_root = $project_root . DIRECTORY_SEPARATOR . 'htdocs';
+
+        $settings = IPF::configure($project_root, $document_root);
+        return IPF::createContainer($settings);
+    }
+}
index 3f41346a6c50cd8c9e9308274ec96b6dd1b597d5..1334d7c11d941423350a3c14f268f2e0c59b59b9 100644 (file)
@@ -4,14 +4,18 @@ class Project_Test extends PHPUnit_Framework_TestCase
 {
     public function testApps()
     {
-        $apps = IPF_Project::getInstance()->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);
-    }
 }
index c74b8def238152fa3aed90c5b77602089af7871e..05780bc8303d0f8902e11a6cc095be8cd5462ee9 100644 (file)
@@ -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);