]> git.andy128k.dev Git - ipf.git/commitdiff
remove IPF_Project
authorAndrey Kutejko <andy128k@gmail.com>
Sat, 16 Mar 2019 19:04:30 +0000 (20:04 +0100)
committerAndrey Kutejko <andy128k@gmail.com>
Sat, 16 Mar 2019 19:29:21 +0000 (20:29 +0100)
14 files changed:
index.php
ipf.php
ipf/admin/app.php
ipf/admin/commands/syncperms.php
ipf/application.php
ipf/auth/app.php
ipf/bootstrap.php [new file with mode: 0644]
ipf/cli.php
ipf/command/collectstatic.php
ipf/command/migrate.php
ipf/middleware/base.php
ipf/middleware/serve_static.php
ipf/project.php [deleted file]
ipf/session/app.php

index a3b8e3a7c1583d15e3e0bef2f13aeccffc99a865..5f92447ce228746fd632bc658cd212a4347c194f 100644 (file)
--- a/index.php
+++ b/index.php
@@ -5,6 +5,4 @@
 $here = dirname(__FILE__);
 $project_root = $here . '/..';
 require_once $project_root . '/vendor/autoload.php';
-IPF::configure($project_root, $here);
-IPF_Project::getInstance()->run();
-
+IPF::run($project_root, $here);
diff --git a/ipf.php b/ipf.php
index 8c8c12873b9f54d7369e821e81af74a5e40ac8e4..9fb32bdb36b90e662e4dc8d68e721341dd20db6d 100644 (file)
--- a/ipf.php
+++ b/ipf.php
@@ -1,7 +1,34 @@
 <?php
 
