├── LICENSE ├── README.md ├── composer.json └── src └── ConsoleRunner.php /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014, Vasile Crudu 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, 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, 11 | this list of conditions and the following disclaimer in the documentation 12 | and/or other materials provided with the distribution. 13 | 14 | * Neither the name of the {organization} 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" 19 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 22 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 24 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 25 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 26 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Console Runner 2 | 3 | [![Latest Version](https://img.shields.io/github/tag/vova07/yii2-console-runner-extension.svg?style=flat-square&label=release)](https://github.com/vova07/yii2-console-runner-extension/releases) 4 | [![Software License](https://img.shields.io/badge/license-BSD-brightgreen.svg?style=flat-square)](LICENSE.md) 5 | [![Build Status](https://img.shields.io/travis/vova07/yii2-console-runner-extension/master.svg?style=flat-square)](https://travis-ci.org/vova07/yii2-console-runner-extension) 6 | [![Coverage Status](https://img.shields.io/scrutinizer/coverage/g/vova07/yii2-console-runner-extension.svg?style=flat-square)](https://scrutinizer-ci.com/g/vova07/yii2-console-runner-extension/code-structure) 7 | [![Quality Score](https://img.shields.io/scrutinizer/g/vova07/yii2-console-runner-extension.svg?style=flat-square)](https://scrutinizer-ci.com/g/vova07/yii2-console-runner-extension) 8 | [![Total Downloads](https://img.shields.io/packagist/dt/vova07/yii2-console-runner-extension.svg?style=flat-square)](https://packagist.org/packages/vova07/yii2-console-runner-extension) 9 | 10 | An extension for running console commands on background in Yii framework. 11 | 12 | ## Installation 13 | 14 | Add the following to `require` section of your `composer.json`: 15 | 16 | ``` 17 | "vova07/yii2-console-runner-extension": "*" 18 | ``` 19 | 20 | Then do `composer install`. 21 | 22 | ## Usage 23 | 24 | ##### Imported class: 25 | 26 | ```php 27 | use vova07\console\ConsoleRunner; 28 | $cr = new ConsoleRunner(['file' => '@my/path/to/yii']); 29 | $cr->run('controller/action param1 param2 ...'); 30 | ``` 31 | 32 | ##### Application component: 33 | 34 | ```php 35 | // config.php 36 | ... 37 | components [ 38 | 'consoleRunner' => [ 39 | 'class' => 'vova07\console\ConsoleRunner', 40 | 'file' => '@my/path/to/yii' // or an absolute path to console file 41 | ] 42 | ] 43 | ... 44 | 45 | // some-file.php 46 | Yii::$app->consoleRunner->run('controller/action param1 param2 ...'); 47 | ``` 48 | 49 | ### Running Tests 50 | ```bash 51 | $ phpunit 52 | ``` 53 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "vova07/yii2-console-runner-extension", 3 | "description": "An extension for running console commands on background in Yii framework.", 4 | "keywords": ["yii2", "yii 2", "console", "runner", "commands", "background"], 5 | "homepage": "https://github.com/vova07/yii2-console-runner-extension", 6 | "type": "yii2-extension", 7 | "license": "BSD-3-Clause", 8 | "authors": [ 9 | { 10 | "name": "Crudu Vasile", 11 | "email": "bazillio07@yandex.ru" 12 | } 13 | ], 14 | "support": { 15 | "issues": "https://github.com/vova07/yii2-console-runner-extension/issues?state=open", 16 | "source": "https://github.com/vova07/yii2-console-runner-extension" 17 | }, 18 | "minimum-stability": "stable", 19 | "require": { 20 | "yiisoft/yii2": "*" 21 | }, 22 | "require-dev": { 23 | "phpunit/phpunit": "5.*" 24 | }, 25 | "autoload": { 26 | "psr-4": { 27 | "vova07\\console\\": ["src", "tests"] 28 | } 29 | }, 30 | "repositories": [ 31 | { 32 | "type": "composer", 33 | "url": "https://asset-packagist.org" 34 | } 35 | ] 36 | } 37 | -------------------------------------------------------------------------------- /src/ConsoleRunner.php: -------------------------------------------------------------------------------- 1 | '@my/path/to/yii', 17 | * 'phpBinaryPath' => '/my/path/to/php', // This is an optional param you may use to override the default `php` binary path. 18 | * ]); 19 | * $cr->run('controller/action param1 param2 ...'); 20 | * ... 21 | * ``` 22 | * or use it like an application component: 23 | * ``` 24 | * // config.php 25 | * ... 26 | * components [ 27 | * 'consoleRunner' => [ 28 | * 'class' => 'vova07\console\ConsoleRunner', 29 | * 'file' => '@my/path/to/yii', // Or an absolute path to console file. 30 | * 'phpBinaryPath' => '/my/path/to/php', // This is an optional param you may use to override the default `php` binary path. 31 | * ] 32 | * ] 33 | * ... 34 | * 35 | * // some-file.php 36 | * Yii::$app->consoleRunner->run('controller/action param1 param2 ...'); 37 | * ``` 38 | */ 39 | class ConsoleRunner extends Component 40 | { 41 | /** 42 | * Usually it can be `yii` file. 43 | * 44 | * @var string Console application file that will be executed. 45 | */ 46 | public $file; 47 | 48 | /** 49 | * @var string The PHP binary path. 50 | */ 51 | public $phpBinaryPath = PHP_BINDIR . '/php'; 52 | 53 | /** 54 | * @inheritdoc 55 | */ 56 | public function init() 57 | { 58 | parent::init(); 59 | 60 | if ($this->file === null) { 61 | throw new InvalidConfigException('The "file" property must be set.'); 62 | } else { 63 | $this->file = Yii::getAlias($this->file); 64 | } 65 | } 66 | 67 | /** 68 | * Running console command on background. 69 | * 70 | * @param string $cmd Argument that will be passed to console application. 71 | * 72 | * @return boolean 73 | */ 74 | public function run($cmd) 75 | { 76 | $cmd = "{$this->phpBinaryPath} {$this->file} $cmd"; 77 | $cmd = $this->isWindows() === true 78 | ? $cmd = "start /b {$cmd}" 79 | : $cmd = "{$cmd} > /dev/null 2>&1 &"; 80 | 81 | pclose(popen($cmd, 'r')); 82 | 83 | return true; 84 | } 85 | 86 | /** 87 | * Check operating system. 88 | * 89 | * @return boolean `true` if it's Windows OS. 90 | */ 91 | protected function isWindows() 92 | { 93 | return PHP_OS == 'WINNT' || PHP_OS == 'WIN32'; 94 | } 95 | } 96 | --------------------------------------------------------------------------------