实现 laravel 的artisan
laravel 的 artisan 命令行太好用了,换个框架没有这个功能,于是自己学习实现一些,直接上代码
新建目录
-artisan
--bin
--src
进入artisan composer init
composer require symfony/console
#!/usr/bin/env php
<?php use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface; require_once __DIR__.'/../vendor/autoload.php'; $app = new Application('artisan','1.1.1'); $app->register('artisan')->setCode(function(InputInterface $input, OutputInterface $output){
$output->writeln('artisan start');
}); $app->run(); exit(); 以上是简单的实现
#!/usr/bin/env php
<?php use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument; require_once __DIR__ . '/../vendor/autoload.php'; $app = new Application('artisan', '1.1.1'); $app->register('artisan')
->setDescription('myself artisan description')
->setCode(
function (InputInterface $input, OutputInterface $output) {
$name = $input->getArgument('name');
$output->writeln("hello {$name}");
}
)->addArgument('name', InputArgument::REQUIRED, 'please input your name'); $app->run(); exit(); 这里演示了如何接收参数
#!/usr/bin/env php
<?php use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption; require_once __DIR__ . '/../vendor/autoload.php'; $app = new Application('artisan', '1.1.1'); $app->register('artisan')
->setDescription('myself artisan description')
->setCode(
function (InputInterface $input, OutputInterface $output) {
$string = $input->getOption('string');
$name = $input->getArgument('name');
if($string == 'lower'){
$name = strtolower($name);
}
if($string == 'upper'){
$name = strtoupper($name);
}
$output->writeln("hello {$name}");
}
)->addArgument('name', InputArgument::REQUIRED, 'please input your name')
->addOption('string',null,InputOption::VALUE_OPTIONAL,'转换字符串大小','lower')
; $app->run(); exit(); 这里演示了如何给命令行添加选项 ./bin/artisan.php artisan ffff --string='upper' echo FFFF
$output->writeln("<info>hello {$name}</info>");
$output->writeln("<error>hello {$name}</error>");
$output->writeln("<comment>hello {$name}</comment>");
$output->writeln("hello {$name}");
可以给它们加上颜色

接下来将命令行拆分为文件

