From bed036a6f0221d32493bf13378ef553c868447c0 Mon Sep 17 00:00:00 2001 From: Christian Blanquera Date: Wed, 30 Jan 2019 15:35:39 +0800 Subject: [PATCH 01/12] Fixing cases for install and update. --- src/events/install.php | 9 ++--- src/events/update.php | 58 ++++++++++++++++++++++++++++++-- src/template/config/deploy.php | 21 ++++++++++++ src/template/config/packages.php | 42 +++++++++++++++++++++++ 4 files changed, 123 insertions(+), 7 deletions(-) create mode 100644 src/template/config/deploy.php create mode 100644 src/template/config/packages.php diff --git a/src/events/install.php b/src/events/install.php index 378840b..cfbcf9b 100644 --- a/src/events/install.php +++ b/src/events/install.php @@ -97,7 +97,6 @@ if(!$request->hasStage('skip-configs')) { CommandLine::system('Setting up config files...'); - $paths = scandir(__DIR__ . '/../template/config'); foreach($paths as $path) { if($path === '.' || $path === '..' || substr($path, -4) !== '.php') { @@ -241,7 +240,9 @@ $this->trigger('update', $request, $response); } - CommandLine::info('Recommended actions:'); - CommandLine::info(' - bin/cradle sql populate'); - CommandLine::info(' - yarn build'); + if (!$response->isError()) { + CommandLine::info('Recommended actions:'); + CommandLine::info(' - bin/cradle sql populate'); + CommandLine::info(' - yarn build'); + } }; diff --git a/src/events/update.php b/src/events/update.php index 8d9fc8a..601776f 100644 --- a/src/events/update.php +++ b/src/events/update.php @@ -41,6 +41,7 @@ //these are the installed packages $installed = $this->package('global')->config('packages'); + $hasErrors = false; foreach ($active as $name => $package) { $type = $package->getPackageType(); //skip pseudo packages @@ -64,6 +65,12 @@ $action = 'update'; } + if ($action === 'install') { + CommandLine::system(sprintf('Installing %s', $name)); + } else { + CommandLine::system(sprintf('Updating %s', $name)); + } + //trigger event $event = sprintf('%s-%s-%s', $vendor, $package, $action); $this->trigger($event, $request, $response); @@ -71,12 +78,51 @@ //if no event was triggered $status = $this->getEventHandler()->getMeta(); if($status === EventHandler::STATUS_NOT_FOUND) { + CommandLine::warning(sprintf('No actions needed on %s', $name)); continue; } + $logs = $response->getResults('logs'); + if (!empty($logs)) { + foreach ($logs as $package => $group) { + foreach ($group as $version => $messages) { + foreach ($messages as $log) { + CommandLine::$brand = '[' . $package . ' ' . $version . ']'; + if (!isset($log['message'])) { + continue; + } + + if (!isset($log['type'])) { + $log['type'] = 'info'; + } + + switch ($log['type']) { + case 'warning': + CommandLine::warning($log['message']); + break; + case 'error': + CommandLine::error($log['message'], false); + break; + case 'system': + case 'info': + default: + CommandLine::system($log['message']); + break; + } + } + } + } + + CommandLine::$brand = '[cradle]'; + $response->removeResults('logs'); + } + //if error if ($response->isError()) { - CommandLine::error($response->getMessage(), false); + $message = sprintf('%s did not correctly install.', $name); + CommandLine::error($message, false); + $response->remove('json'); + $hasErrors = true; continue; } @@ -132,10 +178,16 @@ continue; } - //it's update - $message = sprintf('Updated %s', $schema['name']); + //it's updated + $message = sprintf('Updated schema %s', $schema['name']); CommandLine::success($message, false); } + if ($hasErrors) { + $message = 'There were some errors in the packages being installed/updated.'; + CommandLine::error($message, false); + $response->setError(true, $message); + } + $response->setResults($schemas); }; diff --git a/src/template/config/deploy.php b/src/template/config/deploy.php new file mode 100644 index 0000000..028e443 --- /dev/null +++ b/src/template/config/deploy.php @@ -0,0 +1,21 @@ + +return [ + //see: https://gist.github.com/cblanquera/3ff60b4c9afc92be1ac0a9d57afceb17#file-instructions-md + 'key' => '/tmp/travis_rsa', + //see: https://github.com/visionmedia/deploy for config + 'servers' => [ + 'app' => [ + 'deploy' => false, + 'user' => 'root', + 'host' => '', + 'repo' => 'git@github.com:/vendor.git', + 'path' => '/path/to/public/on/live/server', + 'ref' => 'origin/' + ], + 'mysql' => [ + 'deploy' => false, + 'user' => 'root', + 'host' => '' + ], + ] +]; diff --git a/src/template/config/packages.php b/src/template/config/packages.php new file mode 100644 index 0000000..3f82632 --- /dev/null +++ b/src/template/config/packages.php @@ -0,0 +1,42 @@ + +return [ + 'cradlephp/cradle-developer' => [ + 'active' => true + ], + 'cradlephp/cradle-queue' => [ + 'active' => true + ], + 'cradlephp/cradle-captcha' => [ + 'active' => true + ], + 'cradlephp/cradle-csrf' => [ + 'active' => true + ], + 'cradlephp/cradle-system' => [ + 'active' => true + ], + 'cradlephp/cradle-install' => [ + 'active' => true + ], + 'cradlephp/cradle-profile' => [ + 'active' => true + ], + 'cradlephp/cradle-auth' => [ + 'active' => true + ], + 'cradlephp/cradle-history' => [ + 'active' => true + ], + 'cradlephp/cradle-role' => [ + 'active' => true + ], + 'cradlephp/cradle-api' => [ + 'active' => true + ], + 'cradlephp/cradle-admin' => [ + 'active' => true + ], + '/module/utility' => [ + 'active' => true + ] +]; From 0b8104eeeacda830951b29a9b15e3ad68f424fa2 Mon Sep 17 00:00:00 2001 From: Christian Blanquera Date: Wed, 30 Jan 2019 19:02:48 +0800 Subject: [PATCH 02/12] fixes https://github.com/CradlePHP/Cradle/issues/204 --- src/events/server.php | 3 ++- src/router.php | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 src/router.php diff --git a/src/events/server.php b/src/events/server.php index 79e45d6..9fd079d 100644 --- a/src/events/server.php +++ b/src/events/server.php @@ -38,5 +38,6 @@ CommandLine::info('Press Ctrl-C to quit.'); $cwd = getcwd(); - system('php -S ' . $host . ':' . $port . ' -t ' . $cwd . '/public'); + $router = dirname(__DIR__) . '/router.php'; + system('php -S ' . $host . ':' . $port . ' -t ' . $cwd . '/public ' . $router); }; diff --git a/src/router.php b/src/router.php new file mode 100644 index 0000000..f99d63e --- /dev/null +++ b/src/router.php @@ -0,0 +1,17 @@ + + +return (function() { + //are we in php server? + if (php_sapi_name() !== 'cli-server') { + return false; + } + + $root = $_SERVER['DOCUMENT_ROOT']; + $path = $_SERVER['REQUEST_URI']; + + if (file_exists($root . $path) && !is_dir($root . $path)) { + return false; + } + + return include $root . '/index.php'; +})(); From 813464f67cb7cb66bc038ad3a91793ae30dcc12c Mon Sep 17 00:00:00 2001 From: Brian Sy Date: Fri, 6 Sep 2019 16:23:05 +0800 Subject: [PATCH 03/12] added cradle-website --- src/template/config/packages.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/template/config/packages.php b/src/template/config/packages.php index 3f82632..8618eec 100644 --- a/src/template/config/packages.php +++ b/src/template/config/packages.php @@ -36,6 +36,9 @@ 'cradlephp/cradle-admin' => [ 'active' => true ], + 'cradlephp/cradle-website' => [ + 'active' => true + ], '/module/utility' => [ 'active' => true ] From 703ca87cae59a9c3f6b0d742372f86d49a8b05ed Mon Sep 17 00:00:00 2001 From: brianpesy Date: Thu, 26 Sep 2019 11:27:20 +0800 Subject: [PATCH 04/12] Fixed bug that required two installs --- src/events/install.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/events/install.php b/src/events/install.php index cfbcf9b..4852569 100644 --- a/src/events/install.php +++ b/src/events/install.php @@ -121,6 +121,12 @@ $contents = str_replace('', $pass, $contents); file_put_contents($destination, $contents); + + $config = include $destination; + + $this->package('global')->service(null); + $this->package('global')->config(basename($path, '.php'), $config); + } } From 915884ddd8d59bf724a420ad0cf2a1fecac0d552 Mon Sep 17 00:00:00 2001 From: Paul Valderrama Date: Tue, 12 Nov 2019 10:52:50 +0800 Subject: [PATCH 05/12] Fix [BUG] Install doesn't use the sample config files Signed-off-by: Paul Valderrama --- src/events/install.php | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/src/events/install.php b/src/events/install.php index 4852569..014cb29 100644 --- a/src/events/install.php +++ b/src/events/install.php @@ -106,7 +106,7 @@ $source = __DIR__ . '/../template/config/' . $path; $destination = $cwd . '/config/' . $path; - if (file_exists($destination) && !$force) { + if (file_exists($destination) && !$force) { $answer = CommandLine::input('Overwrite config/' . $path . '?(y)', 'y'); if ($answer !== 'y') { CommandLine::system('Skipping...'); @@ -114,6 +114,29 @@ } } + //check for sample files + $fileName = trim($path, '.php'); + $sampleFile = $fileName . '.sample.php'; + $sampleFilePath = $cwd . '/config/' . $sampleFile; + + if (file_exists($sampleFilePath) && !$force) { + CommandLine::system('Sample file found for '. $path); + $answer = CommandLine::input('Use sample file for ' . $path . '?(y)', 'y'); + if ($answer !== 'y') { + CommandLine::system('Skipping...'); + continue; + } + + $contents = file_get_contents($sampleFilePath); + $contents = str_replace('', $host, $contents); + $contents = str_replace('', $name, $contents); + $contents = str_replace('', $user, $contents); + $contents = str_replace('', $pass, $contents); + + file_put_contents($destination, $contents); + continue; + } + $contents = file_get_contents($source); $contents = str_replace('', $host, $contents); $contents = str_replace('', $name, $contents); @@ -121,12 +144,6 @@ $contents = str_replace('', $pass, $contents); file_put_contents($destination, $contents); - - $config = include $destination; - - $this->package('global')->service(null); - $this->package('global')->config(basename($path, '.php'), $config); - } } @@ -246,9 +263,7 @@ $this->trigger('update', $request, $response); } - if (!$response->isError()) { - CommandLine::info('Recommended actions:'); - CommandLine::info(' - bin/cradle sql populate'); - CommandLine::info(' - yarn build'); - } + CommandLine::info('Recommended actions:'); + CommandLine::info(' - bin/cradle sql populate'); + CommandLine::info(' - yarn build'); }; From 6e82b66fa569d08345cc0c6bd972e046eae3d62e Mon Sep 17 00:00:00 2001 From: Paul Valderrama Date: Tue, 12 Nov 2019 11:02:17 +0800 Subject: [PATCH 06/12] Clean code Signed-off-by: Paul Valderrama --- src/events/install.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/events/install.php b/src/events/install.php index 014cb29..b6b606a 100644 --- a/src/events/install.php +++ b/src/events/install.php @@ -134,6 +134,10 @@ $contents = str_replace('', $pass, $contents); file_put_contents($destination, $contents); + $config = include $destination; + + $this->package('global')->service(null); + $this->package('global')->config(basename($path, '.php'), $config); continue; } @@ -144,6 +148,11 @@ $contents = str_replace('', $pass, $contents); file_put_contents($destination, $contents); + + $config = include $destination; + + $this->package('global')->service(null); + $this->package('global')->config(basename($path, '.php'), $config); } } From cd6daa3230f110e44fe31a6b16e4bb878afd4930 Mon Sep 17 00:00:00 2001 From: Paul Valderrama Date: Tue, 12 Nov 2019 11:03:33 +0800 Subject: [PATCH 07/12] Add Check for errors Signed-off-by: Paul Valderrama --- src/events/install.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/events/install.php b/src/events/install.php index b6b606a..1ffaf0f 100644 --- a/src/events/install.php +++ b/src/events/install.php @@ -272,7 +272,9 @@ $this->trigger('update', $request, $response); } - CommandLine::info('Recommended actions:'); - CommandLine::info(' - bin/cradle sql populate'); - CommandLine::info(' - yarn build'); + if (!$response->isError()) { + CommandLine::info('Recommended actions:'); + CommandLine::info(' - bin/cradle sql populate'); + CommandLine::info(' - yarn build'); + } }; From aa0b88ae4265b9c4f87f64c6df5a16482391bae9 Mon Sep 17 00:00:00 2001 From: Paul Valderrama Date: Tue, 12 Nov 2019 12:21:09 +0800 Subject: [PATCH 08/12] Remove spaces Signed-off-by: Paul Valderrama --- src/events/install.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/events/install.php b/src/events/install.php index 1ffaf0f..aae47ce 100644 --- a/src/events/install.php +++ b/src/events/install.php @@ -106,7 +106,7 @@ $source = __DIR__ . '/../template/config/' . $path; $destination = $cwd . '/config/' . $path; - if (file_exists($destination) && !$force) { + if (file_exists($destination) && !$force) { $answer = CommandLine::input('Overwrite config/' . $path . '?(y)', 'y'); if ($answer !== 'y') { CommandLine::system('Skipping...'); @@ -151,7 +151,7 @@ $config = include $destination; - $this->package('global')->service(null); + $this->package('global')->service(null); $this->package('global')->config(basename($path, '.php'), $config); } } From b5cf954498c6cb9c0a346e9ee43311d359adc8aa Mon Sep 17 00:00:00 2001 From: Christian Blanquera Date: Wed, 20 Nov 2019 18:02:53 +0800 Subject: [PATCH 09/12] using basename instead of trim, check the php docs on trim --- src/events/install.php | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/src/events/install.php b/src/events/install.php index aae47ce..c727464 100644 --- a/src/events/install.php +++ b/src/events/install.php @@ -115,30 +115,27 @@ } //check for sample files - $fileName = trim($path, '.php'); + $fileName = basename($path, '.php'); $sampleFile = $fileName . '.sample.php'; $sampleFilePath = $cwd . '/config/' . $sampleFile; if (file_exists($sampleFilePath) && !$force) { CommandLine::system('Sample file found for '. $path); $answer = CommandLine::input('Use sample file for ' . $path . '?(y)', 'y'); - if ($answer !== 'y') { - CommandLine::system('Skipping...'); + if ($answer === 'y') { + $contents = file_get_contents($sampleFilePath); + $contents = str_replace('', $host, $contents); + $contents = str_replace('', $name, $contents); + $contents = str_replace('', $user, $contents); + $contents = str_replace('', $pass, $contents); + + file_put_contents($destination, $contents); + $config = include $destination; + + $this->package('global')->service(null); + $this->package('global')->config(basename($path, '.php'), $config); continue; } - - $contents = file_get_contents($sampleFilePath); - $contents = str_replace('', $host, $contents); - $contents = str_replace('', $name, $contents); - $contents = str_replace('', $user, $contents); - $contents = str_replace('', $pass, $contents); - - file_put_contents($destination, $contents); - $config = include $destination; - - $this->package('global')->service(null); - $this->package('global')->config(basename($path, '.php'), $config); - continue; } $contents = file_get_contents($source); From 3fd05abde2052e54a9c1a32fcc8674fa3f4ef12a Mon Sep 17 00:00:00 2001 From: Christian Blanquera Date: Wed, 20 Nov 2019 21:42:44 +0800 Subject: [PATCH 10/12] when installing, now retains package versions unless you add --reinstall-packages flag --- src/events/install.php | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/events/install.php b/src/events/install.php index c727464..d69a707 100644 --- a/src/events/install.php +++ b/src/events/install.php @@ -94,11 +94,11 @@ //setup the configs $cwd = getcwd(); - if(!$request->hasStage('skip-configs')) { + if (!$request->hasStage('skip-configs')) { CommandLine::system('Setting up config files...'); $paths = scandir(__DIR__ . '/../template/config'); - foreach($paths as $path) { + foreach ($paths as $path) { if($path === '.' || $path === '..' || substr($path, -4) !== '.php') { continue; } @@ -154,7 +154,7 @@ } //create compiled, log, public/upload, config/schema - if(!$request->hasStage('skip-mkdir')) { + if (!$request->hasStage('skip-mkdir')) { if (!is_dir($cwd . '/compiled')) { CommandLine::system('Making ' . $cwd . '/compiled'); mkdir($cwd . '/compiled', 0777); @@ -182,12 +182,12 @@ } //chmod compiled, log, config, public/upload - if(!$request->hasStage('skip-chmod')) { + if (!$request->hasStage('skip-chmod')) { // special case for config folder $configDirectories = glob($cwd . '/config/*', GLOB_ONLYDIR); // map each directories - foreach($configDirectories as $directory) { + foreach ($configDirectories as $directory) { CommandLine::system('chmoding ' . $directory); chmod($directory, 0777); } @@ -208,7 +208,7 @@ } } - if(!$request->hasStage('skip-sql')) { + if (!$request->hasStage('skip-sql')) { //SQL CommandLine::system('Setting up SQL...'); @@ -239,7 +239,7 @@ } } - if(!$request->hasStage('skip-versioning')) { + if ($request->hasStage('reinstall-packages')) { // copy the default packages if it doesn't exists if (!$this->package('global')->config('packages')) { // get sample package config @@ -254,7 +254,7 @@ $packages = $this->package('global')->config('packages'); // on each packages - foreach($packages as $package => $config) { + foreach ($packages as $package => $config) { // reset the version so we can re-install again if (isset($config['version'])) { unset($packages[$package]['version']); @@ -264,8 +264,10 @@ // update the config $this->package('global')->config('packages', $packages); } + } - //now run the update + if (!$request->hasStage('skip-versioning')) { + //run the update $this->trigger('update', $request, $response); } From 0fd8550c707d4ad8efdd2c703098070b59a8926b Mon Sep 17 00:00:00 2001 From: Christian Blanquera Date: Tue, 26 May 2020 14:16:11 +0800 Subject: [PATCH 11/12] improvements on using sample configs and force flag --- src/events/install.php | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/src/events/install.php b/src/events/install.php index d69a707..562b11f 100644 --- a/src/events/install.php +++ b/src/events/install.php @@ -106,7 +106,12 @@ $source = __DIR__ . '/../template/config/' . $path; $destination = $cwd . '/config/' . $path; - if (file_exists($destination) && !$force) { + if (file_exists($destination)) { + //by default dont override existing configs + if ($force) { + continue; + } + $answer = CommandLine::input('Overwrite config/' . $path . '?(y)', 'y'); if ($answer !== 'y') { CommandLine::system('Skipping...'); @@ -119,22 +124,16 @@ $sampleFile = $fileName . '.sample.php'; $sampleFilePath = $cwd . '/config/' . $sampleFile; - if (file_exists($sampleFilePath) && !$force) { + if (file_exists($sampleFilePath)) { CommandLine::system('Sample file found for '. $path); - $answer = CommandLine::input('Use sample file for ' . $path . '?(y)', 'y'); - if ($answer === 'y') { - $contents = file_get_contents($sampleFilePath); - $contents = str_replace('', $host, $contents); - $contents = str_replace('', $name, $contents); - $contents = str_replace('', $user, $contents); - $contents = str_replace('', $pass, $contents); - - file_put_contents($destination, $contents); - $config = include $destination; - - $this->package('global')->service(null); - $this->package('global')->config(basename($path, '.php'), $config); - continue; + //by default use sample configs + if ($force) { + $source = $sampleFilePath; + } else { + $answer = CommandLine::input('Use sample file for ' . $path . '?(y)', 'y'); + if ($answer === 'y') { + $source = $sampleFilePath; + } } } From a67583e6ed2edfd4f5b10ec0319a834ee06bf757 Mon Sep 17 00:00:00 2001 From: Christian Blanquera Date: Thu, 28 May 2020 16:54:19 +0800 Subject: [PATCH 12/12] fix for installer adds flags to schemas --- src/events/update.php | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/events/update.php b/src/events/update.php index 601776f..7605a29 100644 --- a/src/events/update.php +++ b/src/events/update.php @@ -165,12 +165,7 @@ //run an update $payload = $this->makePayload(); - $payload['request']->setStage($schema); - $this->trigger( - 'system-schema-update', - $payload['request'], - $payload['response'] - ); + $this->method('system-schema-update', $schema, $payload['response']); //if error if ($payload['response']->isError()) {