Laravel 5.1软件包开发-在开发中加载软件包依赖项 [英] Laravel 5.1 Package Development - loading packages dependencies in development

查看:93
本文介绍了Laravel 5.1软件包开发-在开发中加载软件包依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Laravel 5.1中开发一个软件包.感谢这里的帮助,我已经设置了基础知识.

Hi I'm trying to develop a package in Laravel 5.1. Thanks to help here I have the basics set up.

我当前的问题是在开发程序包时如何为程序包加载依赖项.

My current problem is how to load dependencies for the package while I'm developing it.

在软件包composer.json中,我添加了依赖项,并将其现在安装在我的软件包开发文件夹中的供应商文件夹中.这不是框架的根供应商文件夹.

In the packages composer.json I have added dependencies and have these installed now in a vendor folder within my packages development folder. This is not the frameworks root vendor folder.

这是软件包composer.json的我的必填部分:

Here's my require section of the packages composer.json:

"require": {
"illuminate/support": "~5.1",
"php" : ">=5.3.0",
"google/apiclient": "dev-master"
},

因为它们不是自动加载过程的一部分,所以什么是最好的方法来确保从开发文件夹中正确加载我的程序包的依赖项?如何包含自动加载功能?我担心如果我将它们引用到它们的当前位置/名称空间,该名称/位置空间以后作为软件包安装在另一个应用程序中时会中断.

Because they are not part of the main autoload process what is the best approach to ensuring the dependencies for my package are loaded correctly from within the development folder? How do I include the autoload? I'm concerned that if I reference them to their current location/namespace that it will break when later installed as a package in another app.

在我的代码中,我有以下内容:

in my code I have the following:

$client = new \Google_Client($config);

出现错误:

Class 'Google_Client' not found

我可以通过在主composer.json文件中添加此依赖项来解决这个问题,但认为这不是保持软件包开发独立性的正确方法(如果有意义)

I can get round this by adding this dependency to the main composer.json but don't think that is the correct approach to keep the package development independent (if that makes sense)

当我在L4.2中进行开发时,有一个工作台来负责加载工作,而该工作台当然不再在L5.1中起作用

When I developed in L4.2 there was the workbench which took care of the loading which of course no longer features in L5.1

感谢任何帮助和最佳做法

Any help and best practice appreciated

推荐答案

因为它们不是自动加载主要过程的一部分

Because they are not part of the main autoload process

我认为您误解了如何管理作曲家依赖性.当在主compose.json文件中列出一个依赖项时,composer会将其及其所有依赖项及其依赖项的依赖项,以递归方式添加到主自动加载过程中.

I think you misunderstood how composer dependencies are managed. When in your main compose.json file you list a dependency, composer will add it to the main autoload process as well as all their dependencies, and the dependencies of their dependencies, and so on recursively.

您不必担心依赖项的存储位置或Composer如何加载它们. Composer会自动将它们添加到自动加载文件中,并且所有要做的就是确保您需要composer自动加载文件.一旦您需要composer自动加载文件,composer加载的所有类和函数都将可用.只要您需要composer自动加载文件,使用任何已安装软件包中的类所要做的就是确保您使用正确的名称空间对它们进行寻址. Composer非常聪明,可以知道所有类的存储位置以及如何加载它们(即psr-0,psr-4,...的用途).

You don't have to worry about where the dependencies are stored or how Composer will load them. Composer will automatically add them to the autoload file and all you have to do is make sure you require the composer autoload file. Once you require the composer autoload file, all the classes and functions loaded by composer will be available. Provided you required the composer autoload file all you have to do to use the classes from any of the installed packages is to make sure you address them using the proper namespace. Composer is smart enough to know where all classes are stored and how to load them (that is what psr-0, psr-4,... are for).

因此,如果您正在开发Composer软件包,则将其称为"A",并将软件包"C"列为软件包"A"的依赖项之一,composer会为您将其添加到自动加载文件中.如果您使用另一个具有Laravel依赖关系的软件包,例如Laravel,它与您的依赖关系为'A',那么由于'A'的依赖关系,因此在Laravel中也可以使用软件包'C'.

So if you are developing a Composer package, lets call it 'A', and you list the package 'C' as one of the dependencies of your package 'A', composer will add it to the autoload file for you. If you use another package, lets say, Laravel, which has a dependency of you package 'A', then also the package 'C' will be available within Laravel, since it is a dependency of 'A'.

即:如果这是您的composer.json文件

I.e: If this is your composer.json file

{
    "name": "foo/bar",
    "require": {
        "google/apiclient": "1.0.*"
    }
}

此代码将起作用

require_once __DIR__ . '/vendor/autoload.php';
$client = new Google_Client();
$youtube = new Google_Service_YouTube($client);

注意,我需要composer自动加载文件,这似乎是您的问题.当您使用Laravel时,它将为您添加该文件.

Note I've required the composer autoload file, which seems to be your problem. When you are using Laravel, it will add that file for you.

这篇关于Laravel 5.1软件包开发-在开发中加载软件包依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