Composer,Laravel和本地包

Har*_*and 6 php autoload laravel composer-php laravel-4

我的问题是我有一个不是存储库的软件包,我试图让它与Laravel和作曲家玩得很好.它仍然位于供应商文件夹下,唯一的问题是,如果我只是设置:

"psr-0": {
        "Test\\Test": "vendor/test/test/src/"
    }
Run Code Online (Sandbox Code Playgroud)

这将加载服务提供商,但没有控制器等将自动加载.使用没有自己的存储库的larval实现包的正确方法是什么.或者这是否违背了包的性质,这应该只是在应用程序控制器下构建.

该软件包是由我使用workbench创建的,但我发现我并不真的需要将它作为一个单独的存储库,但将它作为一个软件包仍然是好的.因此,结构与常规包完全相同:

vendor
    testvendor
        testpackage
            public
            src
            tests
            .gitignore
            composer.json
            phpunit.xml
Run Code Online (Sandbox Code Playgroud)

更新:

作为我目前正在使用的解决方案:

"autoload": {
    "classmap": [
        "app/commands",
        "app/controllers",
        "app/models",
        "app/database/migrations",
        "app/database/seeds",
        "app/tests/TestCase.php",
        "vendor/package"
    ]
},
Run Code Online (Sandbox Code Playgroud)

作为班级地图中的条目.期待我可能会将其重构到app文件夹中或为此包创建一个存储库.

Ant*_*iro 13

如果你有一些你称之为"包"的类,你不应该将这些文件添加到你的供应商文件夹中.此文件夹由作曲家管理,您可以随时松开它.在您的应用程序中创建一个子文件夹并将这些文件放在那里.

您必须确保PSR-0自动加载适用于文件夹结构中的每个文件.所以,如果你的root是vendor/test/test/src/你的命名空间

Test\\Test
Run Code Online (Sandbox Code Playgroud)

您的所有文件都必须在

vendor/test/test/src/Test/Test/ClassFileName.php
Run Code Online (Sandbox Code Playgroud)

PSR-4更易于处理和理解

"psr-4": {
    "Test\\Test\\": "vendor/test/test/src/"
}
Run Code Online (Sandbox Code Playgroud)

意味着您的文件必须像:

vendor/test/test/src/ClassFileName.php
Run Code Online (Sandbox Code Playgroud)

双击您的命名空间.使用带有PSR-0的命名空间时很容易出错并记住这一点

composer dump-autoload
Run Code Online (Sandbox Code Playgroud)

每次更改composer.json中的内容或创建新文件时都必须运行.如果它是一个简单的类自动加载,每次创建文件时,如果它是PSR-X自动加载,则每次在composer.json文件中创建或更新名称空间时.

如果您拥有的是一个真正的包,那么您应该使用Composer:当您的包被构造为一个composer包时(以Laravel的composer.json为例),将它添加到您的应用程序的正确方法,如果它不是Packagist中的列表,是通过repositories.

您可以在公共VCS存储库中包含(非包装)包:

{
    "require": {
        "monolog/monolog": "dev-bugfix"
    },

    "repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/igorw/monolog"
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

您可以在受密码保护的VCS存储库(git,bitbucket ...)中包含(非包装)包:

{
    "require": {
        "vendor/my-private-repo": "dev-master"
    },
    "repositories": [
        {
            "type": "vcs",
            "url":  "git@bitbucket.org:vendor/my-private-repo.git"
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

您可以将软件包压缩到硬盘驱动器中,然后通过artifact存储库类型加载它们:

"repositories": [
    {
        "type": "artifact",
        "url": "path/to/directory/with/zips/"
    }
],
Run Code Online (Sandbox Code Playgroud)


gan*_*alf 5

Though @Antonio Carlos Ribeiro's answer is really nice, I had problem with installing custom packages locally(which is also stated in the last part of his answer)

Let's assume this is the directory structure of the package we are trying to install:

D:/test_pack
    src/
    composer.json
Run Code Online (Sandbox Code Playgroud)

If you do not want to upload your custom package (that most likely you have developed, yourself) to online repositories you can use one of the following two methods:

Method I

(You have to specify version for your package, otherwise you'll get this error: The requested package could not be found in any version, there may be a typo in the package name.)

1) In composer.json, Add version to your package. your package's json should look something like this:

{
"name": "gandalf/test_pack",//This is your package's name
"description": "some desc",
"version": "1.0.0",//This is the version that you have to specify
"authors": [
    {
        "name": "gandalf the grey",
        "email": "fake@yahoo.com"
    }
],
"minimum-stability": "dev",
"require": {
    "laravel/framework": "~5.4"
},
"autoload": {
    "psr-4": {
        "Gandalf\\BotPack\\": "src/"
    }
} }
Run Code Online (Sandbox Code Playgroud)

2) zip your package(let's assume the zip file is in D:/test_pack/test_packa.zip)

3) In laravel's composer.json add your package name (in our case gandalf/test_pack into require part of json) and add the repository array to the composer.json file and in that array specify the directory in which your package's zip file exists(in our case D:/test_pack) . like this

{
    ...,
    "require": {//adding our package name to laravel's composer.json
        ...,
        "gandalf/test_pack": "*"//package's name
    },
    ...,

    "repositories": [
        {
            "type": "artifact",
            "url": "D:/test_pack"
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

Method II(My Favorite method, You have to initialize your package directory as git local repository using git init and then git add . and git commit -m "your message")

1) initialize the package directory as git directory and commit all your changes to the local repository

(let's say D:/test_pack is the directory that contains your package(src/ directory and composer.json))

go to D:/test_pack directory and run these commands

git init
git add .
git commit -m "your message for this commit"
Run Code Online (Sandbox Code Playgroud)

2) 在你的包composer.json文件中添加 minimum-stability

    {
        "name": "gandalf/test_pack",
        "description": "some desc",
        "authors": [
            {
                "name": "gandalf the grey",
                "email": "fake@yahoo.com"
            }
        ],
        "minimum-stability": "dev",//setting minimum-stability
        "require": {
               //dependencies that your package needs
        },
        "autoload": {
            "psr-4": {
                "Gandalf\\BotPack\\": "src/"
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

3)在laravel的composer.json文件中需要你的包的“dev-master”

{
    ...,
    "require": {
        ...,//some dependencies that laravel needs
        "gandalf/test_pack": "dev-master"//requiring dev-master from repository
    },

    "repositories": [
        {
            "type": "git",
            "url": "D:/test_pack"//path of the local repository directory which contains your package
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)