在AppKernel

时间:2016-10-28 04:20:23

标签: symfony caching parameters kernel

我们需要将缓存和日志文件存储在项目文件夹结构之外。我已经设置了arguments_prod.yml和parameters_dev.yml,它们将在部署到不同的服务器/环境时由Bamboo构建。

有没有办法可以在AppKernal中访问这些参数,以便我可以在getCacheDir()函数中使用它们?这将是一种简单的方法,无法自己解析它们或其他东西。

因此,除了缓存和日志之外,最终目录结构应该与默认的Symfony目录结构相同。服务器团队已请求缓存和日志应位于var / tmp和var / logs下。因此,对于应用程序,缓存将是/ var / tmp / symfony / projectName / prod和/ var / tmp / symfony / projectName / dev。日志将遵循类似的结构。

所以基本上结构将遵循正常的Symfony,除了/ var / www / Symfony / projectName / var / cache变为/ var / tmp / symfony / projectName和/ var / www / Symfony / projectName / var / logs变为/无功/日志/ symfony中/ PROJECTNAME。请注意,这里的所有这些位置都是绝对的(并且项目根目录的位置可能略有不同,当Bamboo部署时,它将设置正确的路径等。)

其中一个奇怪的事情是,当我这样设置时,网站实际运行,但我在新的缓存位置下看不到任何东西(还没有开始在日志方面工作)。所以必须在某处有缓存文件,但是找不到它们的位置!

注意:我现在发现,如果你运行内部服务器,这个问题不会发生。只有在Apache下加载站点时才会发生这种情况。

2 个答案:

答案 0 :(得分:1)

您的想法的问题是,在构造ConfigCache对象后立即初始化服务容器和参数,并将绝对缓存路径作为参数。

namespace Symfony\Component\HttpKernel;

...


/**
 * The Kernel is the heart of the Symfony system.
 *
 * It manages an environment made of bundles.
 *
 * @author Fabien Potencier <fabien@symfony.com>
 */
abstract class Kernel implements KernelInterface, TerminableInterface
{
    ...
        /**
     * Initializes the service container.
     *
     * The cached version of the service container is used when fresh, otherwise the
     * container is built.
     */
    protected function initializeContainer()
    {
        $class = $this->getContainerClass();
        // !!!!!! cache config object construction
        $cache = new ConfigCache($this->getCacheDir().'/'.$class.'.php', $this->debug);
        $fresh = true;
        if (!$cache->isFresh()) {
            $container = $this->buildContainer();
            $container->compile();
            $this->dumpContainer($cache, $container, $class, $this->getContainerBaseClass());

            $fresh = false;
        }

        require_once $cache->getPath();

        $this->container = new $class();
        $this->container->set('kernel', $this);

        if (!$fresh && $this->container->has('cache_warmer')) {
            $this->container->get('cache_warmer')->warmUp($this->container->getParameter('kernel.cache_dir'));
        }
    }
}

因此,您无法访问getCacheDir()方法中的自定义参数。

你可以覆盖getCacheDir()方法吗?

假设您的目录结构如下所示

-home

 --symfony_app

 --custom_cache_directory

比方法覆盖看起来像这样:

public function getCacheDir()
{
    return dirname(__DIR__).'/../custom_cache_directory/cache/'.$this->getEnvironment();
}

官方文档的更多信息:http://symfony.com/doc/current/configuration/override_dir_structure.html#override-the-cache-directory

答案 1 :(得分:0)

所以答案是像Matko建议的那样做,不要使用/ var / tmp目录。

所以在/appName/app/AppKernel.php中我编辑了getCacheDir()使它看起来更像:

public function getCacheDir()
{
    return '/var/symfony/cache/projectName' . '/' . $this->getEnvironment();
}

或您想要使用的任何路径。

不要在/ tmp或/ var / tmp(我在RHEL上)下使用任何东西,因为这些文件夹做了一些奇怪的事情(/ var / tmp似乎从未真正将缓存写入磁盘)。