【问题标题】:How to install twig without using composer如何在不使用 Composer 的情况下安装 Twig
【发布时间】:2017-04-19 21:57:15
【问题描述】:

我想为一个项目安装 twig,但没有对服务器的命令行访问权限。我只能通过 ftp 上传文件。这意味着我必须手动设置 twig lib,即自己创建 Autoload.php 文件。我已经彻底搜索过,但关于这个主题的信息很少。我已尝试从另一个项目“借用”以下自动加载,但这不会产生有效的设置。

<?php

/*
 * This file is part of Twig.
 *
 * (c) 2009 Fabien Potencier
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 * Autoloads Twig classes.
 *
 * @author Fabien Potencier <fabien@symfony.com>
 */
class Twig_Autoloader
{
    /**
     * Registers Twig_Autoloader as an SPL autoloader.
     *
     * @param bool    $prepend Whether to prepend the autoloader or not.
     */
    public static function register($prepend = false)
    {
        if (version_compare(phpversion(), '5.3.0', '>=')) {
            spl_autoload_register(array(__CLASS__, 'autoload'), true, $prepend);
        } else {
            spl_autoload_register(array(__CLASS__, 'autoload'));
        }
    }

    /**
     * Handles autoloading of classes.
     *
     * @param string $class A class name.
     */
    public static function autoload($class)
    {
        if (0 !== strpos($class, 'Twig')) {
            return;
        }

        if (is_file($file = dirname(__FILE__).'/../'.str_replace(array('_', "\0"), array('/', ''), $class).'.php')) {
            require $file;
        }
    }
}

任何帮助将不胜感激。

【问题讨论】:

  • 你有本地开发副本吗?您可以使用 Composer 在本地安装,然后使用您的部署过程部署新版本。
  • 您能找到其他托管服务提供商吗?

标签: php installation twig


【解决方案1】:
  1. 下载树枝。 (来自例如:https://github.com/twigphp/Twig/archive/2.x.zip
  2. 将其解压缩到您想要的相对路径(例如:./Twig-2.x)。
  3. Twig-2.x/src文件夹重命名为Twig-2.x/Twig
  4. 将此 spl_autoload_register() 函数包含到您的引导脚本中。
  5. 记得使用 FQCN 来引用 Twig 的类

示例目录结构:

Appdir/
Appdir/Twig-2.x/
Appdir/Twig-2.x/Twig/ <- this is the original src dir renamed to Twig
Appdir/templates/
Appdir/templates/index.html
Appdir/cache/
Appdir/index.php

代码“index.php”:

<?php

#ini_set('display_errors',1); # uncomment if you need debugging

spl_autoload_register(function ($classname) {
    $dirs = array (
        './Twig-2.x/' #./path/to/dir_where_src_renamed_to_Twig_is_in
    );

    foreach ($dirs as $dir) {
        $filename = $dir . str_replace('\\', '/', $classname) .'.php';
        if (file_exists($filename)) {
            require_once $filename;
            break;
        }
    }

});

$loader = new \Twig\Loader\FilesystemLoader('templates');
$twig = new \Twig\Environment($loader, [
    'cache' => 'cache',
]);

echo $twig->render('index.html', ['name' => 'Carlos']);

?>

代码“index.html”:

<h1>Hello {{ name }}!</h1>

【讨论】:

    【解决方案2】:

    如果你真的需要在没有 Composer 的情况下加载 Twig,你可以使用这个:https://gist.github.com/sarciszewski/b6cd3776fbd20acaf26b

    我建议在本地开发环境中设置 Composer。 (您可以从https://getcomposer.org/下载) 使用 Composer 安装 twig。

    composer require twig/twig:~2.0
    

    然后将自动加载器包含到您的项目中:

    require_once 'vendor/autoload.php';
    

    您可以在本地工作,当项目准备就绪时,使用包含已安装软件包的供应商目录部署到您的服务器。您的服务器上不需要作曲家。

    【讨论】:

    • 为什么不提供official 下载源Twig
    【解决方案3】:

    在研究了 András 提供的链接后,我意识到关键不是加载机制,而是“autoload.php”似乎丢失的事实。然后我发现,我使用的 od Twig 版本实际上是 2.X 版本,至少需要 PHP 7。由于我使用的是 5.4,这显然行不通。幸运的是,最新的 1.X 版本的 lib 确实提供了 autoload.php,所以从那里开始一切正常。

    【讨论】:

      猜你喜欢
      • 2013-04-03
      • 2012-07-23
      • 2015-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-23
      • 1970-01-01
      相关资源
      最近更新 更多