laravel 更新 Composer 依赖项时 Artisan 命令出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35026380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:07:08  来源:igfitidea点击:

Error on Artisan commands when updating Composer dependencies

phplaravellaravel-5

提问by ba0708

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

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

The composer.jsonfile for the "main project" contains the following scripts.

composer.json“主项目”的文件包含以下脚本。

"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"
    ]
},

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

我可以很好地包含库依赖项,除了一件事;在pre-update-cmdpost-update-cmd脚本抛出一个错误,并导致了我很多麻烦。运行sudo composer update更新依赖项时,出现以下错误。

$ 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

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.

在问这个问题之前,我在谷歌上搜索了很多,并通读了我能找到的几乎所有相关内容。显然,这是一个已知问题,已在 Laravel 存储库中的多个 GitHub 问题中讨论过。但是,我还没有找到解决方法,即使在尝试了多个方法之后。

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-compiledcommand to post-update-cmdcauses the same error, which surprises me a bit because I thought the service provider would be available at this point.

似乎问题在于 Artisan 命令引导 Laravel,这会导致错误,因为此时服务提供者不可用 - 或类似的东西。将clear-compiled命令移到post-update-cmd会导致相同的错误,这让我感到有些惊讶,因为我认为此时服务提供程序可用。

The only thing that works for me is to manually comment out the line that includes the service provider in config/app.phpbefore running composer updateand 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.

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

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 依赖项时不会收到找不到我的服务提供商的错误?

EDIT:Here is the composer.jsonfile for the library.

编辑:这是composer.json库的文件。

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

采纳答案by patricus

Edit

编辑

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

这个问题终于在laravel/framework:v5.2.25和 中得到解决laravel/laravel:v5.2.27,并反向移植到laravel/framework:v5.1.33laravel/laravel:v5.1.33

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:

laravel/laravel除了 Laravel 框架 ( laravel/framework)之外,此修复还包括对 Laravel 应用程序 ( )的更改。要实施,您需要:

1) Update the scriptssection of your composer.jsonfile to match that in the laravel/laravelpackage. Specifically:

1) 更新文件的scripts部分composer.json以匹配laravel/laravel包中的部分。具体来说:

  • remove the pre-update-cmdsection
  • in the post-install-cmdsection, replace "php artisan clear-compiled"with "Illuminate\\Foundation\\ComposerScripts::postInstall"
  • in the post-update-cmdsection, replace "php artisan clear-compiled"with "Illuminate\\Foundation\\ComposerScripts::postUpdate"
  • 删除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"

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.

2)一旦你更新了你的composer.json,运行一个composer update. 如果只想更新框架,可以运行composer update laravel/framework.



Original

原来的

After looking over the Github issueyou 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/binand change composer.jsonto run that, but it looks like they are waiting for a PR from the community, and won't actually implement this themselves.

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

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

你没有做错任何事;您的自动加载设置正确。现在的问题是 Laravel。

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

将命令移到post-update-cmd脚本中不起作用,因为 artisan 总是会在缓存文件存在时尝试加载它们。运行clear-compiled命令时,artisan 会在尝试删除缓存文件之前加载缓存文件(启动的一部分)。

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.jsonfile (for your main project, not your package).

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

Files to delete:

要删除的文件:

  • 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

回答by Lucas Silva

When using composer installor composer updateyou can use --no-scriptsoption to skips execution of scripts defined in composer.json.

使用composer installcomposer update您可以使用--no-scripts选项跳过在 composer.json 中定义的脚本的执行。

e. g.: composer update --no-scripts.

例如:composer update --no-scripts

Source: https://getcomposer.org/doc/03-cli.md#install

来源:https: //getcomposer.org/doc/03-cli.md#install

回答by Sven

It looks like you're simply not including your ServiceProvider. Put this in your root project's composer.json:

看起来您只是不包括您的 ServiceProvider。把这个在你的根项目composer.json

{
    "autoload": {
        "psr-4": {
            "App\": "app/",
            "MyName\MyProject\": "../relative/path/to/serviceprovider/"
        }
    }
}

回答by Raymond Cheng

composer update --no-scripts

this command will ignore command defined in composer.json,otherwise it will excute laravel command which will check if serviceProvider is loaded.

此命令将忽略在 composer.json 中定义的命令,否则它将执行 laravel 命令,该命令将检查 serviceProvider 是否已加载。

回答by Bamba

Run This

运行这个

composer install --ignore-platform-reqs