From 31d9cbd9a1abe0ca679ba603150404b63f4d376b Mon Sep 17 00:00:00 2001 From: Tobias Munk Date: Wed, 22 May 2013 23:41:50 +0200 Subject: [PATCH 1/9] added run method to execute yii commands --- .../composer/yii/composer/InstallHandler.php | 33 ++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/extensions/composer/yii/composer/InstallHandler.php b/extensions/composer/yii/composer/InstallHandler.php index 6d43abd..794ec24 100644 --- a/extensions/composer/yii/composer/InstallHandler.php +++ b/extensions/composer/yii/composer/InstallHandler.php @@ -9,10 +9,16 @@ namespace yii\composer; use Composer\Script\CommandEvent; +defined('YII_DEBUG') or define('YII_DEBUG', true); + +// fcgi doesn't have STDIN defined by default +defined('STDIN') or define('STDIN', fopen('php://stdin', 'r')); + /** * InstallHandler is called by Composer after it installs/updates the current package. * * @author Qiang Xue + * @author Tobias Munk * @since 2.0 */ class InstallHandler @@ -50,4 +56,31 @@ class InstallHandler } } } + + /** + * Executes a yii command. + * @param CommandEvent $event + */ + public static function run($event) + { + $options = array_merge(array( + 'run' => array(), + ), $event->getComposer()->getPackage()->getExtra()); + + $appPath = realpath(__DIR__ . '/../../../../..'); + + require($appPath . '/vendor/yiisoft/yii2/yii/Yii.php'); + require($appPath . '/vendor/autoload.php'); + $config = require($appPath . '/config/console.php'); + + foreach((array)$options['run'] as $params){ + $command = $params[0]; + unset($params[0]); + $params = array(); + // TODO: add params to array + echo "Running command: {$command}\n"; + $application = new \yii\console\Application($config); + $application->runAction($command, $params); + } + } } From d878520c38cc8d0459a3deab0599d764059beb74 Mon Sep 17 00:00:00 2001 From: Tobias Munk Date: Thu, 23 May 2013 00:04:06 +0200 Subject: [PATCH 2/9] fixed formatting --- extensions/composer/yii/composer/InstallHandler.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/extensions/composer/yii/composer/InstallHandler.php b/extensions/composer/yii/composer/InstallHandler.php index 794ec24..961f39b 100644 --- a/extensions/composer/yii/composer/InstallHandler.php +++ b/extensions/composer/yii/composer/InstallHandler.php @@ -64,8 +64,8 @@ class InstallHandler public static function run($event) { $options = array_merge(array( - 'run' => array(), - ), $event->getComposer()->getPackage()->getExtra()); + 'run' => array(), + ), $event->getComposer()->getPackage()->getExtra()); $appPath = realpath(__DIR__ . '/../../../../..'); From 8e6b427f60718d29c74f42358125860a59657e7a Mon Sep 17 00:00:00 2001 From: Tobias Munk Date: Thu, 23 May 2013 12:25:18 +0200 Subject: [PATCH 3/9] removed composer autoloading, since it's already active --- extensions/composer/yii/composer/InstallHandler.php | 1 - 1 file changed, 1 deletion(-) diff --git a/extensions/composer/yii/composer/InstallHandler.php b/extensions/composer/yii/composer/InstallHandler.php index 961f39b..0f121e6 100644 --- a/extensions/composer/yii/composer/InstallHandler.php +++ b/extensions/composer/yii/composer/InstallHandler.php @@ -70,7 +70,6 @@ class InstallHandler $appPath = realpath(__DIR__ . '/../../../../..'); require($appPath . '/vendor/yiisoft/yii2/yii/Yii.php'); - require($appPath . '/vendor/autoload.php'); $config = require($appPath . '/config/console.php'); foreach((array)$options['run'] as $params){ From f53b21758930c13432466b87ef4832bf83333e68 Mon Sep 17 00:00:00 2001 From: Tobias Munk Date: Thu, 23 May 2013 12:26:11 +0200 Subject: [PATCH 4/9] added parsing of anonymous params --- extensions/composer/yii/composer/InstallHandler.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/extensions/composer/yii/composer/InstallHandler.php b/extensions/composer/yii/composer/InstallHandler.php index 0f121e6..2b3661d 100644 --- a/extensions/composer/yii/composer/InstallHandler.php +++ b/extensions/composer/yii/composer/InstallHandler.php @@ -8,6 +8,7 @@ namespace yii\composer; use Composer\Script\CommandEvent; +use yii\console; defined('YII_DEBUG') or define('YII_DEBUG', true); @@ -72,11 +73,13 @@ class InstallHandler require($appPath . '/vendor/yiisoft/yii2/yii/Yii.php'); $config = require($appPath . '/config/console.php'); - foreach((array)$options['run'] as $params){ - $command = $params[0]; - unset($params[0]); - $params = array(); - // TODO: add params to array + foreach ((array)$options['run'] as $rawParams) { + // TODO: we're doing about the same here like console\Request::resolve() + $command = $rawParams[0]; + unset($rawParams[0]); + $params[\yii\console\Request::ANONYMOUS_PARAMS] = $rawParams; + // TODO end + echo "Running command: {$command}\n"; $application = new \yii\console\Application($config); $application->runAction($command, $params); From e7c98a499e454e533110dd9c972df15043aa6d60 Mon Sep 17 00:00:00 2001 From: Tobias Munk Date: Thu, 23 May 2013 16:02:39 +0200 Subject: [PATCH 5/9] added configuration file detection from composer.json (extra.config), path cleanup --- extensions/composer/yii/composer/InstallHandler.php | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/extensions/composer/yii/composer/InstallHandler.php b/extensions/composer/yii/composer/InstallHandler.php index 2b3661d..6ac645a 100644 --- a/extensions/composer/yii/composer/InstallHandler.php +++ b/extensions/composer/yii/composer/InstallHandler.php @@ -66,12 +66,21 @@ class InstallHandler { $options = array_merge(array( 'run' => array(), + 'config' => null, ), $event->getComposer()->getPackage()->getExtra()); - $appPath = realpath(__DIR__ . '/../../../../..'); + // resolve and include config file + if (($options['config'] === null)) { + throw new console\Exception('Config file not specified in composer.json extra.config'); + } else { + if (!is_file(getcwd() . '/' . $options['config'])) { + throw new console\Exception("Config file '{$options['config']}' specified in composer.json extra.config not found"); + } else { + $config = require($options['config']); + } + } - require($appPath . '/vendor/yiisoft/yii2/yii/Yii.php'); - $config = require($appPath . '/config/console.php'); + require(__DIR__ . '/../../../yii2/yii/Yii.php'); foreach ((array)$options['run'] as $rawParams) { // TODO: we're doing about the same here like console\Request::resolve() From c73ab91099282936bbc06cc1831da601cd32e3d6 Mon Sep 17 00:00:00 2001 From: Tobias Munk Date: Thu, 23 May 2013 18:16:18 +0200 Subject: [PATCH 6/9] added line-break after message --- apps/bootstrap/commands/HelloController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/bootstrap/commands/HelloController.php b/apps/bootstrap/commands/HelloController.php index 16f5f74..b5ecac2 100644 --- a/apps/bootstrap/commands/HelloController.php +++ b/apps/bootstrap/commands/HelloController.php @@ -24,6 +24,6 @@ class HelloController extends Controller */ public function actionIndex($message = 'hello world') { - echo $message; + echo $message."\n"; } } \ No newline at end of file From 8264c5c51d13a513bd650529286170bd89cab491 Mon Sep 17 00:00:00 2001 From: Tobias Munk Date: Thu, 23 May 2013 18:17:50 +0200 Subject: [PATCH 7/9] refactored param parsing --- extensions/composer/yii/composer/InstallHandler.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/extensions/composer/yii/composer/InstallHandler.php b/extensions/composer/yii/composer/InstallHandler.php index 6ac645a..1171889 100644 --- a/extensions/composer/yii/composer/InstallHandler.php +++ b/extensions/composer/yii/composer/InstallHandler.php @@ -80,17 +80,17 @@ class InstallHandler } } + // prepare console application require(__DIR__ . '/../../../yii2/yii/Yii.php'); + $application = new \yii\console\Application($config); + $request = $application->getRequest(); - foreach ((array)$options['run'] as $rawParams) { - // TODO: we're doing about the same here like console\Request::resolve() - $command = $rawParams[0]; - unset($rawParams[0]); - $params[\yii\console\Request::ANONYMOUS_PARAMS] = $rawParams; - // TODO end - + // run commands from extra.run + foreach ((array)$options['run'] as $rawCommand) { + $opts = str_getcsv($rawCommand, ' '); // see http://stackoverflow.com/a/6609509/291573 + $request->setParams($opts); + list($command, $params) = $request->resolve(); echo "Running command: {$command}\n"; - $application = new \yii\console\Application($config); $application->runAction($command, $params); } } From 34793c1303206a6e3ee8872cd21f270d471250c0 Mon Sep 17 00:00:00 2001 From: Tobias Munk Date: Thu, 23 May 2013 18:33:48 +0200 Subject: [PATCH 8/9] added full display of command --- extensions/composer/yii/composer/InstallHandler.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extensions/composer/yii/composer/InstallHandler.php b/extensions/composer/yii/composer/InstallHandler.php index 1171889..0c4eb2b 100644 --- a/extensions/composer/yii/composer/InstallHandler.php +++ b/extensions/composer/yii/composer/InstallHandler.php @@ -90,7 +90,7 @@ class InstallHandler $opts = str_getcsv($rawCommand, ' '); // see http://stackoverflow.com/a/6609509/291573 $request->setParams($opts); list($command, $params) = $request->resolve(); - echo "Running command: {$command}\n"; + echo "Running command: yiic {$rawCommand}\n"; $application->runAction($command, $params); } } From ebe31c5f604338a50de9e6ece52852a16c3e2778 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Thu, 23 May 2013 13:24:10 -0400 Subject: [PATCH 9/9] Fixes issue 380: invalid route exception should be 404. --- framework/yii/web/Application.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/framework/yii/web/Application.php b/framework/yii/web/Application.php index 3387044..a786985 100644 --- a/framework/yii/web/Application.php +++ b/framework/yii/web/Application.php @@ -8,6 +8,8 @@ namespace yii\web; use Yii; +use yii\base\HttpException; +use yii\base\InvalidRouteException; /** * Application is the base class for all application classes. @@ -25,6 +27,7 @@ class Application extends \yii\base\Application /** * Processes the request. * @return integer the exit status of the controller action (0 means normal, non-zero values mean abnormal) + * @throws HttpException if the request cannot be resolved. */ public function processRequest() { @@ -32,7 +35,11 @@ class Application extends \yii\base\Application Yii::setAlias('@wwwroot', dirname($request->getScriptFile())); Yii::setAlias('@www', $request->getBaseUrl()); list ($route, $params) = $request->resolve(); - return $this->runAction($route, $params); + try { + return $this->runAction($route, $params); + } catch (InvalidRouteException $e) { + throw new HttpException(404, $e->getMessage(), $e->getCode(), $e); + } } private $_homeUrl;