Composer、Laravel 和本地包

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22509731/
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 09:14:00  来源:igfitidea点击:

Composer, Laravel and local packages

phplaravellaravel-4composer-phpautoload

提问by Hard-Boiled Wonderland

My issue is I have a package which isn't a repository and I am trying to get it to play nice with Laravel and composer. It is still located under the vendor folder, the only issue is that if I simply set:

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

"psr-0": {
        "Test\Test": "vendor/test/test/src/"
    }

This will load the service provider but none of the controllers etc will autoload. What is the correct way to implement a package with larval that does not have it's own repository. Or does this go against the nature of packages and this should simply be structured under the applications controllers.

这将加载服务提供者,但不会自动加载任何控制器等。使用没有自己的存储库的幼虫实现包的正确方法是什么。或者这是否违背了包的性质,这应该简单地在应用程序控制器下构建。

The package was created by me using workbench but I found i did not really need this as a separate repository but it would still be good to keep it as a package. Therefore the structure is exactly the same as a regular package:

该包是我使用工作台创建的,但我发现我并不真正需要它作为单独的存储库,但将它作为一个包保留仍然很好。因此结构与常规包完全相同:

vendor
    testvendor
        testpackage
            public
            src
            tests
            .gitignore
            composer.json
            phpunit.xml

UPDATE:

更新:

As a solution for the time being I am using:

作为暂时的解决方案,我正在使用:

"autoload": {
    "classmap": [
        "app/commands",
        "app/controllers",
        "app/models",
        "app/database/migrations",
        "app/database/seeds",
        "app/tests/TestCase.php",
        "vendor/package"
    ]
},

As an entry in the class map. Looking forward I will probably refactor this into the app folder or create a repository for this package.

作为类映射中的条目。展望未来,我可能会将其重构到 app 文件夹中或为此包创建存储库。

回答by Antonio Carlos Ribeiro

If you have some classes that you're calling "package", you're not supposed to add those files to your vendor folder. This folder is managed by composer and at any time you might loose it. Create a subfolder in your application and put those files there.

如果您将某些类称为“包”,则不应将这些文件添加到供应商文件夹中。该文件夹由 Composer 管理,您随时可能会丢失它。在您的应用程序中创建一个子文件夹并将这些文件放在那里。

You have to be sure your PSR-0 autoloading will work for every single file in your folder structure. So, if your root is vendor/test/test/src/and your namespace is

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

Test\Test

All your files must be in

您的所有文件都必须在

vendor/test/test/src/Test/Test/ClassFileName.php

PSR-4 is easier to deal and understand, this

PSR-4 更容易处理和理解,这个

"psr-4": {
    "Test\Test\": "vendor/test/test/src/"
}

Means that your files would have to be like:

意味着您的文件必须是这样的:

vendor/test/test/src/ClassFileName.php

Doublecheck your namespaces. It's easy to make mistakes when using namespaces with PSR-0 and remember that

仔细检查您的命名空间。使用带有 PSR-0 的命名空间时很容易出错,请记住

composer dump-autoload

Must be ran every time you change things in composer.json or create new files. If it's a simple class autoloading, every time you create a file, if it's a PSR-X autoloading, everytime you create or update a namespace in your composer.json file.

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

If what you have is is really a package you should use Composer: when your package is structured as a composer package (check Laravel's composer.json as an example), the correct way of adding it to your application, if it's not list in Packagist, is via repositories.

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

You can have (non-packagist) packages in a public VCS repository:

您可以在公共 VCS 存储库中拥有(非打包者)包:

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

    "repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/igorw/monolog"
        }
    ]
}

You can have (non-packagist) packages in a protected by password VCS repository (git, bitbucket...):

您可以在受密码保护的 VCS 存储库(git、bitbucket...)中拥有(非打包者)包:

{
    "require": {
        "vendor/my-private-repo": "dev-master"
    },
    "repositories": [
        {
            "type": "vcs",
            "url":  "[email protected]:vendor/my-private-repo.git"
        }
    ]
}

You can have your packages zipped in your hard drive and load them via the artifactrepository type:

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

"repositories": [
    {
        "type": "artifact",
        "url": "path/to/directory/with/zips/"
    }
],

回答by gandalf

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)

尽管@Antonio Carlos Ribeiro 的回答非常好,但我在本地安装自定义软件包时遇到了问题(这也在他回答的最后一部分中有所说明)

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

让我们假设这是我们尝试安装的包的目录结构:

D:/test_pack
    src/
    composer.json

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:

1) 在 composer.json 中,将版本添加到您的包中。你的包的 json 应该是这样的:

{
"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": "[email protected]"
    }
],
"minimum-stability": "dev",
"require": {
    "laravel/framework": "~5.4"
},
"autoload": {
    "psr-4": {
        "Gandalf\BotPack\": "src/"
    }
} }

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

2) 压缩你的包(假设 zip 文件在 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

3)在laravel的composer.json中添加你的包名(在我们的例子中gandalf/test_pack到json的require部分)并将存储库数组添加到composer.json文件中,并在该数组中指定包的zip文件所在的目录(在我们的例子中 D:/test_pack) 。像这样

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

    "repositories": [
        {
            "type": "artifact",
            "url": "D:/test_pack"
        }
    ]
}

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

方法二(我最喜欢的方法,你必须使用git initand then git add .and将你的包目录初始化为git本地存储库git commit -m "your message"

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

1) 将包目录初始化为 git 目录并将所有更改提交到本地存储库

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

(假设D:/test_pack是包含您的包的src/目录(目录和composer.json))

go to D:/test_packdirectory and run these commands

转到D:/test_pack目录并运行这些命令

git init
git add .
git commit -m "your message for this commit"

2) In your packages composer.jsonfile add minimum-stability

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

    {
        "name": "gandalf/test_pack",
        "description": "some desc",
        "authors": [
            {
                "name": "gandalf the grey",
                "email": "[email protected]"
            }
        ],
        "minimum-stability": "dev",//setting minimum-stability
        "require": {
               //dependencies that your package needs
        },
        "autoload": {
            "psr-4": {
                "Gandalf\BotPack\": "src/"
            }
        }
    }

3)In laravel's composer.json file require the "dev-master" of your package

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