如何在没有 Composer 作为依赖项 (PSR-0) 的情况下使用带有命名空间的 PHP 库? [英] How to use a PHP library with namespacing without Composer as dependency (PSR-0)?

查看:25
本文介绍了如何在没有 Composer 作为依赖项 (PSR-0) 的情况下使用带有命名空间的 PHP 库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用一些带有依赖项的 PHP 库,但是我对客户端的 web 服务器有一些限制.这是一个托管的网络服务器,我不能使用控制台,例如通过 SSH.

I need to use some PHP libraries with dependencies but I have some restrictions on the webserver of the client. It is a managed webserver and I can not use a console eg over SSH.

那么我现在如何在没有 Composer 的情况下使用这些库?
我可以手动创建一些目录吗,我需要创建哪些目录或路径?另外,我需要创建什么才能使自动加载和命名空间正常工作?

So how do I use now these libraries without Composer?
Can I create some directories manually and what directories or paths do I need to create? Also, what do I need to create so autoloading and namespacing is working?

我可以手动创建 autoload.php 吗?文件的内容是什么?

Can I create the autoload.php somehow manually and what is the content of the file?

推荐答案

用一个简单的自动加载器就可以了,而且做起来并不难:

It is possible with a simple autoloader and it is not so hard to do it:

function __autoload($className)
{
    $className = ltrim($className, '\');
    $fileName  = '';
    $namespace = '';
    if ($lastNsPos = strripos($className, '\')) {
        $namespace = substr($className, 0, $lastNsPos);
        $className = substr($className, $lastNsPos + 1);
        $fileName  = str_replace('\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
    }
    $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
    // $fileName .= $className . '.php'; //sometimes you need a custom structure
    //require_once "library/class.php"; //or include a class manually
    require $fileName;

}

但有时您必须调整 $fileName 以使其适用于所有库.这取决于自动加载的标准以及库的类名的命名方式.有时您必须在 _ 上拆分类名并使用第一个元素作为目录名称并将其添加到类名中.例如,我有一个类似 Library_Parser 的类的第二个库,但结构是 Library/library-parser.php.

But sometimes you have to adjust the $fileName so it works with all libraries. It depends on the standard for autoloading and how the class names of the libraries are named. Sometimes you have to split the classname on _ and use the first element for the direcotry name and add this also to the class name. I had for example a second library with a class like Library_Parser but the structure was Library/library-parser.php.

第一个库直接使用上面的代码,所有的类都会自动加载.

The first library worked directly with the above code and all classes were automatically loaded.

代码取自 http://www.sitepoint.com/autoloading-and-the-psr-0-standard/ 但我不得不更正一些代码部分(额外的下划线和反斜杠).我使用了 PSR-0 标准解决方案.

The code was taken from http://www.sitepoint.com/autoloading-and-the-psr-0-standard/ but I had to correct some code parts (additional underscores and backslashes). I have used the PSR-0 Standard solution.

https://stackoverflow.com/users/1740659/thibault

PSR-4 版本:

PSR-4 version by https://stackoverflow.com/users/1740659/thibault:

function loadPackage($dir)
{
    $composer = json_decode(file_get_contents("$dir/composer.json"), 1);
    $namespaces = $composer['autoload']['psr-4'];

    // Foreach namespace specified in the composer, load the given classes
    foreach ($namespaces as $namespace => $classpaths) {
        if (!is_array($classpaths)) {
            $classpaths = array($classpaths);
        }
        spl_autoload_register(function ($classname) use ($namespace, $classpaths, $dir) {
            // Check if the namespace matches the class we are looking for
            if (preg_match("#^".preg_quote($namespace)."#", $classname)) {
                // Remove the namespace from the file path since it's psr4
                $classname = str_replace($namespace, "", $classname);
                $filename = preg_replace("#\\#", "/", $classname).".php";
                foreach ($classpaths as $classpath) {
                    $fullpath = $dir."/".$classpath."/$filename";
                    if (file_exists($fullpath)) {
                        include_once $fullpath;
                    }
                }
            }
        });
    }
}

loadPackage(__DIR__."/vendor/project");

new CompanyNamePackageNameTest();

这篇关于如何在没有 Composer 作为依赖项 (PSR-0) 的情况下使用带有命名空间的 PHP 库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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