From: Andrey Kutejko Date: Sat, 16 Mar 2019 19:04:30 +0000 (+0100) Subject: remove IPF_Project X-Git-Url: https://git.andy128k.dev/?a=commitdiff_plain;h=ce5365e7d7d88b3dd388cf0d40228a5502781da5;p=ipf.git remove IPF_Project --- diff --git a/index.php b/index.php index a3b8e3a..5f92447 100644 --- 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 8c8c128..9fb32bd 100644 --- a/ipf.php +++ b/ipf.php @@ -1,7 +1,34 @@ 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; } - diff --git a/ipf/admin/app.php b/ipf/admin/app.php index a50d402..1cc936a 100644 --- a/ipf/admin/app.php +++ b/ipf/admin/app.php @@ -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']), ); } } diff --git a/ipf/admin/commands/syncperms.php b/ipf/admin/commands/syncperms.php index 7210c41..5353905 100644 --- a/ipf/admin/commands/syncperms.php +++ b/ipf/admin/commands/syncperms.php @@ -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; diff --git a/ipf/application.php b/ipf/application.php index 3c6da45..57f8aee 100644 --- a/ipf/application.php +++ b/ipf/application.php @@ -1,14 +1,15 @@ 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 index 0000000..9b6fc24 --- /dev/null +++ b/ipf/bootstrap.php @@ -0,0 +1,71 @@ +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); + } + } +} diff --git a/ipf/cli.php b/ipf/cli.php index bc1a9da..ad078be 100644 --- a/ipf/cli.php +++ b/ipf/cli.php @@ -1,5 +1,7 @@ $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"; } diff --git a/ipf/command/collectstatic.php b/ipf/command/collectstatic.php index 635cece..8c3926b 100644 --- a/ipf/command/collectstatic.php +++ b/ipf/command/collectstatic.php @@ -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 } } } - diff --git a/ipf/command/migrate.php b/ipf/command/migrate.php index 2535ec9..96acc96 100644 --- a/ipf/command/migrate.php +++ b/ipf/command/migrate.php @@ -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'; } diff --git a/ipf/middleware/base.php b/ipf/middleware/base.php index 5a493d1..47f80c7 100644 --- a/ipf/middleware/base.php +++ b/ipf/middleware/base.php @@ -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; } diff --git a/ipf/middleware/serve_static.php b/ipf/middleware/serve_static.php index 584b484..d496b46 100644 --- a/ipf/middleware/serve_static.php +++ b/ipf/middleware/serve_static.php @@ -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 index 38e513a..0000000 --- a/ipf/project.php +++ /dev/null @@ -1,105 +0,0 @@ -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'); - } - } -} diff --git a/ipf/session/app.php b/ipf/session/app.php index efb9893..2a00e37 100644 --- a/ipf/session/app.php +++ b/ipf/session/app.php @@ -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(),