Eloquent提供程序位于供应商中的composer包中,在开发环境中无法自动加载。

huangapple go评论55阅读模式
英文:

Eloquent provider inside composer package in vendor not auto-loading on a dev environment

问题

I'm developing a composer package. I've done this before, but I don't know why at this point I'm not getting composer to figure out about my file in the package when building auto-loading.

The purpose of the package is to add a listener in Laravel's Eloquent to log queries to a file. However, for this I need to make the system execute a provider boot method right at the beginning of the system load.

The idea is for the package to be installed only in development environment, to facilitate query improvement without the danger of uploading unnecessary code to production. I've tried some variations, making use of "classmap", "file" and others, all within "autoload-dev" but all without success.

I have the perception that in addition to not discovering the files, the "providers" item (which would actually make it load the provider) would also be incorrect, but I couldn't get to this point to validate.

My environment in the project where I'm testing the package installation is set to "APP_ENV=local".

Below is the latest version of the attempted change I made to the "composer.json" file inside the package, directly in the vendor directory of the test project. The package being installed is available at

.

英文:

I'm developing a composer package. I've done this before, but I don't know why at this point I'm not getting composer to figure out about my file in the package when building auto-loading.

The purpose of the package is to add a listener in Laravel's Eloquent to log queries to a file. However, for this I need to make the system execute a provider boot method right at the beginning of the system load.

The idea is for the package to be installed only in development environment, to facilitate query improvement without the danger of uploading unnecessary code to production. I've tried some variations, making use of "classmap", "file" and others, all within "autoload-dev" but all without success.

I have the perception that in addition to not discovering the files, the providers item (which would actualy make it to load the provider) would also be incorrect, but I couldn't get to this point to validate.

My environment in the project where I'm testing the package installation is set to APP_ENV=local.

Below is the latest version of the attempted change I made to the composer.json file inside the package, directly in the vendor directory of the test project. The package being installed is available at https://github.com/rstriquer/eloquent-logger/tree/bc32616a0dcf727e7385059ef523c3dcb41881fa (link to v1.0.0-rc2 tag)

{
  "name": "rstriquer/eloquent-logger",
  "description": "Write SQL queries to a file into Laravel \"storage/logs\" directory.",
  "keywords": ["laravel", "eloquent", "logger", "debugger", "laravel-dev"],
  "license": "MIT",
  "support": {
      "issues": "https://github.com/rstriquer/eloquent-logger/issues",
      "source": "https://github.com/rstriquer/eloquent-logger"
  },
  "authors": [
      {
          "name": "Ricardo Soares",
          "email": "rstriquer@gmail.com"
      }
  ],
  "require": {
      "php": ">7.0",
      "laravel/framework": ">8.0"
  },
  "require-dev": {
      "laravel/pint": "^1.1.1",
      "mockery/mockery": "^1.4.4",
      "phpstan/phpstan": "^1.4.6",
      "phpunit/phpunit": "^9.5.23",
      "fakerphp/faker": "^1.9.2"
  },
  "autoload-dev": {
      "classmap": [
        "vendor/rstriquer/eloquent-logger/src/Adapters/Laravel/DatabaseServiceProvider.php"
      ],
      "psr-4": {
        "RStriquer\\EloquentLogger\\Adapters\\Laravel": "src/Adapters/Laravel",
        "RStriquer\\EloquentLogger\\Tests\\Unit\\": "tests/Unit"
      }
  },
  "minimum-stability": "dev",
  "prefer-stable": true,
  "autoload": {
      "psr-4": {
          "RStriquer\\EloquentLogger\\": "src/"
      }
  },
  "config": {
      "preferred-install": "prod",
      "sort-packages": true
  },
  "extra": {
      "laravel": {
          "providers": [
              "RStriquer\\EloquentLogger\\Adapters\\Laravel\\DatabaseServiceProvider"
          ]
      }
  },
  "scripts": {
      "lint": "pint -v",
      "test:lint": "pint --test -v",
      "test:types": "phpstan analyse --ansi",
      "test:unit": "phpunit --colors=always",
      "test": [
          "@test:lint",
          "@test:types",
          "@test:unit"
      ]
  }
}

PS: Real thanks in advance to everyone who makes the effort to help. I am much obliged!

答案1

得分: 1

I identified what was going on.

我确定了发生了什么。

I don't know if I can call it a bug, I'll study a little more and if that's the case, open an issue on the composer project's GitHub for discussion later, but here are my findings.

我不确定是否可以称其为一个错误,我会再仔细研究一下,如果是这样的话,稍后会在composer项目的GitHub上开一个问题进行讨论,但以下是我的发现。

What was happening is that I was doing installation tests with a beta version of the package, so it wasn't loading. I believe that as the package was being loaded from a GitHub repository it was not able to perform an autoloading, even in the development environment. Maybe because of some security restriction.

发生的情况是,我正在使用包的beta版本进行安装测试,所以它没有加载。我认为因为该包是从GitHub存储库加载的,所以即使在开发环境中也无法执行自动加载,可能是因为某些安全限制。

To solve it I simply published the package on getcomposer.com and the problem was resolved.

为了解决这个问题,我只需将包发布到getcomposer.com,问题就解决了。

英文:

I identified what was going on.

I don't know if I can call it a bug, I'll study a little more and if that's the case, open an issue on the composer project's github for discussion later, but here are my findings.

What was happening is that I was doing installation tests with a beta version of the package, so it wasn't loading. I believe that as the package was being loaded from a github repository it was not able to perform an autoloading, even in the development environment. Maybe because of some security restriction.

To solve it I simply published the package on getcomposer.com and the problem was resolved.

huangapple
  • 本文由 发表于 2023年4月20日 04:07:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76058428.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定