autoload bitbucket repository + composer.json

时间:2014-04-17 17:19:15

标签: php json git composer-php

我想使用composer将bitbucket存储库添加到我的vendor文件夹中。这就是我在composer.json中所拥有的:

{
    "require": {
        "silex/silex": "~1.1",
        "doctrine/dbal": "2.2.*",
        "twig/twig" : "1.*",
        "symfony/twig-bridge": "~2.3",
        "twitter/bootstrap": "*",
        "symfony/assetic-bundle": "2.1.*",
        "leafo/lessphp": "*",
        "silex/web-profiler": "~1.0",
        "symfony/security": "~2.3",
        "symfony/form": "~2.3",
        "symfony/validator": "~2.3",
        "symfony/config": "~2.3",
        "symfony/translation": "~2.3",
        "monolog/monolog": ">=1.0.0",
        "symfony/yaml": "~2.3",
        "jasongrimes": "dev-master"
    },
    "autoload": {
        "psr-0": { "": "src/" }
    },
    "repositories": [
        {
            "type": "package",
            "package": {
                "name": "jasongrimes",
                "version": "dev-master",
                "source": {
                    "url": "mybitbucketurl",
                    "type": "git",
                    "reference": "origin/master"
                }
            }
        }
    ]
}

我没有在我的bitbucket存储库中有一个composer.json,我想通过composer添加它。现在,当我运行我的应用程序时,我收到以下错误:

Fatal error: Class 'SimpleUser\UserServiceProvider' not found in app/bootstrap.php on line 82

如何确保它也在自动装带器中?

1 个答案:

答案 0 :(得分:17)

当您指定包存储库时,您基本上提供了该包的composer.json中的所有详细信息(如果有)。要使自动加载工作,必须指定程序包的autoload属性。作曲家手册有details on the autoload property

如果您的bitbucket存储库符合PSR-0或PSR-4,您只需指定正确的标准,以及要加载的类存储在存储库中的位置。例如,使用PSR-4和您的类存储在src/目录中:

{
    "require": {
        "jasongrimes": "dev-master"
    },
    "repositories": [
        {
            "type": "package",
            "package": {
                "name": "jasongrimes",
                "version": "dev-master",
                "source": {
                    "url": "mybitbucketurl",
                    "type": "git",
                    "reference": "origin/master"
                },
                "autoload": {
                    "psr-4": { "": "src/" }
                }
            }
        }
    ]
}

否则,您可以使用classmap指定要扫描包含类的.php.inc文件的目录或文件。例如,如果您尝试加载的类位于存储库中的文件SimpleUser/UserServiceProvider.php中:

{
    "require": {
        "jasongrimes": "dev-master"
    },
    "repositories": [
        {
            "type": "package",
            "package": {
                "name": "jasongrimes",
                "version": "dev-master",
                "source": {
                    "url": "mybitbucketurl",
                    "type": "git",
                    "reference": "origin/master"
                },
                "autoload": {
                    "classmap": [ "SimpleUser/UserServiceProvider.php" ]
                }
            }
        }
    ]
}