我正在学习如何制作作曲家包。到目前为止,我已经这样做了:
作曲家.json
{
"name": "Iv/MyPackage",
"autoload": {
"psr-4": {
"Iv\\MyPackage\\": "src/"
}
},
"require": {
"guzzlehttp/guzzle": "~6.0"
},
"require-dev": {
"phpunit/phpunit": "4.*",
"guzzlehttp/guzzle": "~6.0"
},
"autoload-dev": {
"psr-4": {
"Iv\\MyPackage\\Tests\\": "tests/"
}
}
}
我在命名空间下有一个类:namespace Iv\MyPackage\Api;
被调用Consumer
并且在它的__construct
方法中它有这个:
public function __construct(array $credentials)
{
$this->client = new Client();
$this->credentials = $credentials;
}
在那个班级的顶部,我有use GuzzleHttp\Client;
。
我得到的错误是:
Fatal error: Class 'GuzzleHttp\Client' not found in path\to\package\Iv\MyPackage\src\Api\Consumer.php on line 27
当我这样做时:
$package = new Iv\MyPackage\Api\Consumer(['user', 'password']);
$query = $api->prepare('/api-endpoint', 'GET');
编辑:
这是我的Consumer
课的样子:
<?php
namespace Iv\MyPackage\Api;
use GuzzleHttp\Client;
class Consumer
{
private $credentials = [];
public function __construct(array $credentials)
{
$this->client = new Client();
$this->credentials = $credentials;
}
...
}
我也在使用 PhpStorm,它告诉我我有这个GuzzleHttp
包,因为当我输入Client()
并按ALT+时它会自动为我导入它ENTER。这意味着我已经跑了composer install/update
。
编辑 2:我有一个文件 -index.php
其中包含以下内容:
<?php
include('vendor/autoload.php');
$api = new Iv\MyPackage\Api\Consumer(['user', 'password']);
$query = $api->prepare('/endpoint', 'GET');
var_dump($api->execute($query));
文件夹结构如下:
-Iv/
--MyPackage/
---src/
----Api/
----Exceptions/
----vendor/
----tests/
----otherfiles (composer.json, phpunit.xml, etc)
-vendor/
-composer.json
-index.php (I mentioned above)
编辑3:我的内容vendor/composer/autoload_psr4.php
:
<?php
// autoload_psr4.php @generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'Symfony\\Component\\Yaml\\' => array($vendorDir . '/symfony/yaml'),
'Psr\\Http\\Message\\' => array($vendorDir . '/psr/http-message/src'),
'Iv\\MyPackage\\Tests\\' => array($baseDir . '/tests'),
'Iv\\MyPackage\\' => array($baseDir . '/src'),
'GuzzleHttp\\Psr7\\' => array($vendorDir . '/guzzlehttp/psr7/src'),
'GuzzleHttp\\Promise\\' => array($vendorDir . '/guzzlehttp/promises/src'),
'GuzzleHttp\\' => array($vendorDir . '/guzzlehttp/guzzle/src'),
'Doctrine\\Instantiator\\' => array($vendorDir . '/doctrine/instantiator/src/Doctrine/Instantiator'),
);
编辑 4:
在index.php
我有这个作曲家的文件夹中:
{
"name": "Test MyPackage",
"autoload": {
"psr-4": {"Iv\\MyPackage\\": "Iv/MyPackage/src"}
}
}
^^ 我在一个主题中看到,OP 在将其上传到 packagist/git 之前询问如何测试他的包。我可以加载Consumer
类,没有错误,但没有找到每个依赖项。( GuzzleHttp\Client
, Symfony\Component\Yaml\Parser
, 等)