From 0ff33517a80f5620f5544ab2002be0c0846a0518 Mon Sep 17 00:00:00 2001 From: Andrii Vasyliev Date: Tue, 12 Jan 2016 12:27:54 +0000 Subject: [PATCH] redoing goals to controllers BROKEN init & dump look working --- bin/hidev | 3 +- src/base/Application.php | 80 +++++-- .../SvnignoreGoal.php => base/Controller.php} | 10 +- src/base/config.php | 22 +- src/components/Config.php | 67 +++--- .../AliasesController.php} | 4 +- src/controllers/ChangelogController.php | 22 ++ .../CommitsController.php} | 17 +- src/controllers/CommonController.php | 225 ++++++++++++++++++ .../DumpController.php} | 6 +- .../FileController.php} | 15 +- .../GenerateController.php} | 4 +- .../GitController.php} | 15 +- .../GitHubController.php} | 6 +- src/controllers/GitignoreController.php | 20 ++ .../InitController.php} | 27 ++- .../InstallController.php} | 4 +- .../PackageController.php} | 4 +- src/controllers/StartController.php | 85 +++++++ .../SvnignoreController.php} | 8 +- .../TemplateController.php} | 7 +- .../UpdateController.php} | 11 +- .../VcsController.php} | 6 +- .../VcsignoreController.php} | 6 +- .../VendorController.php} | 4 +- src/goals/BaseGoal.php | 22 -- src/goals/ChangelogGoal.php | 25 -- src/goals/DefaultGoal.php | 204 ---------------- src/goals/StartGoal.php | 71 ------ src/handlers/BaseHandler.php | 3 +- 30 files changed, 530 insertions(+), 473 deletions(-) rename src/{goals/SvnignoreGoal.php => base/Controller.php} (65%) rename src/{goals/AliasesGoal.php => controllers/AliasesController.php} (83%) create mode 100644 src/controllers/ChangelogController.php rename src/{goals/CommitsGoal.php => controllers/CommitsController.php} (56%) create mode 100644 src/controllers/CommonController.php rename src/{goals/DumpGoal.php => controllers/DumpController.php} (81%) rename src/{goals/FileGoal.php => controllers/FileController.php} (89%) rename src/{goals/GenerateGoal.php => controllers/GenerateController.php} (93%) rename src/{goals/GitGoal.php => controllers/GitController.php} (92%) rename src/{goals/GitHubGoal.php => controllers/GitHubController.php} (92%) create mode 100644 src/controllers/GitignoreController.php rename src/{goals/InitGoal.php => controllers/InitController.php} (80%) rename src/{goals/InstallGoal.php => controllers/InstallController.php} (82%) rename src/{goals/PackageGoal.php => controllers/PackageController.php} (97%) create mode 100644 src/controllers/StartController.php rename src/{goals/GitignoreGoal.php => controllers/SvnignoreController.php} (64%) rename src/{goals/TemplateGoal.php => controllers/TemplateController.php} (64%) rename src/{goals/UpdateGoal.php => controllers/UpdateController.php} (73%) rename src/{goals/VcsGoal.php => controllers/VcsController.php} (85%) rename src/{goals/VcsignoreGoal.php => controllers/VcsignoreController.php} (90%) rename src/{goals/VendorGoal.php => controllers/VendorController.php} (91%) delete mode 100644 src/goals/BaseGoal.php delete mode 100644 src/goals/ChangelogGoal.php delete mode 100644 src/goals/DefaultGoal.php delete mode 100644 src/goals/StartGoal.php diff --git a/bin/hidev b/bin/hidev index 11abb89..73e2229 100755 --- a/bin/hidev +++ b/bin/hidev @@ -21,4 +21,5 @@ defined('STDOUT') or define('STDOUT', fopen('php://stdout', 'w')); require_once(__DIR__ . '/../_bootstrap.php'); -exit(hidev\base\Application::main()); +exit(hidev\base\Application::main(require __DIR__ . '/../src/base/config.php')); + diff --git a/src/base/Application.php b/src/base/Application.php index 935ec5b..eef4760 100644 --- a/src/base/Application.php +++ b/src/base/Application.php @@ -29,19 +29,27 @@ class Application extends \yii\console\Application implements ViewContextInterfa protected $_viewPath; - public static function main() + protected $_config; + + public function __construct($config = []) + { + $this->_config = $config; + parent::__construct($config); + } + + /** + * Creates application with given config and runs it. + * @param array $config + * @return int exit code + */ + public static function main(array $config) { try { Yii::setLogger(Yii::createObject('hidev\base\Logger')); $config = ArrayHelper::merge( - require dirname(dirname(__DIR__)) . '/.hidev/vendor/yiisoft/yii2-extraconfig.php', - require dirname(dirname(__DIR__)) . '/vendor/yiisoft/yii2-extraconfig.php', - require __DIR__ . '/config.php' + static::readExtraVendor($config['vendorPath']), + $config ); - # var_dump($config); die(); - foreach ($config['aliases'] as $name => $alias) { - Yii::setAlias($name, $alias); - } $exitCode = (new static($config))->run(); } catch (Exception $e) { if ($e instanceof InvalidParamException || $e instanceof ConsoleException) { @@ -55,6 +63,50 @@ public static function main() return $exitCode; } + public static function readExtraVendor($dir) + { + return require $dir . '/yiisoft/yii2-extraconfig.php'; + } + + /** + * Load extra config files. + * @param array $config + * @return void + */ + public function loadExtraVendor($dir) + { + $this->setExtraConfig(static::readExtraVendor($dir)); + } + + /** + * Implements extra configuration. + * @param array $config + * @return void + */ + public function setExtraConfig($config) + { + $this->_config = $config = ArrayHelper::merge($config, $this->_config); + + if (!empty($config['aliases'])) { + $this->setAliases($config['aliases']); + } + if (!empty($config['modules'])) { + $this->setModules($config['modules']); + /*$this->setModules(ArrayHelper::merge( + $config['modules'], + ArrayHelper::getItems($this->modules, array_keys($config['modules'])) + ));*/ + } + if (!empty($config['components'])) { + foreach ($config['components'] as $id => $component) { + if ($this->has($id, true)) { + unset($config['components'][$id]); + } + } + $this->setComponents($config['components']); + } + } + /*public function getViewPath() { if ($this->_viewPath === null) { @@ -66,14 +118,14 @@ public static function main() public function createControllerByID($id) { - if (!$this->get('config')->hasGoal($id)) { - var_dump("CANT RUN GOAL: $id"); - #var_dump(ArrayHelper::toArray($this->get('config'))); - throw new InvalidConfigException("can't run goal '$id'"); + if ($this->get('config')->hasGoal($id)) { + return $this->get('config')->get($id); } - #var_dump( $this->get('config')->get($id) ); - return $this->get('config')->get($id); + $controller = parent::createControllerByID($id); + $this->get('config')->set($id, $controller); + + return $controller; } public function runRequest($string) diff --git a/src/goals/SvnignoreGoal.php b/src/base/Controller.php similarity index 65% rename from src/goals/SvnignoreGoal.php rename to src/base/Controller.php index 98c3b89..794d8d6 100644 --- a/src/goals/SvnignoreGoal.php +++ b/src/base/Controller.php @@ -9,12 +9,14 @@ * @copyright Copyright (c) 2014-2016, HiQDev (http://hiqdev.com/) */ -namespace hidev\goals; +namespace hidev\base; + +use Yii; /** - * Goal for svn:ignore. + * Basic controller. */ -class SvnignoreGoal extends VcsignoreGoal +class Controller extends \yii\console\Controller { - protected $_fileType = 'svnignore'; + public $defaultAction = 'perform'; } diff --git a/src/base/config.php b/src/base/config.php index 20c8821..a18654b 100644 --- a/src/base/config.php +++ b/src/base/config.php @@ -3,8 +3,8 @@ return [ 'id' => 'hidev', 'name' => 'HiDev', - 'basePath' => '@hidev', - 'vendorPath' => '@vendor', + 'basePath' => dirname(__DIR__), + 'vendorPath' => dirname(dirname(__DIR__)) . '/vendor', 'runtimePath' => dirname(substr(__DIR__, 0, 7) === 'phar://' ? $_SERVER['SCRIPT_NAME'] : dirname(__DIR__)) . '/runtime', 'enableCoreCommands' => false, 'controllerNamespace' => 'hidev\\controllers', @@ -22,26 +22,14 @@ 'cache' => [ 'class' => 'yii\caching\FileCache', ], - 'request' => [ + /*'request' => [ 'class' => 'hidev\base\Request', - ], + ],*/ 'binaries' => [ 'class' => 'hidev\components\Binaries', ], 'config' => [ - 'class' => 'hidev\components\Config', - 'git' => 'hidev\goals\GitGoal', - 'init' => 'hidev\goals\InitGoal', - 'start' => 'hidev\goals\StartGoal', - 'github' => 'hidev\goals\GitHubGoal', - 'update' => 'hidev\goals\UpdateGoal', - 'vendor' => 'hidev\goals\VendorGoal', - 'commits' => 'hidev\goals\CommitsGoal', - 'install' => 'hidev\goals\InstallGoal', - 'package' => 'hidev\goals\PackageGoal', - '.gitignore' => 'hidev\goals\GitignoreGoal', - 'vcsignore' => 'hidev\goals\VcsignoreGoal', - 'CHANGELOG.md' => 'hidev\goals\ChangelogGoal', + 'class' => 'hidev\components\Config', ], 'view' => [ 'class' => 'hidev\base\View', diff --git a/src/components/Config.php b/src/components/Config.php index 435779d..96910fe 100644 --- a/src/components/Config.php +++ b/src/components/Config.php @@ -11,6 +11,7 @@ namespace hidev\components; +use hidev\base\File; use hidev\helpers\Helper; use Yii; use yii\base\BootstrapInterface; @@ -24,73 +25,59 @@ class Config extends \hiqdev\yii2\collection\Object { public $file = '.hidev/config.yml'; - public function hasGoal($name) + protected $_included = []; + + public function hasGoal($id) { - return $this->hasItem($name); + return $this->hasItem($id); } - /*public function findGoal($name) + /*public function findGoal($id) { - $config = $this->getGoals()->get($name); + $config = $this->getGoals()->get($id); return is_scalar($config) ? ['class' => $config] : (array) $config; }*/ - public function getItemConfig($name = null, array $config = []) + public function getItemConfig($id = null, array $config = []) { + var_dump($config); die('getItemConfig'); return ArrayHelper::merge([ - 'goalName' => $name, - 'class' => 'hidev\goals\DefaultGoal', + 'class' => 'hidev\controllers\CommonController', ], $config); } - protected function createItem($name, $config = []) + protected function createItem($id, $config = []) { - $config = is_scalar($config) ? ['class' => $config] : (array) $config; - return Yii::createObject($this->getItemConfig($name, $config), [$name, Yii::$app]); + return Yii::createObject($this->getItemConfig($id, $config), [$id, $this->module]); } - public function getItem($name) + public function getItem($id) { - if ($name === 'default') { - return $this; - } - $item = &$this->_items[$name]; + $item = &$this->_items[$id]; #var_dump($item); #if (is_array($item) || is_null($item)) { - if (!is_object($item)) { - $item = $this->createItem($name, $item ?: []); + if (is_array($item)) { + $item = $this->createItem($id, $item); } return $item; } /** - * Loads all the configs. Reads or creates if doesn't exist. - * @void + * Include config file, unique only. + * @param string|array $path + * @return bool true if the path was unique and loaded */ - public function loadAllConfigs() + public function includeConfig($path) { - if (!file_exists($this->file)) { - throw new InvalidParamException('No config found. Use hidev init vendor/package'); - } - if (Yii::$app->get('configs')) { - foreach (Yii::$app->get as $path) { - $this->includeConfig($path); - } + $file = File::create($path); + $path = $file->getPath(); + if (!isset($this->_included[$path])) { + $this->_included[$path] = $path; + $this->setItems($file->load()); + return true; } - $this->includeConfig($this->file); - if ($this->has('include')) { - foreach (Helper::csplit($this->rawItem('include')) as $path) { - $this->includeConfig($path); - } - } - } - public function includeConfig($path) - { - $file = Yii::createObject(array_merge([ - 'class' => 'hidev\base\File', - ], is_array($path) ? $path : compact('path'))); - $this->setItems($file->load()); + return false; } } diff --git a/src/goals/AliasesGoal.php b/src/controllers/AliasesController.php similarity index 83% rename from src/goals/AliasesGoal.php rename to src/controllers/AliasesController.php index 48831f6..2be62df 100644 --- a/src/goals/AliasesGoal.php +++ b/src/controllers/AliasesController.php @@ -9,11 +9,11 @@ * @copyright Copyright (c) 2014-2016, HiQDev (http://hiqdev.com/) */ -namespace hidev\goals; +namespace hidev\controllers; use hidev\helpers\Helper; -class AliasesGoal extends DefaultGoal +class AliasesController extends CommonController { public function getItem($name) { diff --git a/src/controllers/ChangelogController.php b/src/controllers/ChangelogController.php new file mode 100644 index 0000000..a41c617 --- /dev/null +++ b/src/controllers/ChangelogController.php @@ -0,0 +1,22 @@ +setDeps($this->getVcs()->goalName); - } - protected $_file = '.hidev/commits.md'; - protected $_fileType = 'commits'; + public $fileType = 'commits'; public function getHistory() { diff --git a/src/controllers/CommonController.php b/src/controllers/CommonController.php new file mode 100644 index 0000000..3d75235 --- /dev/null +++ b/src/controllers/CommonController.php @@ -0,0 +1,225 @@ +id'"); + return $this->runActions(['before', 'make', 'after']); + } + + public function actionBefore() + { + return $this->runRequests($this->getBefore()); + } + + public function actionMake() + { + return $this->runActions($this->getMake()); + } + + public function actionAfter() + { + return $this->runRequests($this->getAfter()); + } + + public function actionLoad() + { + Yii::trace("Loading nothing for '$this->id'"); + } + + public function actionSave() + { + Yii::trace("Saving nothing for '$this->id'"); + } + + public function setBefore($requests) + { + $this->_before = array_merge($this->getBefore(), $this->normalizeTasks($requests)); + } + + public function getBefore() + { + return $this->_before; + } + + public function setMake($requests) + { + $this->_make = array_merge($this->getMake(), $this->normalizeTasks($requests)); + } + + public function getMake() + { + return $this->_make; + } + + public function setAfter($requests) + { + $this->_after = array_merge($this->getAfter(), $this->normalizeTasks($requests)); + } + + public function getAfter() + { + return $this->_after; + } + + public function normalizeTasks($tasks) + { + if (!$tasks) { + return []; + } elseif (!is_array($tasks)) { + $tasks = [(string)$tasks => 1]; + } + $res = []; + foreach ($tasks as $dep => $enabled) { + $res[(string)(is_int($dep) ? $enabled : $dep)] = (bool)(is_int($dep) ? 1 : $enabled); + } + + return $res; + } + + /** + * Runs array of requests. Stops on failure and returns exit code. + * @param null|string|array $requests + * @return int|Response exit code + */ + public function runRequests($requests) + { + foreach ($this->normalizeTasks($requests) as $request => $enabled) { + if ($enabled) { + $res = $this->runRequest($request); + if (static::isNotOk($res)) { + return $res; + } + } + } + + return 0; + } + + public function runRequest($request) + { + return $request === null ? null : $this->module->runRequest($request); + } + + public static function isNotOk($res) + { + return is_object($res) ? $res->exitStatus : $res; + } + + /** + * Runs list of actions. + * TODO: think to redo with runRequests + * @param null|string|array $actions + * @return int|Response exit code + */ + public function runActions($actions) + { + foreach ($this->normalizeTasks($actions) as $action => $enabled) { + if ($enabled) { + $res = $this->runAction($action); + if (static::isNotOk($res)) { + return $res; + } + } + } + + return 0; + } + + public function runAction($id, $params = []) + { + if ($this->isDone($id)) { + return; + } + $result = parent::runAction($id, $params); + $this->markDone($id); + + return $result; + } + + public function isDone($action, $timestamp = null) + { + if ($this->_done[$action]) { + Yii::trace("Already done: '$this->id/$action'"); + + return true; + } + + return false; + } + + /** + * Mark action as already done. + * + * @param string $action action id + * @param int $time microtime when action was done, false for action was not done + */ + public function markDone($action, $time = null) + { + $this->_done[$action] = ($time === null || $time === true) ? microtime(1) : $time; + } + + /** + * Runs given binary with given arguments. + * @param string $name + * @param string $args + * @return int exit code + */ + public function passthru($name, $args = '') + { + return Yii::$app->get('binaries')->passthru($name, $args); + } + + public function getConfig() + { + return Yii::$app->get('config'); + } + + public function getGoal($id) + { + return Yii::$app->createControllerById($id); + } + + public function getVcs() + { + /// TODO determine VCS + return $this->getGoal('git'); + } +} diff --git a/src/goals/DumpGoal.php b/src/controllers/DumpController.php similarity index 81% rename from src/goals/DumpGoal.php rename to src/controllers/DumpController.php index df63fcc..9b75dbe 100644 --- a/src/goals/DumpGoal.php +++ b/src/controllers/DumpController.php @@ -9,7 +9,7 @@ * @copyright Copyright (c) 2014-2016, HiQDev (http://hiqdev.com/) */ -namespace hidev\goals; +namespace hidev\controllers; use yii\helpers\ArrayHelper; use Symfony\Component\Yaml\Yaml; @@ -17,8 +17,10 @@ /** * Dump goal. */ -class DumpGoal extends DefaultGoal +class DumpController extends CommonController { + protected $_before = ['start']; + public function actionMake() { $data = $this->getConfig()->getItems(); diff --git a/src/goals/FileGoal.php b/src/controllers/FileController.php similarity index 89% rename from src/goals/FileGoal.php rename to src/controllers/FileController.php index f2f6092..5293520 100644 --- a/src/goals/FileGoal.php +++ b/src/controllers/FileController.php @@ -9,7 +9,7 @@ * @copyright Copyright (c) 2014-2016, HiQDev (http://hiqdev.com/) */ -namespace hidev\goals; +namespace hidev\controllers; use hidev\base\File; use hidev\helpers\Helper; @@ -17,9 +17,9 @@ use yii\helpers\ArrayHelper; /** - * A File Goal. + * File controller. */ -class FileGoal extends DefaultGoal +class FileController extends CommonController { /** * @var array|File the file to be handled. @@ -35,7 +35,7 @@ public function setTemplate($template) public function getTemplate() { - return Helper::file2template($this->_template ?: $this->goalName); + return Helper::file2template($this->_template ?: $this->id); } /** @@ -51,7 +51,7 @@ public function getFile() 'class' => File::className(), 'template' => $this->getTemplate(), 'goal' => $this, - 'path' => $this->goalName, + 'path' => $this->id, ], is_string($this->_file) ? ['path' => $this->_file] : (array) $this->_file @@ -108,7 +108,8 @@ public function actionLoad() public function actionSave() { - $this->_items = Helper::uniqueConfig($this->_items); - return $this->getFile()->save($this); + #$this->_items = Helper::uniqueConfig($this->_items); + $this->getFile()->save($this); + return 0; } } diff --git a/src/goals/GenerateGoal.php b/src/controllers/GenerateController.php similarity index 93% rename from src/goals/GenerateGoal.php rename to src/controllers/GenerateController.php index bc507d4..724352e 100644 --- a/src/goals/GenerateGoal.php +++ b/src/controllers/GenerateController.php @@ -9,12 +9,12 @@ * @copyright Copyright (c) 2014-2016, HiQDev (http://hiqdev.com/) */ -namespace hidev\goals; +namespace hidev\controllers; /** * Generate goal to build files by template and params. */ -class GenerateGoal extends TemplateGoal +class GenerateController extends TemplateController { public static function template2file($template, $extension = '.php') { diff --git a/src/goals/GitGoal.php b/src/controllers/GitController.php similarity index 92% rename from src/goals/GitGoal.php rename to src/controllers/GitController.php index 8e38a00..10c1824 100644 --- a/src/goals/GitGoal.php +++ b/src/controllers/GitController.php @@ -9,23 +9,16 @@ * @copyright Copyright (c) 2014-2016, HiQDev (http://hiqdev.com/) */ -namespace hidev\goals; +namespace hidev\controllers; use Yii; /** - * Goal for Git. + * Controller for Git. */ -class GitGoal extends VcsGoal +class GitController extends VcsController { - /** - * {@inheritdoc} - */ - public function init() - { - parent::init(); - $this->setDeps('.gitignore'); - } + protected $_before = ['.gitignore']; /** * @var array VCS tags diff --git a/src/goals/GitHubGoal.php b/src/controllers/GitHubController.php similarity index 92% rename from src/goals/GitHubGoal.php rename to src/controllers/GitHubController.php index 55b13e0..a0ef439 100644 --- a/src/goals/GitHubGoal.php +++ b/src/controllers/GitHubController.php @@ -9,12 +9,12 @@ * @copyright Copyright (c) 2014-2016, HiQDev (http://hiqdev.com/) */ -namespace hidev\goals; +namespace hidev\controllers; /** - * Goal for GitHub. + * Controller for GitHub. */ -class GitHubGoal extends DefaultGoal +class GitHubController extends CommonController { public function setName($value) { diff --git a/src/controllers/GitignoreController.php b/src/controllers/GitignoreController.php new file mode 100644 index 0000000..b8e0a79 --- /dev/null +++ b/src/controllers/GitignoreController.php @@ -0,0 +1,20 @@ +vendor = $vendor; + } + if ($package) { + $this->package = $package; + } + if (!$this->package || !$this->vendor) { throw new InvalidParamException('Wrong vendor/package given: ' . $name); } - $this->vendor = $vendor; - $this->package = $package; $this->template = $template; if (!file_exists($this->dirname)) { @@ -45,11 +53,6 @@ public function options($actionId) return array_merge(parent::options($actionId), explode(',', 'namespace,headline,title,type,license,keywords,description,year,nick,author,email,novendor,norequire')); } - public function getPackage() - { - return $this->getItem('package'); - } - public function getType() { return $this->getItem('type') ?: 'project'; diff --git a/src/goals/InstallGoal.php b/src/controllers/InstallController.php similarity index 82% rename from src/goals/InstallGoal.php rename to src/controllers/InstallController.php index 00e230b..b2f4109 100644 --- a/src/goals/InstallGoal.php +++ b/src/controllers/InstallController.php @@ -9,14 +9,14 @@ * @copyright Copyright (c) 2014-2016, HiQDev (http://hiqdev.com/) */ -namespace hidev\goals; +namespace hidev\controllers; use Yii; /** * Install goal. */ -class InstallGoal extends DefaultGoal +class InstallController extends CommonController { public function actionMake() { diff --git a/src/goals/PackageGoal.php b/src/controllers/PackageController.php similarity index 97% rename from src/goals/PackageGoal.php rename to src/controllers/PackageController.php index 655bde5..9d16808 100644 --- a/src/goals/PackageGoal.php +++ b/src/controllers/PackageController.php @@ -9,14 +9,14 @@ * @copyright Copyright (c) 2014-2016, HiQDev (http://hiqdev.com/) */ -namespace hidev\goals; +namespace hidev\controllers; use hidev\helpers\Helper; /** * Package part of the config. */ -class PackageGoal extends VendorGoal +class PackageController extends VendorController { public function getYears() { diff --git a/src/controllers/StartController.php b/src/controllers/StartController.php new file mode 100644 index 0000000..1747cb5 --- /dev/null +++ b/src/controllers/StartController.php @@ -0,0 +1,85 @@ +findDir()); + $this->getConfig()->includeConfig(static::MAIN_CONFIG); + $this->requireAll(); + $this->includeAll(); + } + + protected function requireAll() + { + $require = $this->getConfig()->rawItem('require'); + if ($require) { + $require['hiqdev/composer-extension-plugin'] = '*@dev'; + $saved = File::create('.hidev/composer.json')->save(compact('require')); + if ($saved || !is_dir('.hidev/vendor')) { + $this->getConfig()->get('update')->makeUpdate(); + } + Yii::$app->clear('config'); + Yii::$app->loadExtraVendor('.hidev/vendor'); + } + } + + /** + * Include all configs. + * @return void + */ + public function includeAll() + { + $still = true; + while ($still) { + $still = false; + $include = $this->getConfig()->rawItem('include'); + if ($include) { + foreach ($include as $path) { + $still = $still || $this->getConfig()->includeConfig($path); + } + } + } + } + + /** + * Chdirs to project's root by looking for config file in the current directory and up. + * @return string path to the root directory of hidev project + * @throws InvalidConfigException when failed to find + */ + protected function findDir() + { + $configDir = '.hidev'; + if (!$isInit) { + for ($i = 0;$i < 9;++$i) { + if (is_dir($configDir)) { + return getcwd(); + } + chdir('..'); + } + } + throw new InvalidConfigException('Not a hidev project (or any of the parent directories). Use `hidev init` to initialize hidev project.'); + } +} diff --git a/src/goals/GitignoreGoal.php b/src/controllers/SvnignoreController.php similarity index 64% rename from src/goals/GitignoreGoal.php rename to src/controllers/SvnignoreController.php index 0d9ec16..024675d 100644 --- a/src/goals/GitignoreGoal.php +++ b/src/controllers/SvnignoreController.php @@ -9,12 +9,12 @@ * @copyright Copyright (c) 2014-2016, HiQDev (http://hiqdev.com/) */ -namespace hidev\goals; +namespace hidev\controllers; /** - * Goal for .gitignore files generation. + * Controller for svn:ignore. */ -class GitignoreGoal extends VcsignoreGoal +class SvnignoreController extends VcsignoreController { - protected $_fileType = 'gitignore'; + public $fileType = 'svnignore'; } diff --git a/src/goals/TemplateGoal.php b/src/controllers/TemplateController.php similarity index 64% rename from src/goals/TemplateGoal.php rename to src/controllers/TemplateController.php index c3e9907..6e2b98f 100644 --- a/src/goals/TemplateGoal.php +++ b/src/controllers/TemplateController.php @@ -9,11 +9,12 @@ * @copyright Copyright (c) 2014-2016, HiQDev (http://hiqdev.com/) */ -namespace hidev\goals; +namespace hidev\controllers; /** - * Goal for templated files. + * Controller for templated files. */ -class TemplateGoal extends FileGoal +class TemplateController extends FileController { + public $fileType = 'template'; } diff --git a/src/goals/UpdateGoal.php b/src/controllers/UpdateController.php similarity index 73% rename from src/goals/UpdateGoal.php rename to src/controllers/UpdateController.php index 0774773..a3ce62a 100644 --- a/src/goals/UpdateGoal.php +++ b/src/controllers/UpdateController.php @@ -9,16 +9,21 @@ * @copyright Copyright (c) 2014-2016, HiQDev (http://hiqdev.com/) */ -namespace hidev\goals; +namespace hidev\controllers; /** * Update goal. */ -class UpdateGoal extends DefaultGoal +class UpdateController extends CommonController { public function actionMake() { - exec('cd .hidev;composer update --prefer-source'); + $this->makeUpdate(); $this->module->runRequest(''); } + + public function makeUpdate() + { + exec('cd .hidev;composer update --prefer-source'); + } } diff --git a/src/goals/VcsGoal.php b/src/controllers/VcsController.php similarity index 85% rename from src/goals/VcsGoal.php rename to src/controllers/VcsController.php index 0e166ee..87cf1e3 100644 --- a/src/goals/VcsGoal.php +++ b/src/controllers/VcsController.php @@ -9,14 +9,14 @@ * @copyright Copyright (c) 2014-2016, HiQDev (http://hiqdev.com/) */ -namespace hidev\goals; +namespace hidev\controllers; use Yii; /** - * Goal for VCS (Version Control Systems). + * Controller for VCS (Version Control Systems). */ -class VcsGoal extends DefaultGoal +class VcsController extends CommonController { public $lastTag = 'Under development'; diff --git a/src/goals/VcsignoreGoal.php b/src/controllers/VcsignoreController.php similarity index 90% rename from src/goals/VcsignoreGoal.php rename to src/controllers/VcsignoreController.php index 365487f..90aa903 100644 --- a/src/goals/VcsignoreGoal.php +++ b/src/controllers/VcsignoreController.php @@ -9,12 +9,12 @@ * @copyright Copyright (c) 2014-2016, HiQDev (http://hiqdev.com/) */ -namespace hidev\goals; +namespace hidev\controllers; /** - * Goal for VCS ignore files generation. + * Controller for VCS ignore files generation. */ -class VcsignoreGoal extends FileGoal +class VcsignoreController extends FileController { protected $_items = [ '.hidev/composer.json' => 'hidev internals', diff --git a/src/goals/VendorGoal.php b/src/controllers/VendorController.php similarity index 91% rename from src/goals/VendorGoal.php rename to src/controllers/VendorController.php index ebda9be..c1b492c 100644 --- a/src/goals/VendorGoal.php +++ b/src/controllers/VendorController.php @@ -9,14 +9,14 @@ * @copyright Copyright (c) 2014-2016, HiQDev (http://hiqdev.com/) */ -namespace hidev\goals; +namespace hidev\controllers; use hidev\helpers\Helper; /** * Vendor part of the config. */ -class VendorGoal extends DefaultGoal +class VendorController extends CommonController { public function getLabel() { diff --git a/src/goals/BaseGoal.php b/src/goals/BaseGoal.php deleted file mode 100644 index 0d1d018..0000000 --- a/src/goals/BaseGoal.php +++ /dev/null @@ -1,22 +0,0 @@ -setDeps('commits'); - } -} diff --git a/src/goals/DefaultGoal.php b/src/goals/DefaultGoal.php deleted file mode 100644 index 9116e4f..0000000 --- a/src/goals/DefaultGoal.php +++ /dev/null @@ -1,204 +0,0 @@ -_fileType; - } - - public function setFileType($type) - { - $this->_fileType = $type; - } - - public function setDeps($deps) - { - $res = $this->getDeps(); - foreach (Helper::ksplit($deps) as $d => $e) { - $res[is_int($d) ? $e : $d] = $e; - } - $this->setItem('deps', $res); - } - - public function getDeps() - { - return Helper::ksplit($this->rawItem('deps')); - } - - public function actionDeps() - { - foreach ($this->getDeps() as $name => $enabled) { - if ($enabled) { - $res = $this->module->runRequest($name); - if (static::isNotOk($res)) { - return $res; - } - } - } - - return 0; - } - - public function runRequest($request) - { - return $request !== null ? $this->module->runRequest($request) : null; - } - - /** - * Runs array of requests. Stops on failure and returns exit code. - * @param null|string|array $requests - */ - public function runRequests($requests) - { - if (is_string($requests)) { - $requests = [$requests]; - } elseif (!is_array($requests)) { - return 0; - } - foreach ($requests as $request) { - $res = $this->runRequest($request); - if (static::isNotOk($res)) { - return $res; - } - } - - return 0; - } - - public function isDone($action, $timestamp = null) - { - if ($this->done[$action]) { - Yii::trace("Already done: $this->goalName/$action"); - - return true; - } - - return false; - } - - /** - * Mark action as already done. - * - * @param string $action action id - * @param int $time microtime when action was done, false for action was not done - */ - public function markDone($action, $time = null) - { - $this->done[$action] = ($time === null || $time === true) ? microtime(1) : $time; - } - - public function actionPerform($name = null, $path = null) - { - Yii::trace("Started: $this->goalName"); - return $this->runActions('deps, make'); - } - - public function actionLoad() - { - Yii::trace("Loading nothing for '$this->goalName'"); - } - - public function actionSave() - { - Yii::trace("Saving nothing for '$this->goalName'"); - } - - public function actionMake() - { - return $this->runActions('load, save'); - } - - public function runAction($id, $params = []) - { - if ($this->isDone($id)) { - return; - } - $result = parent::runAction($id, $params); - $this->markDone($id); - - return $result; - } - - public function runActions($actions) - { - foreach (Helper::ksplit($actions) as $action) { - $res = $this->runAction($action); - if (static::isNotOk($res)) { - return $res; - } - } - - return 0; - } - - public static function isNotOk($res) - { - return is_object($res) ? $res->exitStatus : $res; - } - - public function options($actionId) - { - return array_merge(parent::options($actionId), array_keys(Helper::getPublicVars(get_called_class()))); - } - - /** - * Runs given binary with given arguments. - * @param string $name - * @param string $args - * @return int exit code - */ - public function passthru($name, $args = '') - { - return Yii::$app->get('binaries')->passthru($name, $args); - } - - public function getRobo() - { - return Yii::$app->get('robo'); - } - - public function getConfig() - { - return Yii::$app->get('config'); - } - - public function getPackage() - { - return $this->getConfig()->get('package'); - } - - public function getVendor() - { - return $this->getConfig()->get('vendor'); - } - - public function getVcs() - { - /// TODO determine VCS - return $this->getConfig()->get('git'); - } -} diff --git a/src/goals/StartGoal.php b/src/goals/StartGoal.php deleted file mode 100644 index a2759fb..0000000 --- a/src/goals/StartGoal.php +++ /dev/null @@ -1,71 +0,0 @@ -findDir()); - $this->loadExtensions(); - } - - protected function loadExtensions() - { - $require = Yii::createObject([ - 'class' => 'hidev\base\File', - 'path' => '.hidev/config.yml', - ])->load()['require']; - if ($require) { - Yii::createObject([ - 'class' => 'hidev\base\File', - 'path' => '.hidev/composer.json', - ])->save(compact('require')); - if (!is_dir('.hidev/vendor')) { - exec('cd .hidev;composer update --prefer-source'); - } - $main = Yii::getAlias('@vendor'); - $local = realpath('./.hidev/vendor'); - if ($local !== $main) { - $this->extensions = array_merge( - $this->prepareExtensions($main), - $this->prepareExtensions($local) - ); - } - } - } - - /** - * Looks for config file in the current directory and up. - * @return string path to the root directory of hidev project - * @throws InvalidConfigException when failed to find - */ - protected function findDir() - { - $configDir = '.hidev'; - if (!$isInit) { - for ($i = 0;$i < 9;++$i) { - if (is_dir($configDir)) { - return getcwd(); - } - chdir('..'); - } - } - throw new InvalidConfigException('Not a hidev project (or any of the parent directories). Use `hidev init` to initialize hidev project.'); - } -} diff --git a/src/handlers/BaseHandler.php b/src/handlers/BaseHandler.php index d1cf1ff..c9b7afd 100644 --- a/src/handlers/BaseHandler.php +++ b/src/handlers/BaseHandler.php @@ -81,7 +81,8 @@ public function render($data) public function prepareData($data) { - return ArrayHelper::toArray($data); + return ArrayHelper::toArray($data, [], false); + #return $data; } /**