bin/artisan.php
ArtisanCommand.php
#!/usr/bin/env php
<?php use Symfony\Component\Console\Application;
use Artisan\ArtisanCommand;
require_once __DIR__ . '/../vendor/autoload.php'; $app = new Application('artisan', '1.1.1'); $app->add(new ArtisanCommand()); $app->run(); exit(); ArtisanCommand.php
<?php
namespace Artisan; use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Command\Command; class ArtisanCommand extends Command{
public function configure()
{
$this->setName('artisan');
$this->setDescription('myself artisan description')
->addArgument('name', InputArgument::REQUIRED, 'please input your name')
->addOption('string',null,InputOption::VALUE_OPTIONAL,'转换字符串大小','lower');
} public function execute(InputInterface $input, OutputInterface $output)
{
$string = $input->getOption('string');
$name = $input->getArgument('name');
if($string == 'lower'){
$name = strtolower($name);
}
if($string == 'upper'){
$name = strtoupper($name);
}
$output->writeln("<info>hello {$name}</info>");
$output->writeln("<error>hello {$name}</error>");
$output->writeln("<comment>hello {$name}</comment>");
$output->writeln("hello {$name}");
}
}
composer.json
{
"name": "baidu/artisan",
"authors": [
{
"name": "gaobingbing",
"email": "v_gaobingbing01@baidu.com"
}
],
"require": {
"symfony/console": "^4.3"
},
"autoload": {
"psr-4": {
"Artisan\\": "src"
}
}
}
至此大功告成,还有其他功能可以去看Symfony文档
实现 laravel 的artisan的更多相关文章
- 使用laravel 的artisan快速创建表
参考:使用laravel 的artisan快速创建表 字段类型参考链接: 结构生成器 版本: Laravel 4.2 1. 创建migrate 文件 php artisan migrate:make ...
- Laravel 的Artisan 命令学习
Laravel 的Artisan 命令学习 Artisan 是 Laravel 提供的 CLI(命令行接口),它提供了非常多实用的命令来帮助我们开发 Laravel 应用.前面我们已使用过 Artis ...
- 源码解读 Laravel PHP artisan config:cache
来源 https://laravel-china.org/articles/5101/source-code-reading-laravel-php-artisan-configcache 源码在哪 ...
- Laravel 之Artisan
简介: Artisan是Laravel中自带的命令行工具的名称: 由强大的Symfony Console组件驱动的: 提供了一些对应用开发有帮助的命令: 查看所有可用的Artisan的命令 php a ...
- (1) laravel php artisan list make
php artisan list make Laravel Framework 5.4.36 Usage: command [options] [arguments] Options: -h, --h ...
- Laravel使用artisan快速实现表单的登陆注册
1. 开发环境 macOS Mojave 10.14.6 XAMPP 5.6.38 Laravel 5.2 2. 在终端,先进入到项目根目录并执行执行命令 php artisan make:auth ...
- laravel 使用artisan命令新增数据库字段
php artisan make:migration create_comments_table <?php use Illuminate\Database\Schema\Blueprint; ...
- laravel php artisan migrate 数据迁移时出现的[HY000][1045]错误
(zz找了块一个小时才发现)主要的错误在于.env文件和database.php的配置不匹配. 1.找到.env文件 2.更改数据库表账密 3.改database.php的数据库账密 4.完成
- laravel中artisan的用法
如:
随机推荐
- 递归型SPFA+二分答案 || 负环 || BZOJ 4773
题解: 基本思路是二分答案,每次用Dfs型SPFA验证该答案是否合法. 一点细节我注释在代码里了. 代码: #include<cstdio> #include<cstring> ...
- The Preliminary Contest for ICPC Asia Shenyang 2019 C. Dawn-K's water
题目:https://nanti.jisuanke.com/t/41401思路:完全背包 #include<bits/stdc++.h> using namespace std; int ...
- grunt-contrib-jshint js代码检查
grunt-contrib-jshint:用于javascript代码检查(并会给出建议),发布js代码前执行jshint任务, 可以避免出现一些低级语法问题jshint拥有非常丰富的配置,可以自由控 ...
- 2019ICPC南京网络赛总结
这次是在学校打的,总体不算好,过两题校排200多..很惨. 开场一段时间没人过题,但是很多人交I, 我也就再看,看着看着发现不可做,这时候转F,花了半天读懂题意的时候想到主席树查找.但是主席树这种查找 ...
- 浮点数的存储、类型转换知识点(面宝P34)
以float a=1.0f为例: (int)a实际上是以浮点数a为参数构造了一个整型数,该整数的值是1: (int&)a则是告诉编译器将a当作整数看(并没有做任何实质上的转换),即读a的内存时 ...
- 解决You may use special comments to disable some warnings.
问题:运行vue项目出现: You may use special comments to disable some warnings.Use // eslint-disable-next-line ...
- Python前端HTML
一.web标准介绍 web标准: w3c:万维网联盟组织,用来制定web标准的机构(组织) web标准:制作网页遵循的规范 web标准规范的分类:结构标准.表现标准.行为标准. 结构:html.表示: ...
- PHP关于对象访问静态方法、属性等问题
为何有这样的问题呢?源自一段代码,如下: class A { // public static $name = 'wangyumeidsb'; public $name = 'woaini'; pub ...
- UVa 1595 Symmetry (set && math)
题意:给出n个在直角坐标系上的点,问你能不能找出一条竖轴(即垂直于x的轴)使得所有的点根据这条轴对称,能则输出YES,否则输出NO 分析:首先需要找到对称轴的值,将所有n个点的x轴的值加起来然后去除以 ...
- CentOS查看和修改PATH环境变量的方法
查看PATH:echo $PATH以添加mongodb server为列修改方法一:export PATH=/usr/local/mongodb/bin:$PATH//配置完后可以通过echo $PA ...