更新Composer依赖项时Artisan命令出错 [英] Error on Artisan commands when updating Composer dependencies

查看:115
本文介绍了更新Composer依赖项时Artisan命令出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Laravel开发一个包含服务提供者的库.我已将此库添加到另一个项目的composer.json文件中.

I am developing a library for Laravel which contains a service provider. I have added this library to another project's composer.json file.

主项目"的composer.json文件包含以下脚本.

The composer.json file for the "main project" contains the following scripts.

"scripts": {
    "post-root-package-install": [
        "php -r \"copy('.env.example', '.env');\""
    ],
    "post-create-project-cmd": [
        "php artisan key:generate"
    ],
    "post-install-cmd": [
        "php artisan clear-compiled",
        "php artisan optimize"
    ],
    "pre-update-cmd": [
        "php artisan clear-compiled"
    ],
    "post-update-cmd": [
        "php artisan optimize"
    ]
},

除了一件事,我可以很好地包括库依赖关系; pre-update-cmdpost-update-cmd脚本引发错误,并引起我很多头痛.运行sudo composer update更新依赖项时,出现以下错误.

I can include the library dependency just fine, except for one thing; the pre-update-cmd and post-update-cmd scripts throw an error and cause me a lot of headaches. When running sudo composer update to update the dependencies, I get the following error.

$ sudo composer update
> php artisan clear-compiled
PHP Fatal error:  Class 'MyName\MyProject\MyAwesomeServiceProvider' not found in /Users/Me/dev/MyProject/vendor/laravel/framework/src/Illuminate/Foundation/ProviderRepository.php on line 146


  [Symfony\Component\Debug\Exception\FatalErrorException]                                              
  Class 'MyName\MyProject\MyAwesomeServiceProvider' not found  


Script php artisan clear-compiled handling the pre-update-cmd event returned with an error


  [RuntimeException]                                                                                         
  Error Output: PHP Fatal error:  Class 'MyName\MyProject\MyAwesomeServiceProvider' 
  not found in /Users/Me/dev/MyProject/vendor/laravel/framework/src/Illuminate/Foundation/ProviderRepository.php on line 146

在问这个问题之前,我已经在Google周围搜索了很多,并通读了我能找到的几乎所有相关内容.显然,这是一个已知问题,已在Laravel存储库中的多个GitHub问题中进行了讨论.但是,即使尝试了多个方法,我仍未找到解决方法.

I have Googled around quite a bit before asking this question and read through pretty much everything related that I could find. Apparently this is a known issue that has been discussed in multiple GitHub issues within the Laravel repository. However, I have yet to find a workaround, even after having tried multiple ones.

问题似乎是Artisan命令引导了Laravel,这会导致错误,因为此时服务提供者不可用-或类似的东西.将clear-compiled命令移至post-update-cmd会导致相同的错误,这使我有些惊讶,因为我认为此时服务提供商将可用.

It appears that the issue is that the Artisan commands bootstrap Laravel, which leads to an error because the service provider is not available at this point - or something like that. Moving the clear-compiled command to post-update-cmd causes the same error, which surprises me a bit because I thought the service provider would be available at this point.

对我唯一有用的是在运行composer update之前手动注释掉config/app.php中包含服务提供程序的行,然后再添加它.我已经这样做了几个小时,它已经使我烦恼了,我真的不敢相信这个问题已经到来.

The only thing that works for me is to manually comment out the line that includes the service provider in config/app.php before running composer update and then adding it again afterwards. I have been doing this for a few hours, and it is already bothering the heck out of me, and I really cannot believe that this issue is around.

有人知道如何解决此错误,以便在更新项目的Composer依赖项时不会出现未找到我的服务提供商的错误吗?

Does anyone know how to work around this error so that I don't get the error that my service provider is not found when updating the Composer dependencies for my project?

这是该库的composer.json文件.

{
    "name": "my-name/my-project",
    "type": "library",
    "authors": [
        {
            "name": "My Name",
            "email": "test@example.com"
        }
    ],
    "require": {
        "php": ">=5.5.0",
        "laravel/framework": "~5.2"
    },
    "autoload": {
        "classmap": [],
        "psr-4": {
            "MyName\\MyProject\\": "src/"
        }
    }
}

