diff --git a/src/events/install.php b/src/events/install.php index 378840b..562b11f 100644 --- a/src/events/install.php +++ b/src/events/install.php @@ -94,12 +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; } @@ -107,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...'); @@ -115,6 +119,24 @@ } } + //check for sample files + $fileName = basename($path, '.php'); + $sampleFile = $fileName . '.sample.php'; + $sampleFilePath = $cwd . '/config/' . $sampleFile; + + if (file_exists($sampleFilePath)) { + CommandLine::system('Sample file found for '. $path); + //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; + } + } + } + $contents = file_get_contents($source); $contents = str_replace('', $host, $contents); $contents = str_replace('', $name, $contents); @@ -122,11 +144,16 @@ $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); } } //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); @@ -154,12 +181,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); } @@ -180,7 +207,7 @@ } } - if(!$request->hasStage('skip-sql')) { + if (!$request->hasStage('skip-sql')) { //SQL CommandLine::system('Setting up SQL...'); @@ -211,7 +238,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 @@ -226,7 +253,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']); @@ -236,12 +263,16 @@ // 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); } - 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/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/events/update.php b/src/events/update.php index 8d9fc8a..7605a29 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; } @@ -119,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()) { @@ -132,10 +173,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/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'; +})(); 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..8618eec --- /dev/null +++ b/src/template/config/packages.php @@ -0,0 +1,45 @@ + +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 + ], + 'cradlephp/cradle-website' => [ + 'active' => true + ], + '/module/utility' => [ + 'active' => true + ] +];