Skip to content
Open

2.2 #14

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 45 additions & 14 deletions src/events/install.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,39 +94,66 @@

//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;
}

$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...');
continue;
}
}

//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('<DATABASE HOST>', $host, $contents);
$contents = str_replace('<DATABASE NAME>', $name, $contents);
$contents = str_replace('<DATABASE USER>', $user, $contents);
$contents = str_replace('<DATABASE PASS>', $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);
Expand Down Expand Up @@ -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);
}
Expand All @@ -180,7 +207,7 @@
}
}

if(!$request->hasStage('skip-sql')) {
if (!$request->hasStage('skip-sql')) {
//SQL
CommandLine::system('Setting up SQL...');

Expand Down Expand Up @@ -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
Expand All @@ -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']);
Expand All @@ -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');
}
};
3 changes: 2 additions & 1 deletion src/events/server.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
65 changes: 56 additions & 9 deletions src/events/update.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -64,19 +65,64 @@
$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);

//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;
}

Expand Down Expand Up @@ -119,23 +165,24 @@

//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()) {
CommandLine::error($payload['response']->getMessage(), false);
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);
};
17 changes: 17 additions & 0 deletions src/router.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php //-->

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';
})();
21 changes: 21 additions & 0 deletions src/template/config/deploy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php //-->
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' => '<SERVER IP>',
'repo' => 'git@github.com:<AUTHOR>/vendor.git',
'path' => '/path/to/public/on/live/server',
'ref' => 'origin/<BRANCH>'
],
'mysql' => [
'deploy' => false,
'user' => 'root',
'host' => '<SERVER IP>'
],
]
];
45 changes: 45 additions & 0 deletions src/template/config/packages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php //-->
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
]
];