]> git.andy128k.dev Git - ipf.git/commitdiff
parametrize commands
authorAndrey Kutejko <andy128k@gmail.com>
Wed, 20 Mar 2019 21:15:12 +0000 (22:15 +0100)
committerAndrey Kutejko <andy128k@gmail.com>
Wed, 20 Mar 2019 21:15:12 +0000 (22:15 +0100)
13 files changed:
composer.json
composer.lock
ipf/cli.php
ipf/command/collectstatic.php
ipf/command/createmigration.php
ipf/command/db.php
ipf/command/dbdump.php
ipf/command/dbrestore.php
ipf/command/debugserver.php
ipf/command/migrate.php
ipf/command/pack.php
ipf/command/shell.php
ipf/command/unpack.php

index c8b71eb0d32cec4f7ba3c8dfb8962448accd2179..32330a90321827d29e10fafbe663cc81b2a0ef54 100644 (file)
   },
   "require": {
     "pear/archive_tar": "1.3.*",
-    "doctrine/dbal": "v2.5.13",
+    "doctrine/dbal": "^2.5",
     "d11wtq/boris": "~1",
     "andy128k/missing-tools": "~0.3",
-    "andy128k/pegp": "0.2",
+    "andy128k/pegp": "~0.2",
     "pimple/pimple": "~3.0",
     "twig/twig": "~1"
   },
index 8666e4a6a86905574a399ca68db269084e1da235..18902b1104fd3f49358ed0f255c808f3e46bc4b8 100644 (file)
@@ -4,7 +4,7 @@
         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
         "This file is @generated automatically"
     ],