+use IPF\BootstrapProvider;
+use Pimple\Container;
+
 final class IPF
 {
+    public static function run($project_root, $document_root)
+    {
+        IPF::configure($project_root, $document_root);
+
+        $container = new Container();
+
+        $container['settings'] = IPF::$settings;
+        $container->register(new BootstrapProvider());
+
+        if (IPF::get('debug')) {
+            error_reporting(E_ALL);
+        }
+
+        if (php_sapi_name() === 'cli') {
+            $container['cli']->run();
+        } else {
+            $request = new IPF_HTTP_Request;
+
+            $pipeline = $container['pipeline'];
+            $response = $pipeline->call($request);
+            $response->render($request->method !== 'HEAD');
+        }
+    }
+
     public static function version()
     {
         return '0.5 beta';
@@ -162,4 +189,3 @@ function __($str)
         $t = $tr[$t];
     return $t;
 }
-
index a50d4025a4b16d2968d06005b24261017270b75e..1cc936a114631e59224ad1fdc047d79dd87a018c 100644 (file)
@@ -8,7 +8,7 @@ class IPF_Admin_App extends IPF_Application
     /** @var Container */
     private $container;
 
-    function configure(Container $container, IPF_Settings $settings)
+    function configure(Container $container)
     {
         $container['admin_log'] = function ($c) {
             return new IPF_Admin_Log($c['db'], $c['router']);
@@ -149,7 +149,7 @@ class IPF_Admin_App extends IPF_Application
     public function commands()
     {
         return array(
-            new IPF_Admin_Command_SyncPerms($this, $this->container['db']),
+            new IPF_Admin_Command_SyncPerms($this->container['apps'], $this, $this->container['db']),
         );
     }
 }
index 7210c41a3b3d148857f04ad49fe10683b5974c9f..5353905455abb4dadb71808cfed4e9c807d9e2bb 100644 (file)
@@ -13,10 +13,14 @@ class IPF_Admin_Command_SyncPerms
     /** @var Connection */
     private $connection;
 
-    public function __construct(IPF_Admin_App $admin_app, Connection $connection)
+    /** @var IPF_Application[] */
+    private $apps;
+
+    public function __construct(array $apps, IPF_Admin_App $admin_app, Connection $connection)
     {
         $this->admin_app = $admin_app;
         $this->connection = $connection;
+        $this->apps = $apps;
     }
 
     public function run($args=null)
@@ -25,7 +29,7 @@ class IPF_Admin_Command_SyncPerms
 
         print "COLLECTED PERMISSIONS:\n";
         $permissions = array();
-        foreach (IPF_Project::getInstance()->appList() as $appname => $app) {
+        foreach ($this->apps as $app) {
             foreach ($this->admin_app->applicationComponents($app) as $component) {
                 foreach ($component->getPerms(null) as $permName) {
                     $name = $app->slug().'|'.$component->slug().'|'.$permName;
index 3c6da45efbf47816ebef1c484b28944fa8905136..57f8aee916a9ab5c3c11a1681b6921ac5f71f440 100644 (file)
@@ -1,14 +1,15 @@
 <?php
 
+use Pimple\Container;
+
 abstract class IPF_Application
 {
     /**
      * Configures application
      *
-     * @param \Pimple\Container $container
-     * @param IPF_Settings $settings
+     * @param Container $container
      */
-    public function configure(\Pimple\Container $container, IPF_Settings $settings)
+    public function configure(Container $container)
     {
     }
 
index fdf113b0677cdc9c8fc1e58f58c2e12cace4a6a1..a87b508fa999c8e9be08946e075a495eafff8e11 100644 (file)
@@ -17,7 +17,7 @@ class IPF_Auth_App extends IPF_Application
      * @param Container $container
      * @param IPF_Settings $settings
      */
-    public function configure(Container $container, IPF_Settings $settings)
+    public function configure(Container $container)
     {
         $this->container = $container;
         $this->userModel = $container['settings']->get('auth_user_model', User::class);
diff --git a/ipf/bootstrap.php b/ipf/bootstrap.php
new file mode 100644 (file)
index 0000000..9b6fc24
--- /dev/null
@@ -0,0 +1,71 @@
+<?php
+
+namespace IPF;
+
+use IPF\Error\ErrorPageProvider;
+use IPF\Template\TemplateProvider;
+use Pimple\Container;
+use Pimple\ServiceProviderInterface;
+
+class BootstrapProvider implements ServiceProviderInterface
+{
+    public function register(Container $container)
+    {
+        $container['apps'] = function ($c) {
+            $applicationNames = $c['settings']->get('applications');
+
+            $apps = array();
+            foreach ($applicationNames as $name) {
+                $className = $name . '_App';
+                /** @var \IPF_Application $app */
+                $app = new $className();
+
+                $apps[$app->slug()] = $app;
+            }
+            return $apps;
+        };
+        $container->extend('apps', function ($apps, $c) {
+            foreach ($apps as $app) {
+                $app->configure($c);
+            }
+            return $apps;
+        });
+
+        $container['router'] = function ($c) {
+            $settings = $c['settings'];
+            return new \IPF_Router(
+                $settings->get('urls'),
+                $settings->get('app_base')
+            );
+        };
+
+        $container->register(new CoreServicesProvider());
+        $container->register(new TemplateProvider());
+        $container->register(new ErrorPageProvider());
+
+        $container['databaseConnection'] = function ($c) {
+            return \IPF_Database::connect();
+        };
+        $container['db'] = $container->factory(function ($c) {
+            return \IPF_Database::connectDBAL();
+        });
+
+        $container->register(new CliProvider());
+
+        $container['pipeline'] = function ($c) {
+            $middlewares = $c['settings']->get('middlewares', array());
+            array_unshift($middlewares, 'IPF_Error_Middleware');
+            array_push($middlewares, 'IPF_Dispatch_Middleware');
+
+            $m = null;
+            foreach (array_reverse($middlewares) as $mw) {
+                $m = new $mw($m, $c);
+            }
+            return $m;
+        };
+
+        foreach ($container['settings']->get('providers', array()) as $providerClass) {
+            $container->register(new $providerClass);
+        }
+    }
+}
index bc1a9dac89eb112c0c9738232844047f7209b634..ad078be3dc77b3d540a58320171828794a64b5b0 100644 (file)
@@ -1,5 +1,7 @@
 <?php
 
+namespace IPF;
+
 /*
     Command is a class with following informal interface.
 
 use Pimple\Container;
 use Pimple\ServiceProviderInterface;
 
-class IPF_Cli_Provider implements ServiceProviderInterface {
-
+class CliProvider implements ServiceProviderInterface
+{
     public function register(Container $container)
     {
-        $container['ipf_commands'] = function($c) {
+        $container['ipf_commands'] = function ($c) {
             return array(
-                new IPF_Command_Shell,
-                new IPF_Command_DebugServer,
-                new IPF_Command_Routes($c['router']),
-                new IPF_Command_DB,
-                new IPF_Command_DBDump,
-                new IPF_Command_DBRestore,
-                new IPF_Command_CollectStatic,
-                new IPF_Command_Pack,
-                new IPF_Command_Unpack,
-                new IPF_Command_CreateMigration,
-                new IPF_Command_Migrate($c['apps'], $c['db']),
+                new \IPF_Command_Shell,
+                new \IPF_Command_DebugServer,
+                new \IPF_Command_Routes($c['router']),
+                new \IPF_Command_DB,
+                new \IPF_Command_DBDump,
+                new \IPF_Command_DBRestore,
+                new \IPF_Command_CollectStatic($c['apps']),
+                new \IPF_Command_Pack,
+                new \IPF_Command_Unpack,
+                new \IPF_Command_CreateMigration,
+                new \IPF_Command_Migrate($c['apps'], $c['db']),
             );
         };
 
-        $container['project_commands'] = function($c) {
+        $container['project_commands'] = function ($c) {
             $commands = array();
-            foreach (IPF::get('commands', array()) as $cmd) {
+            foreach (\IPF::get('commands', array()) as $cmd) {
                 if (is_string($cmd))
                     $cmd = new $cmd;
                 $commands[] = $cmd;
@@ -46,7 +48,7 @@ class IPF_Cli_Provider implements ServiceProviderInterface {
                 'IPF' => $c['ipf_commands'],
             ];
 
-            /** @var IPF_Application[] $apps */
+            /** @var \IPF_Application[] $apps */
             $apps = $c['apps'];
             foreach ($apps as $app) {
                 $app_commands = $app->commands();
@@ -94,7 +96,7 @@ class IPF_Cli
             print "\n\033[1;36m$group:\033[0m\n";
 
             foreach ($commands as $command) {
-                echo '    '.str_pad($command->command, 20) . "\t" . $command->description . "\n";
+                echo '    ' . str_pad($command->command, 20) . "\t" . $command->description . "\n";
             }
         }
 
@@ -110,12 +112,12 @@ class IPF_Cli
             return $_SERVER['argv'];
         if (@is_array($GLOBALS['HTTP_SERVER_VARS']['argv']))
             return $GLOBALS['HTTP_SERVER_VARS']['argv'];
-        throw new IPF_Exception("IPF_Cli: Could not read command arguments (register_argc_argv=Off?)");
+        throw new \Exception("IPF_Cli: Could not read command arguments (register_argc_argv=Off?)");
     }
 
     public function run()
     {
-        print "IPF command line tool. Version: ".IPF::version()."\n";
+        print "IPF command line tool. Version: " . \IPF::version() . "\n";
 
         $args = $this->getArgs();
 
@@ -126,14 +128,14 @@ class IPF_Cli
 
         $command = $this->getCommand($args[1]);
         if (!$command) {
-            print "Unknown command: '".$args[1]."'\n\n";
+            print "Unknown command: '" . $args[1] . "'\n\n";
             $this->usage();
             return;
         }
 
         try {
             $command->run(array_slice($args, 2));
-        } catch (Exception $e) {
+        } catch (\Exception $e) {
             $m = $e->getMessage();
             print "\n\033[1;31m$m\033[0m\n";
         }
index 635cece92de598673618e87af1035092ded999b0..8c3926b2c6e5e9283f69ce27b3c9f599ee98bfa0 100644 (file)
@@ -5,6 +5,14 @@ class IPF_Command_CollectStatic
     public $command = 'collectstatic';
     public $description = 'Collect static files';
 
+    /** @var IPF_Application[] */
+    private $apps;
+
+    function __construct(array $apps)
+    {
+        $this->apps = $apps;
+    }
+
     public function run($args=null)
     {
         if (!in_array('--quiet', $args))
@@ -12,7 +20,7 @@ class IPF_Command_CollectStatic
 
         $destination = IPF::get('document_root') . DIRECTORY_SEPARATOR . IPF::get('static_url');
 
-        foreach (IPF_Project::getInstance()->appList() as $app) {
+        foreach ($this->apps as $app) {
             $source = $app->getPath() . 'static';
             if (is_dir($source))
                 IPF_Utils::copyDirectory($source, $destination);
@@ -34,4 +42,3 @@ class IPF_Command_CollectStatic
         }
     }
 }
-
index 2535ec96338ac963a61b0f9491eb412ff84e3075..96acc9699347b073c31a10926cd0efabf8e1af69 100644 (file)
@@ -7,21 +7,21 @@ class IPF_Command_Migrate
     public $command = 'migrate';
     public $description = 'Migrate DB schema';
 
-    /** @var IPF_Application[] $appList */
-    private $appList;
+    /** @var IPF_Application[] $apps */
+    private $apps;
     /** @var Connection */
     private $connection;
 
-    function __construct(array $appList, Connection $connection)
+    function __construct(array $apps, Connection $connection)
     {
-        $this->appList = $appList;
+        $this->apps = $apps;
         $this->connection = $connection;
     }
 
     public function run($args=null)
     {
         $paths = array(IPF::get('project_root').'/project/db/migrations');
-        foreach ($this->appList as $app) {
+        foreach ($this->apps as $app) {
             $paths[] = $app->getPath() . 'migrations';
         }
 
index 5a493d163af16b34ba6ccfe52d0e6a7416154cf5..47f80c7a42fca6ce7d89b01d35aa830b30dfbfe7 100644 (file)
@@ -7,26 +7,16 @@ class IPF_Middleware
     /** @var IPF_Middleware */
     protected $next;
 
-    /** @var IPF_Settings */
-    protected $settings; // deprecated
-
-    /** @var IPF_Project */
-    protected $project; // deprecated
-
     /** @var Container */
     protected $container;
 
     /**
      * @param IPF_Middleware|null $next Next middleware in a chain
-     * @param IPF_Settings $settings
-     * @param IPF_Project $project
      * @param Container $container
      */
-    function __construct($next, IPF_Settings $settings, IPF_Project $project, Container $container)
+    function __construct($next, Container $container)
     {
         $this->next = $next;
-        $this->settings = $settings;
-        $this->project = $project;
         $this->container = $container;
     }
 
index 584b4842b19559baa9434441bc8544f8f4efcab9..d496b46a488d0ec4d759fdf0bbe1c059fbb76cbc 100644 (file)
@@ -11,7 +11,7 @@ class IPF_Serve_Static_Middleware extends IPF_Middleware
 
         $query = $matches[1];
 
-        foreach ($this->getAppList() as $app) {
+        foreach ($this->getApps() as $app) {
             $static = $app->getPath() . $staticUrl . $query;
             if (is_file($static))
                 return new IPF_HTTP_Response_File($static, null, $this->mimetype($query));
@@ -45,7 +45,7 @@ class IPF_Serve_Static_Middleware extends IPF_Middleware
     /**
      * @return IPF_Application[]
      */
-    private function getAppList()
+    private function getApps()
     {
         return $this->container['apps'];
     }
diff --git a/ipf/project.php b/ipf/project.php
deleted file mode 100644 (file)
index 38e513a..0000000
+++ /dev/null
@@ -1,105 +0,0 @@
-<?php
-
-use IPF\CoreServicesProvider;
-use IPF\Error\ErrorPageProvider;
-use IPF\Template\TemplateProvider;
-use Pimple\Container;
-
-final class IPF_Project
-{
-    /** @var Container */
-    private $container = null;
-    private $apps = array();
-
-    static private $instance = NULL;
-
-    static function getInstance()
-    {
-        if (self::$instance == NULL)
-            self::$instance = new IPF_Project;
-        return self::$instance;
-    }
-
-    private function __construct()
-    {
-        $this->container = new Container();
-        $this->container['settings'] = IPF::$settings;
-
-        $apps = array();
-        foreach (IPF::get('applications') as $name) {
-            $className = $name.'_App';
-            /** @var IPF_Application $app */
-            $app = new $className();
-            $app->configure($this->container, IPF::$settings);
-
-            $this->apps[$name] = $app;
-            $apps[$app->slug()] = $app;
-        }
-        $this->container['apps'] = $apps;
-        $this->container['router'] = function ($c) {
-            $settings = $c['settings'];
-            return new IPF_Router(
-                $settings->get('urls'),
-                $settings->get('app_base')
-            );
-        };
-    }
-
-    private function __clone()
-    {
-    }
-
-    public function appList()
-    {
-        return $this->apps;
-    }
-
-    /**
-     * @return IPF_Middleware
-     */
-    private function chainMiddlewares()
-    {
-        $middlewares = $this->container['settings']->get('middlewares', array());
-        array_unshift($middlewares, 'IPF_Error_Middleware');
-        array_push($middlewares, 'IPF_Dispatch_Middleware');
-
-        $m = null;
-        foreach (array_reverse($middlewares) as $mw) {
-            $m = new $mw($m, IPF::$settings, $this, $this->container);
-        }
-        return $m;
-    }
-
-    public function run()
-    {
-        $this->container->register(new CoreServicesProvider());
-        $this->container->register(new TemplateProvider());
-        $this->container->register(new ErrorPageProvider());
-
-        $this->container['databaseConnection'] = function ($c) {
-            return IPF_Database::connect();
-        };
-        $this->container['db'] = $this->container->factory(function ($c) {
-            return IPF_Database::connectDBAL();
-        });
-
-        $this->container->register(new IPF_Cli_Provider());
-
-        foreach ($this->container['settings']->get('providers', array()) as $providerClass) {
-            $this->container->register(new $providerClass);
-        }
-
-        if (IPF::get('debug')) {
-            error_reporting(E_ALL);
-        }
-
-        if (php_sapi_name() === 'cli') {
-            $this->container['cli']->run();
-        } else {
-            $request = new IPF_HTTP_Request;
-
-            $response = $this->chainMiddlewares()->call($request);
-            $response->render($request->method !== 'HEAD');
-        }
-    }
-}
index efb9893cf05118191992a34e0d2d125ac25b91f1..2a00e37219879493408840b058e74c5c36aa0e76 100644 (file)
@@ -5,7 +5,7 @@ class IPF_Session_App extends IPF_Application
     /** @var SessionBackend[] */
     private $backends;
 
-    public function configure(\Pimple\Container $container, IPF_Settings $settings)
+    public function configure(\Pimple\Container $container)
     {
         $this->backends = [
             new CookieSessionBackend(),