推荐答案

编辑

此问题终于从laravel/framework:v5.2.25laravel/laravel:v5.2.27开始解决,并反向移植到laravel/framework:v5.1.33laravel/laravel:v5.1.33.

Edit

This issue has finally been resolved as of laravel/framework:v5.2.25 and laravel/laravel:v5.2.27, and backported to laravel/framework:v5.1.33 and laravel/laravel:v5.1.33.

此修复程序包括对Laravel应用程序(laravel/laravel)的更改,以及Laravel框架(laravel/framework).要实施,您将需要:

This fix includes a change to the Laravel application (laravel/laravel), in addition to the Laravel Framework (laravel/framework). To implement, you will need to:

1)更新composer.json文件的scripts部分,使其与 laravel/laravel软件包.具体来说:

1) Update the scripts section of your composer.json file to match that in the laravel/laravel package. Specifically:

  • 删除pre-update-cmd部分
  • post-install-cmd部分中,将"php artisan clear-compiled"替换为"Illuminate\\Foundation\\ComposerScripts::postInstall"
  • post-update-cmd部分中,将"php artisan clear-compiled"替换为"Illuminate\\Foundation\\ComposerScripts::postUpdate"
  • remove the pre-update-cmd section
  • in the post-install-cmd section, replace "php artisan clear-compiled" with "Illuminate\\Foundation\\ComposerScripts::postInstall"
  • in the post-update-cmd section, replace "php artisan clear-compiled" with "Illuminate\\Foundation\\ComposerScripts::postUpdate"

2)更新composer.json后,运行composer update.如果只想更新框架,则可以运行composer update laravel/framework.

2) Once you have updated your composer.json, run a composer update. If you only want to update the framework, you can run composer update laravel/framework.

查看了您在评论中发布的 Github问题相关问题,您可能需要等待一段时间.泰勒想在vendor/bin中放置脚本并更改composer.json来运行该脚本,但是看起来他们正在等待社区的PR,实际上他们自己不会实现.

After looking over the Github issue you posted in the comments, as well as the related issues, you may be in for a bit of a wait. Taylor would like to put a script in vendor/bin and change composer.json to run that, but it looks like they are waiting for a PR from the community, and won't actually implement this themselves.

您没有做错任何事;您的自动加载设置正确.问题出在Laravel.

You haven't done anything wrong; your autoloading is setup correctly. The issue is with Laravel right now.

将命令移动到post-update-cmd脚本不起作用,因为技术人员将始终尝试在缓存文件存在时加载它们.运行clear-compiled命令时,artisan在尝试删除高速缓存文件之前将其加载(启动的一部分).

Moving the command to the post-update-cmd script doesn't work because artisan will always try to load the cache files when they exist. When running the clear-compiled command, artisan loads the cache files (part of startup) before it ever tries to delete them.

最好的选择是在artisan运行之前手动删除缓存文件.并且,您需要在Laravel/Artisan之外进行操作.因此,您可以手动删除文件,也可以创建一个小脚本来将其添加到您的composer.json文件中(对于您的主项目,而不是您的软件包).

Your best bet is to manually delete the cache files before artisan gets run. And, you need to do it outside of Laravel/Artisan. So, you can manually delete the files, or you can create a little script to do it and add that to your composer.json file (for your main project, not your package).

要删除的文件:

  • Laravel 5.2:
    bootstrap/cache/compiled.php
    bootstrap/cache/services.php
  • Laravel 5.1:
    bootstrap/cache/compiled.php
    bootstrap/cache/services.json
  • Laravel 5.0:
    vendor/compiled.php
    storage/framework/compiled.php
    vendor/services.json
    storage/framework/services.json
  • Laravel 5.2:
    bootstrap/cache/compiled.php
    bootstrap/cache/services.php
  • Laravel 5.1:
    bootstrap/cache/compiled.php
    bootstrap/cache/services.json
  • Laravel 5.0:
    vendor/compiled.php
    storage/framework/compiled.php
    vendor/services.json
    storage/framework/services.json

这篇关于更新Composer依赖项时Artisan命令出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