1. Go to this page and download the library: Download soliphp/console library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
soliphp / console example snippets
// 终端命令默认访问的命名空间
namespace App\Console;
and
{
/**
* 默认执行的方法:index
*/
public function index($name = 'wukong')
{
return "hello $name, in task:index.\n";
}
/**
* 命令行输入的参数将按顺序作为 action 的参数传入
*/
public function handle($name = 'wukong', $alias = '孙行者')
{
return "hello $name <$alias>, in task:handle.\n";
}
}
$app = new \Soli\Console\App();
echo $app->handle();
namespace App\Console;
/**
* 终端命令结合多进程
*/
class Proc2 extends \Soli\Console\Command
{
/** @var bool $runnable 这个属性决定了action是否以多进程方式运行 */
protected $runnable = false;
/** @var string $name 进程名称,只在 Linux 系统下起作用 */
protected $name = 'soli console proc2';
/** @var int $count 进程数 */
protected $count = 4;
/** @var bool $daemonize 是否以守护进程方式运行 */
protected $daemonize = false;
/** @var bool $refork 是否自动补充退出的进程 */
protected $refork = false;
/** @var string $logFile 记录进程的输出、终止等信息到此文件 */
protected $logFile = '/tmp/soli-console-proc2.log';
public function __construct()
{
// 针对不同的 action 可以选择是否使用多进程,以及指定不同的进程属性
$action = $this->dispatcher->getActionName();
switch ($action) {
case 'command':
$this->runnable = false;
break;
case 'process':
$this->runnable = true;
$this->logFile = null;
break;
case 'daemonize':
$this->runnable = true;
$this->daemonize = true;
echo "Process output info in {$this->logFile}\n";
break;
case 'refork':
$this->runnable = true;
$this->refork = true;
break;
}
}
public function command($name = 'wukong')
{
echo "hello $name. just a command.\n";
}
/**
* $worker 参数将成为 action 的第一个参数,$worker 参数之后是从命令行输入的参数
*/
public function process(\Soli\Process $worker, $name = 'wukong')
{
echo "hello $name. dump message from [worker:{$worker->id} {$worker->workerPid}] process.\n";
}
public function daemonize(\Soli\Process $worker)
{
echo "dump message from [worker:{$worker->id} {$worker->workerPid}] daemonize process.\n";
}
public function refork(\Soli\Process $worker)
{
echo "dump message from [worker:{$worker->id} {$worker->workerPid}] process.\n";
}
}
php task.php task
// 或
php task.php task:index
php task.php task:handle
php task.php task:handle hulk 绿巨人
php examples/console proc2:command
php examples/console proc2:daemonize
php examples/console proc2:refork
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.