在没有框架的情况下使用 Swiftmailer [英] Using Swiftmailer without a framework

查看:42
本文介绍了在没有框架的情况下使用 Swiftmailer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 Swiftmailer 作为一个独立的库,没有 Symfony、Laravel、Yii、composer 等.

I am wanting to use Swiftmailer as a standalone library without Symfony, Laravel, Yii, composer, etc.

根据 Swiftmailer 网站,包含库的方法是调用 composer 的自动加载.php,但是在这种情况下我没有使用 composer 并且想知道需要包含/vender/swiftmailer 中的哪些基本文件才能使用库而不是 require_once '/path/to/vendor/autoload.php .php';

According to the Swiftmailer website the way to include the library is by calling composer's autoload.php, however I am not using composer in this case and would like to know which base files in /vender/swiftmailer need to be included in order to use the library instead of require_once '/path/to/vendor/autoload.php';

我查看了 Swiftmailer Google Group,但它已经停止并说来这里.

I looked at the Swiftmailer Google Group, but it's been discontinued and says to come here.

谢谢

推荐答案

正如您所发现的,如果您将 Swiftmailer 放入网站而没有任何帮助加载的内容,您将需要为外部添加您自己的自动加载器依赖.

As you have discovered, if you are putting the Swiftmailer into a website without anything to help with the loading, you will need to add your own autoloader for the external dependencies.

Swiftmailer v6.1.0 有两个:https://github.com/egulias/EmailValidator而另一个https://github.com/doctrine/lexer

There are two for Swiftmailer v6.1.0: https://github.com/egulias/EmailValidator and another https://github.com/doctrine/lexer

包含 swiftlib/swift_required.php 会自动加载所有 swift 类.您可以将其他人的自动加载添加到同一个文件中.这是将两个依赖项复制到 swiftlib 文件夹的子目录中的示例:

Including swiftlib/swift_required.php autoloads all the swift classes. You could add the autoload for the others into the same file. Here's an example where the two dependencies have been copied into subdirectories of the swiftlib folder:

spl_autoload_register(function ($class) {

    $load_deps = [ 
        'Egulias\\EmailValidator\\' => __DIR__ . '/EmailValidator/',
        'Doctrine\\Common\\Lexer\\' => __DIR__ . '/Doctrine/Common/Lexer/',
    ];

    foreach ($load_deps as $prefix => $base_dir) {
        // does the class use the namespace prefix?
        $len = strlen($prefix);
        if (strncmp($prefix, $class, $len) == 0) {

            // get the relative class name
            $relative_class = substr($class, $len);

            // replace the namespace prefix with the base directory, replace namespace
            // separators with directory separators in the relative class name, append
            // with .php
            $file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';

            // if the file exists, require it
            if (file_exists($file)) {
                require $file;
            }
        }
    }
});

这篇关于在没有框架的情况下使用 Swiftmailer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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