├── LICENSE.md ├── README.md ├── Runner.php └── composer.json /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015, Giulio Ganci 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without modification, 5 | are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | 10 | * Redistributions in binary form must reproduce the above copyright notice, this 11 | list of conditions and the following disclaimer in the documentation and/or 12 | other materials provided with the distribution. 13 | 14 | * Neither the names of Giulio Ganci nor the names of its 15 | contributors may be used to endorse or promote products derived from 16 | this software without specific prior written permission. 17 | 18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 19 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 20 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 22 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 23 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 24 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 25 | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 27 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | yii2-console-runner 2 | ======================== 3 | [![Latest Stable Version](https://poser.pugx.org/toriphes/yii2-console-runner/v/stable)](https://packagist.org/packages/toriphes/yii2-console-runner) [![Total Downloads](https://poser.pugx.org/toriphes/yii2-console-runner/downloads)](https://packagist.org/packages/toriphes/yii2-console-runner) [![Latest Unstable Version](https://poser.pugx.org/toriphes/yii2-console-runner/v/unstable)](https://packagist.org/packages/toriphes/yii2-console-runner) [![License](https://poser.pugx.org/toriphes/yii2-console-runner/license)](https://packagist.org/packages/toriphes/yii2-console-runner) 4 | 5 | This is component for running console command in yii2 web applications 6 | 7 | ## Installation 8 | 9 | The preferred way to install this extension is through [composer](http://getcomposer.org/download/). 10 | 11 | To install, either run 12 | 13 | ``` 14 | $ composer require toriphes/yii2-console-runner "*" 15 | ``` 16 | 17 | or add 18 | 19 | ``` 20 | "toriphes/yii2-console-runner": "*" 21 | ``` 22 | 23 | to the ```require``` section of your `composer.json` file. 24 | 25 | ## Usage 26 | 27 | You can user yii2-console-runner importing the class file 28 | ```php 29 | use toriphes\console\Runner; 30 | $output = ''; 31 | $runner = new Runner(); 32 | $runner->run('controller/action param1 param2 ...', $output); 33 | echo $output; //prints the command output 34 | ``` 35 | 36 | or using it like an application component: 37 | ```php 38 | //you config file 39 | 'components' => [ 40 | 'consoleRunner' => [ 41 | 'class' => 'toriphes\console\Runner' 42 | ] 43 | ] 44 | ``` 45 | ```php 46 | //some application file 47 | $output = ''; 48 | Yii::$app->consoleRunner->run('controller/action param1 param2 ...', $output); 49 | echo $output; //prints the command output 50 | ``` 51 | -------------------------------------------------------------------------------- /Runner.php: -------------------------------------------------------------------------------- 1 | 6 | * @copyright Copyright (c) 2015 Giulio Ganci 7 | * @license BSD-3-Clause 8 | * @version 1.0 9 | */ 10 | 11 | namespace toriphes\console; 12 | 13 | use Yii; 14 | use yii\base\Component; 15 | 16 | /** 17 | * Class Runner - a component for running console command in yii2 web applications 18 | * 19 | * This extensions is inspired by the project https://github.com/vova07/yii2-console-runner-extension 20 | * 21 | * Basic usage: 22 | * ```php 23 | * use toriphes\console\Runner; 24 | * $output = ''; 25 | * $runner = new Runner(); 26 | * $runner->run('controller/action param1 param2 ...', $output); 27 | * echo $output; //prints the command output 28 | * ``` 29 | * 30 | * Application component usage: 31 | * ```php 32 | * //you config file 33 | * 'components' => [ 34 | * 'consoleRunner' => [ 35 | * 'class' => 'toriphes\console\Runner' 36 | * ] 37 | * ] 38 | * ``` 39 | * ```php 40 | * //some application file 41 | * $output = ''; 42 | * Yii::$app->consoleRunner->run('controller/action param1 param2 ...', $output); 43 | * echo $output; //prints the command output 44 | * ``` 45 | * @author Giulio Ganci 46 | */ 47 | class Runner extends Component 48 | { 49 | /** 50 | * @var string yii console application file that will be executed 51 | */ 52 | public $yiiscript; 53 | 54 | /** 55 | * @var string path to php executable 56 | */ 57 | public $phpexec; 58 | 59 | /** 60 | * @inheritdoc 61 | */ 62 | public function init() 63 | { 64 | parent::init(); 65 | 66 | set_time_limit(0); 67 | 68 | if($this->yiiscript == null) { 69 | $this->yiiscript = "@app/yii"; 70 | } 71 | } 72 | 73 | /** 74 | * Runs yii console command 75 | * 76 | * @param $cmd command with arguments 77 | * @param string $output filled with the command output 78 | * @return int termination status of the process that was run 79 | */ 80 | public function run($cmd, &$output = '') 81 | { 82 | $handler = popen($this->buildCommand($cmd), 'r'); 83 | 84 | while(!feof($handler)) 85 | $output .= fgets($handler); 86 | 87 | $output = trim($output); 88 | $status = pclose($handler); 89 | 90 | return $status; 91 | } 92 | 93 | /** 94 | * Builds the command string 95 | * 96 | * @param $cmd Yii command 97 | * @return string full command to execute 98 | */ 99 | protected function buildCommand($cmd) 100 | { 101 | return $this->getPHPExecutable() . ' ' . Yii::getAlias($this->yiiscript) . ' ' . $cmd . ' 2>&1'; 102 | } 103 | 104 | /** 105 | * If property $phpexec is set it will be used as php executable 106 | * 107 | * @return string path to php executable 108 | */ 109 | public function getPHPExecutable() 110 | { 111 | if($this->phpexec) { 112 | return $this->phpexec; 113 | } 114 | 115 | return strpos(PHP_SAPI, 'apache') !== false ? PHP_BINDIR . '/php' : PHP_BINARY; 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "toriphes/yii2-console-runner", 3 | "description": "Runs console command in yii web application", 4 | "type": "yii2-extension", 5 | "keywords": ["yii2", "extension", "console", "runner", "web", "application"], 6 | "license": "BSD-3-Clause", 7 | "authors": [ 8 | { 9 | "name": "Giulio Ganci", 10 | "email": "giulioganci@gmail.com" 11 | } 12 | ], 13 | "minimum-stability": "dev", 14 | "require": { 15 | "yiisoft/yii2": ">=2.0.0" 16 | }, 17 | "autoload": { 18 | "psr-4": { 19 | "toriphes\\console\\": "" 20 | } 21 | } 22 | } 23 | --------------------------------------------------------------------------------