简体   繁体   English

作曲家需要然后将服务注入Laravel App而不离开laravel命令行

[英]Composer Require then Inject service into Laravel App without leaving laravel command line

So I am making a Laravel artisan command. 因此,我正在制定Laravel工匠指令。 In that command I run 'composer require' for a package. 在该命令中,我为软件包运行“ composer require”。 I am trying to find a way to use that package I required without leaving the current command I am in. 我试图找到一种方法来使用我需要的软件包,而无需离开当前所在的命令。

I thought of temporarily adding The Namespace to Laravel the way it does when you auto load files. 我想到了像自动加载文件时那样将名称空间临时添加到Laravel。

So my first question is how do I use this code below inside of an Artisan Command? 所以我的第一个问题是如何在Artisan Command内部使用下面的代码? Since app in not automatically accessible inside an artisan command. 由于应用程序无法在工匠命令内部自动访问。

include_once __DIR__ .'/../vendor/....';

$this->app->make('My\Namespace');

My Second Question. 我的第二个问题。 Assuming I get the above code to work properly inside an Artisan command, is that to unstable? 假设我在Artisan命令中可以使以上代码正常工作,那是不稳定的吗? Should I just make a second artisan command for after the composer require? 根据作曲家的要求,我是否应该只对工匠做出第二个命令?

Any advice would be appreciated. 任何意见,将不胜感激。

To answer your first question, composer has its own API to do things manually what it does under the hood. 为了回答您的第一个问题,作曲家拥有自己的API ,可以手动执行其幕后工作。

You can required a package, then add the new Class fo usage in classmaps API using its vendor path and dump the autoloads in your command. 您可以需要一个软件包,然后使用其供应商路径在classmaps API中添加新的Class用法,并在命令中转储自动加载。

Secondly, I will suggest to make 2 commands each responsible to do just one task. 其次,我建议执行两个命令,每个命令仅负责执行一项任务。 First to install and handle installation errors, the second to use it. 首先安装并处理安装错误,其次使用它。

Last but importantly, this is an unconventional way of requiring a package. 最后但重要的是,这是要求包装的一种非常规方式。 Because mostly packages are required once and then may be updated/removed in the project. 因为大多数软件包只需要一次,然后可以在项目中进行更新/删除。 So you can install the package manually once and then run the command to eliminate the worry to install package using command. 因此,您可以手动安装软件包一次,然后运行命令来消除使用命令安装软件包的麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM