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的更多相关文章

  1. 使用laravel 的artisan快速创建表

    参考:使用laravel 的artisan快速创建表 字段类型参考链接: 结构生成器 版本: Laravel 4.2 1. 创建migrate 文件 php artisan migrate:make ...

  2. Laravel 的Artisan 命令学习

    Laravel 的Artisan 命令学习 Artisan 是 Laravel 提供的 CLI(命令行接口),它提供了非常多实用的命令来帮助我们开发 Laravel 应用.前面我们已使用过 Artis ...

  3. 源码解读 Laravel PHP artisan config:cache

    来源 https://laravel-china.org/articles/5101/source-code-reading-laravel-php-artisan-configcache 源码在哪 ...

  4. Laravel 之Artisan

    简介: Artisan是Laravel中自带的命令行工具的名称: 由强大的Symfony Console组件驱动的: 提供了一些对应用开发有帮助的命令: 查看所有可用的Artisan的命令 php a ...

  5. (1) laravel php artisan list make

    php artisan list make Laravel Framework 5.4.36 Usage: command [options] [arguments] Options: -h, --h ...

  6. Laravel使用artisan快速实现表单的登陆注册

    1. 开发环境 macOS Mojave 10.14.6 XAMPP 5.6.38 Laravel 5.2 2. 在终端,先进入到项目根目录并执行执行命令 php artisan make:auth ...

  7. laravel 使用artisan命令新增数据库字段

    php artisan make:migration create_comments_table <?php use Illuminate\Database\Schema\Blueprint; ...

  8. laravel php artisan migrate 数据迁移时出现的[HY000][1045]错误

    (zz找了块一个小时才发现)主要的错误在于.env文件和database.php的配置不匹配. 1.找到.env文件 2.更改数据库表账密 3.改database.php的数据库账密 4.完成

  9. laravel中artisan的用法

    如:

随机推荐

  1. zencart移站后批量替换数据库中网址、电子邮箱、重置用户密码

    -- SEO标签中网址替换 update categories_description set categories_description=replace(categories_descriptio ...

  2. uestc summer training #3 线段树优化建边

    线段树建边 struct E { int value, modvalue; } a[MAXN << ]; pair<int, int> b[MAXN]; ], r[MAXN & ...

  3. WinMain lpCmdLine

    int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE, LPSTR lpCmdLine, int){ //命令行参数 TCHAR pCommandLine[2 ...

  4. @Mapper和@Repository的区别

    参考博客地址 https://www.cnblogs.com/wangshen31/p/8735037.html 相同点 两个都是注解在Dao上 不同 @Repository需要在Spring中配置扫 ...

  5. PM、RD、QA、OP、CM、EPG 英文缩写是什么意思?

    1.PM: Product Manager,产品经理,又称品牌经理.举凡产品从创意到上市,所有相关的研发.调研.生产.编预算.广告.促销活动等等,都由产品经理掌控. 2.RD: Research an ...

  6. 安装nodejs与使用

    nodejs 官方下载地址:https://nodejs.org/en/ 下载完成后,双击打开安装程序 然后: 然后点击install,等待安装 安装完成后的目录如下: 检测是否真的安装成功.打开cm ...

  7. Python中self的用法详解,或者总是提示:TypeError: add() missing 1 required positional argument: 'self'的问题解决

    https://blog.csdn.net/songlh1234/article/details/83587086 下面总结一下self的用法详解,大家可以访问,可以针对平时踩过的坑更深入的了解下. ...

  8. Python&R:警告信息管理

    计算机程序有时很人性化,比如给你警告提示信息: 计算机程序有时又非常不人性化,比如动不动就给你警告提示...... 如果你的程序是要给客户使用,有运行美化要求: 再尤其是比如警告出现在循环里的情况,那 ...

  9. springboot 开启缓存

    Caching Data with Spring This guide walks you through the process of enabling caching on a Spring ma ...

  10. 【leetcode】1248. Count Number of Nice Subarrays

    题目如下: Given an array of integers nums and an integer k. A subarray is called nice if there are k odd ...