From d40c189ba4b8da8467a2ecb5459e5482e23657a4 Mon Sep 17 00:00:00 2001 From: Andrey Kutejko Date: Wed, 20 Mar 2019 22:15:12 +0100 Subject: [PATCH] parametrize commands --- composer.json | 4 ++-- composer.lock | 2 +- ipf/cli.php | 25 ++++++++++++++++--------- ipf/command/collectstatic.php | 11 ++++++----- ipf/command/createmigration.php | 12 ++++++++++-- ipf/command/db.php | 12 ++++++++++-- ipf/command/dbdump.php | 14 ++++++++++---- ipf/command/dbrestore.php | 14 ++++++++++---- ipf/command/debugserver.php | 13 ++++++++++--- ipf/command/migrate.php | 7 +++++-- ipf/command/pack.php | 21 +++++++++++++++------ ipf/command/shell.php | 5 +++-- ipf/command/unpack.php | 17 +++++++++++++---- 13 files changed, 111 insertions(+), 46 deletions(-) diff --git a/composer.json b/composer.json index c8b71eb..32330a9 100644 --- a/composer.json +++ b/composer.json @@ -17,10 +17,10 @@ }, "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" }, diff --git a/composer.lock b/composer.lock index 8666e4a..18902b1 100644 --- a/composer.lock +++ b/composer.lock @@ -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", diff --git a/ipf/cli.php b/ipf/cli.php index f935246..a3ecec6 100644 --- a/ipf/cli.php +++ b/ipf/cli.php @@ -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), ); }; diff --git a/ipf/command/collectstatic.php b/ipf/command/collectstatic.php index 8c3926b..2e3d46d 100644 --- a/ipf/command/collectstatic.php +++ b/ipf/command/collectstatic.php @@ -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); diff --git a/ipf/command/createmigration.php b/ipf/command/createmigration.php index b2c586b..83ef0c4 100644 --- a/ipf/command/createmigration.php +++ b/ipf/command/createmigration.php @@ -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"; } } diff --git a/ipf/command/db.php b/ipf/command/db.php index 0ea2d11..5602784 100644 --- a/ipf/command/db.php +++ b/ipf/command/db.php @@ -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"; } } } diff --git a/ipf/command/dbdump.php b/ipf/command/dbdump.php index 69d3a69..ac9c5c9 100644 --- a/ipf/command/dbdump.php +++ b/ipf/command/dbdump.php @@ -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"; } } } - diff --git a/ipf/command/dbrestore.php b/ipf/command/dbrestore.php index ccc3349..6b382dc 100644 --- a/ipf/command/dbrestore.php +++ b/ipf/command/dbrestore.php @@ -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"; } } } - diff --git a/ipf/command/debugserver.php b/ipf/command/debugserver.php index 2d3c103..17e26d6 100644 --- a/ipf/command/debugserver.php +++ b/ipf/command/debugserver.php @@ -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); } } - diff --git a/ipf/command/migrate.php b/ipf/command/migrate.php index edc060c..42d5af7 100644 --- a/ipf/command/migrate.php +++ b/ipf/command/migrate.php @@ -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'; } diff --git a/ipf/command/pack.php b/ipf/command/pack.php index 39f7f10..9e1af05 100644 --- a/ipf/command/pack.php +++ b/ipf/command/pack.php @@ -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'); } } - diff --git a/ipf/command/shell.php b/ipf/command/shell.php index c922891..8e48e0c 100644 --- a/ipf/command/shell.php +++ b/ipf/command/shell.php @@ -1,5 +1,7 @@ '); + $boris = new Boris('ipf> '); $boris->start(); } } - diff --git a/ipf/command/unpack.php b/ipf/command/unpack.php index 639d7f0..b2e55b7 100644 --- a/ipf/command/unpack.php +++ b/ipf/command/unpack.php @@ -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'); } } - -- 2.49.0