I'm trying to find the base directory of my application from within a composer package. Is there an composer API or proper way to accomplish this.
我正在尝试从Composer包中查找我的应用程序的基本目录。是否有Composer API或适当的方法来实现这一点。
To clarify, if my package install looks like this and I've got files here (using psr-4):
为了说明这一点,如果我的包安装如下所示,并且我在这里有文件(使用PSR-4):
/home/project/vendor/Acme/my-package/src/
how can I find /home/project dynamically?
如何动态查找/home/project?
Updated Info:
更新信息:
I'm trying to load a .env file from the root which contains an API URL endpoint via Dotenv package from within the package.
我试图从根目录加载一个.env文件,该文件包含一个API URL端点,通过Dotenv包从包内加载。
更多回答
I wonder why you'd need the physical location of the project you are contained inside. This somehow states a dependency on the outside world, and probably should be handled by requiring the user of your package to provide that path instead of autodetecting it.
我想知道为什么你需要你所在的项目的实际位置。这在某种程度上说明了对外部世界的依赖,可能应该通过要求包的用户提供该路径而不是自动检测来处理。
Updated question with more info.
更新问题,提供更多信息。
Based in your input I'm seeing that you're suggesting I should just load the data on demand via constructor.. this is an easier solution
根据您的输入,我看到您建议我应该通过构造函数按需加载数据。这是一个更简单的解决方案
That's likely the better solution. If you want to reuse your code (if you don't, why do you put it into a composer package then?), the next project that uses it won't have .env files somewhere, but a completely different configuration system. Ask for the information you need to do your work: Strings, integer, floats, booleans from "the configuration" should be read from wherever and passed as a parameter either into a factory or the constructor.
这可能是更好的解决方案。如果您想要重用您的代码(如果您不想这样做,那么为什么要把它放到一个Composer包中呢?),使用它的下一个项目将不会在某个地方有.env文件,而是一个完全不同的配置系统。询问完成工作所需的信息:应该从任何地方读取“配置”中的字符串、整数、浮点数、布尔值,并将其作为参数传递到工厂或构造函数中。
优秀答案推荐
This should do:
这应该可以做到:
<?php
$root = null;
// Set the current directory.
// Make sure you set this up so
// that you get out of your own root.
// Assuming this php file is at the root
// of this composer package, this should suffice.
$directory = dirname(__FILE__);
// Go up until you find a composer.json file
// which should exist in the ancestors
// because its a composer package.
do {
$directory = dirname($directory);
$composer = $directory . '/composer.json';
if(file_exists($composer)) $root = $directory;
} while(is_null($root) && $directory != '/');
// We either are at the root or we got lost.
// i.e. a composer.json was nowhere to be found.
if(!is_null($root))
{
// Yay! we are at the root.
// and $root contains the path.
// Do whatever you seem fit!
bootstrapOrSomething();
}
else
{
// Oh no! Can we default to something?
// Or just bail out?
throw new Exception('Oops, did you require this package via composer?');
}
@sven There may be situations where this strategy might help. Like a general console app (phar) for any project and to avoid global installation but still load a bootsrap file. Composer allows bin files to be installed at the root https://getcomposer.org/doc/articles/vendor-binaries.md, but allowing users to place the phar file wherever they see fit is a plus.
@Sven在某些情况下,这个策略可能会有所帮助。就像任何项目的通用控制台应用程序(Phar)一样,避免全局安装,但仍然加载Bootsrap文件。Composer允许将bin文件安装在根https://getcomposer.org/doc/articles/vendor-binaries.md,上,但允许用户将PHAR文件放在他们认为合适的任何位置是一个优势。
One could argue about inversion of control, but imo, simplicity should come into play sometimes.
人们可能会争论控制权的反转,但在国际海事组织,简单性有时应该发挥作用。
Ah.. I think I've figured it out..
啊……我想我已经弄明白了..
Since I used include_once './vendor/autoload.php';
in my index.php I can just use getcwd()
from the package
由于我使用了INCLUDE_ONCE‘./Vendor/autoload.php’;在我的index.php中,我只能使用包中的getcwd()
Based in input from @Sven best and cleanest solution was to collect input on class initialization and leave configuration out of the package.
基于来自@Sven的输入,最好和最干净的解决方案是收集类初始化的输入,并将配置排除在包之外。
Try saboohy/basepath
package. Gives directory of the project.
尝试使用sboohy/basepath包。给出了项目目录。
Try this:
试试这个:
$basePath = explode('/vendor', __FILE__)[0];
更多回答
No, you can't, because that value may be changed anytime during application execution.
不,您不能,因为该值可能在应用程序执行期间随时更改。
我是一名优秀的程序员,十分优秀!