-    "content-hash": "b880eb10592b0ae55866fd75d9136ab7",
+    "content-hash": "644aeae9afe6d53dd080f9d189b994f5",
     "packages": [
         {
             "name": "andy128k/missing-tools",
index f935246497263ad0589109fd067a5de66c74c822..a3ecec6484d02302b4ada747281deede582b036f 100644 (file)
@@ -18,18 +18,25 @@ class CliProvider implements ServiceProviderInterface
     public function register(Container $container)
     {
         $container['ipf_commands'] = function ($c) {
+            /** @var \IPF_Settings $settings */
+            $settings = $c['settings'];
+            $document_root = $settings->get('document_root');
+            $project_root = $settings->get('project_root');
+            $staticDir = $document_root . DIRECTORY_SEPARATOR . $settings->get('static_url');
+            $uploadsDir = $document_root . DIRECTORY_SEPARATOR . $settings->get('upload_url');
+            $databaseConfig = $settings->get('database');
             return array(
                 new \IPF_Command_Shell,
-                new \IPF_Command_DebugServer,
+                new \IPF_Command_DebugServer($document_root),
                 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']),
+                new \IPF_Command_DB($databaseConfig),
+                new \IPF_Command_DBDump($databaseConfig),
+                new \IPF_Command_DBRestore($databaseConfig),
+                new \IPF_Command_CollectStatic($c['apps'], $staticDir),
+                new \IPF_Command_Pack($uploadsDir, $databaseConfig),
+                new \IPF_Command_Unpack($uploadsDir, $databaseConfig),
+                new \IPF_Command_CreateMigration($project_root),
+                new \IPF_Command_Migrate($c['apps'], $c['db'], $project_root),
             );
         };
 
index 8c3926b2c6e5e9283f69ce27b3c9f599ee98bfa0..2e3d46d82958a26765cdcbdfdb4dd7565f0af41f 100644 (file)
@@ -7,10 +7,13 @@ class IPF_Command_CollectStatic
 
     /** @var IPF_Application[] */
     private $apps;
+    /** @var string */
+    private $staticDir;
 
-    function __construct(array $apps)
+    function __construct(array $apps, $staticDir)
     {
         $this->apps = $apps;
+        $this->staticDir = $staticDir;
     }
 
     public function run($args=null)
@@ -18,18 +21,16 @@ class IPF_Command_CollectStatic
         if (!in_array('--quiet', $args))
             print "Collecting static files\n";
 
-        $destination = IPF::get('document_root') . DIRECTORY_SEPARATOR . IPF::get('static_url');
-
         foreach ($this->apps as $app) {
             $source = $app->getPath() . 'static';
             if (is_dir($source))
-                IPF_Utils::copyDirectory($source, $destination);
+                IPF_Utils::copyDirectory($source, $this->staticDir);
 
             foreach ($app->assets() as $asset => $output) {
                 $compiler = new IPF_Assets($app->getPath());
 
                 $src = $compiler->path($asset);
-                $dest = $destination . $output;
+                $dest = $this->staticDir . $output;
 
                 if (is_dir($src)) {
                     IPF_Utils::copyDirectory($src, $dest);
index b2c586bbaf198a498c33a9f48885a3273094004e..83ef0c426f985a99e773b4615a00475e13ec68d0 100644 (file)
@@ -7,10 +7,18 @@ class IPF_Command_CreateMigration
     public $command = 'createmigration';
     public $description = 'Create migration';
 
-    public function run($args=null)
+    /** @var string */
+    private $project_root;
+
+    function __construct($project_root)
+    {
+        $this->project_root = $project_root;
+    }
+
+    public function run($args = null)
     {
         $m = new Migrations;
-        $filename = $m->createMigration(IPF::get('project_root').'/project/db/migrations', implode('_', $args));
+        $filename = $m->createMigration($this->project_root . '/project/db/migrations', implode('_', $args));
         echo "Create new migration $filename\n";
     }
 }
index 0ea2d11ec801671051b0d4b5ccb54846a60a351e..5602784ccab22f4827e7b7b98a476d584c1ce4d1 100644 (file)
@@ -5,9 +5,17 @@ class IPF_Command_DB
     public $command = 'db';
     public $description = 'Database console';
 
+    /** @var array */
+    private $databaseConfig;
+
+    function __construct(array $databaseConfig)
+    {
+        $this->databaseConfig = $databaseConfig;
+    }
+
     public function run($args=null)
     {
-        $db = IPF::get('database');
+        $db = $this->databaseConfig;
 
         if ($db['driver'] === 'mysql') {
             IPF_Shell::call('mysql',
@@ -16,7 +24,7 @@ class IPF_Command_DB
                 '-p'.$db['password'],
                 $db['database']);
         } else {
-            print 'Do not know how to connect to "'.$db['scheme'].'" database.';
+            print "Do not know how to connect to \"{$db['type']}\" database.\n";
         }
     }
 }
index 69d3a69c3c99993b294e409239d3d724bcc417f7..ac9c5c96e59a66ae81f5449f8a9ad127f9888f9b 100644 (file)
@@ -5,15 +5,22 @@ class IPF_Command_DBDump
     public $command = 'dbdump';
     public $description = 'Dumps database to a file';
 
+    /** @var array */
+    private $databaseConfig;
+
+    function __construct(array $databaseConfig)
+    {
+        $this->databaseConfig = $databaseConfig;
+    }
+
     public function run($args=null)
     {
+        $db = $this->databaseConfig;
         $output = 'dump.sql';
 
         if (!in_array('--quiet', $args))
             print "Dumping database to file $output\n";
 
-        $db = IPF::get('database');
-
         if ($db['driver'] === 'mysql') {
             IPF_Shell::call('mysqldump',
                 '-h'.$db['host'],
@@ -22,8 +29,7 @@ class IPF_Command_DBDump
                 '-r'.$output,
                 $db['database']);
         } else {
-            print 'Do not know how to connect to "'.$db['scheme'].'" database.';
+            print "Do not know how to connect to \"{$db['driver']}\" database.\n";
         }
     }
 }
-
index ccc334991040ec6dc5f7c981a2a8a514df40f17e..6b382dc268017df53e553b835cd4248bdfd8545a 100644 (file)
@@ -5,15 +5,22 @@ class IPF_Command_DBRestore
     public $command = 'dbrestore';
     public $description = 'Restores database from a file';
 
+    /** @var array */
+    private $databaseConfig;
+
+    function __construct(array $databaseConfig)
+    {
+        $this->databaseConfig = $databaseConfig;
+    }
+
     public function run($args=null)
     {
+        $db = $this->databaseConfig;
         $input = 'dump.sql';
 
         if (!in_array('--quiet', $args))
             print "Restoring database from file $input\n";
 
-        $db = IPF::get('database');
-
         if ($db['driver'] === 'mysql') {
             IPF_Shell::call('mysql',
                 '-h'.$db['host'],
@@ -23,8 +30,7 @@ class IPF_Command_DBRestore
                 'source '.$input,
                 $db['database']);
         } else {
-            print 'Do not know how to connect to "'.$db['scheme'].'" database.';
+            print "Do not know how to connect to \"{$db['driver']}\" database.\n";
         }
     }
 }
-
index 2d3c103a9768a2e46d0a7a5087cd84bc11d0a9b8..17e26d6434616da2fa4090574a779bce0c11ea5d 100644 (file)
@@ -5,11 +5,18 @@ class IPF_Command_DebugServer
     public $command = 'run';
     public $description = 'Run debug server on 0.0.0.0:8000';
 
+    /** @var string */
+    private $document_root;
+
+    function __construct($document_root)
+    {
+        $this->document_root = $document_root;
+    }
+
     public function run($args=null)
     {
-        $root = IPF::get('document_root');
+        $document_root = $this->document_root;
         $router = dirname(__FILE__).'/../debug_router.php';
-        IPF_Shell::call('php', '-S', '0.0.0.0:8000', '-t', $root, $router);
+        IPF_Shell::call('php', '-S', '0.0.0.0:8000', '-t', $document_root, $router);
     }
 }
-
index edc060c52017fe03f138610dcd1a533d7d35223d..42d5af778421e426eeac4a33a25e5a0954cf86f4 100644 (file)
@@ -12,16 +12,19 @@ class IPF_Command_Migrate
     private $apps;
     /** @var Connection */
     private $connection;
+    /** @var string */
+    private $project_root;
 
-    function __construct(array $apps, Connection $connection)
+    function __construct(array $apps, Connection $connection, $project_root)
     {
         $this->apps = $apps;
         $this->connection = $connection;
+        $this->project_root = $project_root;
     }
 
     public function run($args=null)
     {
-        $paths = array(IPF::get('project_root').'/project/db/migrations');
+        $paths = array($this->project_root .'/project/db/migrations');
         foreach ($this->apps as $app) {
             $paths[] = $app->getPath() . 'migrations';
         }
index 39f7f10d9924fbc8ce1fb6afc510ff5ee575b87b..9e1af05dc56a3f5c075c310dbf8d4b9c474a332d 100644 (file)
@@ -5,21 +5,31 @@ class IPF_Command_Pack
     public $command = 'pack';
     public $description = 'Pack database dump and uploaded files to a single archive';
 
-    public function run($args=null)
+    /** @var string */
+    private $uploadsDir;
+    /** @var array */
+    private $databaseConfig;
+
+    function __construct($uploadsDir, array $databaseConfig)
+    {
+        $this->uploadsDir = $uploadsDir;
+        $this->databaseConfig = $databaseConfig;
+    }
+
+    public function run($args = null)
     {
         $outputFileName = 'working-data.tar';
 
-        $uploadsDir = IPF::get('document_root') . IPF::getUploadUrl();
-        if (is_dir($uploadsDir)) {
+        if (is_dir($this->uploadsDir)) {
             $workingDirectory = getcwd();
-            chdir($uploadsDir . '/..');
+            chdir($this->uploadsDir . '/..');
             $tar_object = new Archive_Tar($workingDirectory . '/upload.tar');
             $tar_object->setErrorHandling(PEAR_ERROR_PRINT);
             $tar_object->create(['upload']);
             chdir($workingDirectory);
         }
 
-        $dumpCommand = new IPF_Command_DBDump;
+        $dumpCommand = new IPF_Command_DBDump($this->databaseConfig);
         $dumpCommand->run(array('--quiet'));
 
         IPF_Shell::unlink($outputFileName);
@@ -32,4 +42,3 @@ class IPF_Command_Pack
         IPF_Shell::unlink('dump.sql');
     }
 }
-
index c92289178ac7ba6808bd1e540e74d86ac111a3f0..8e48e0c1aa6eaac40a9822e2aacc78aa6b59c5bb 100644 (file)
@@ -1,5 +1,7 @@
 <?php
 
+use Boris\Boris;
+
 class IPF_Command_Shell
 {
     public $command = 'shell';
@@ -7,8 +9,7 @@ class IPF_Command_Shell
 
     public function run($args=null)
     {
-        $boris = new \Boris\Boris('ipf> ');
+        $boris = new Boris('ipf> ');
         $boris->start();
     }
 }
-
index 639d7f061e24fc93991c2f7afcccd15b1851edb0..b2e55b744d05ab295aad9b852e2679928bc79a2a 100644 (file)
@@ -5,6 +5,17 @@ class IPF_Command_Unpack
     public $command = 'unpack';
     public $description = 'Unpack database dump and uploaded files from an archive';
 
+    /** @var string */
+    private $uploadsDir;
+    /** @var array */
+    private $databaseConfig;
+
+    function __construct($uploadsDir, array $databaseConfig)
+    {
+        $this->uploadsDir = $uploadsDir;
+        $this->databaseConfig = $databaseConfig;
+    }
+
     public function run($args=null)
     {
         $inputFileName = 'working-data.tar';
@@ -20,14 +31,12 @@ class IPF_Command_Unpack
         $archive = new Archive_Tar($inputFileName);
         $archive->extract('.');
 
-        $restoreCommand = new IPF_Command_DBRestore;
+        $restoreCommand = new IPF_Command_DBRestore($this->databaseConfig);
         $restoreCommand->run(array('--quiet'));
         IPF_Shell::unlink('dump.sql');
 
-        $uploadsDir = IPF::get('document_root') . IPF::getUploadUrl();
         $archive = new Archive_Tar('upload.tar');
-        $archive->extract($uploadsDir . '/..');
+        $archive->extract($this->uploadsDir . '/..');
         IPF_Shell::unlink('upload.tar');
     }
 }
-