From 54f6ab0c792e8dfcf279b6570f280739fabcc3e3 Mon Sep 17 00:00:00 2001 From: Vlad Date: Mon, 14 Nov 2016 19:23:32 -0500 Subject: [PATCH] - Abstracts path creation logic - Adds blank index.php to cache and temp folders - Updates unit tests - Extracts cache folder location from Renderer to Env class - Implements #643 --- lib/Config/Env.php | 22 ++++++++++++++++++---- lib/Config/Renderer.php | 2 +- tests/unit/Config/EnvTest.php | 9 +++++++++ 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/lib/Config/Env.php b/lib/Config/Env.php index cd4c0d40a9..f27acd6ba0 100644 --- a/lib/Config/Env.php +++ b/lib/Config/Env.php @@ -13,6 +13,7 @@ class Env { static $assets_path; static $assets_url; static $temp_path; + static $cache_path; static $temp_url; static $languages_path; static $lib_path; @@ -38,10 +39,12 @@ class Env { self::$assets_path = self::$path . '/assets'; self::$assets_url = plugins_url('/assets', $file); $wp_upload_dir = wp_upload_dir(); - self::$temp_path = $wp_upload_dir['basedir'] . '/' . self::$plugin_name; - if(!is_dir(self::$temp_path)) { - mkdir(self::$temp_path); - } + self::$temp_path = self::intializePath( + $wp_upload_dir['basedir'] . '/' . self::$plugin_name + ); + self::$cache_path = self::intializePath( + self::$temp_path . '/cache' + ); self::$temp_url = $wp_upload_dir['baseurl'] . '/' . self::$plugin_name; self::$languages_path = self::$path . '/lang'; self::$lib_path = self::$path . '/lib'; @@ -65,6 +68,17 @@ class Env { self::$db_timezone_offset = self::getDbTimezoneOffset(); } + static function intializePath($path) { + if(!is_dir($path)) { + mkdir($path); + file_put_contents( + $path . '/index.php', + str_replace('\n', PHP_EOL, 'caching_enabled = $caching_enabled; $this->debugging_enabled = $debugging_enabled; - $this->cache_path = Env::$temp_path . '/cache'; + $this->cache_path = Env::$cache_path; $file_system = new TwigFileSystem(Env::$views_path); $this->renderer = new TwigEnv( diff --git a/tests/unit/Config/EnvTest.php b/tests/unit/Config/EnvTest.php index 6b34e3f6c8..ffa20f2c8d 100644 --- a/tests/unit/Config/EnvTest.php +++ b/tests/unit/Config/EnvTest.php @@ -54,6 +54,15 @@ class EnvTest extends MailPoetTest { expect(Env::$db_charset)->equals($charset); } + function testItCanInitializeTempAndCacheFolders() { + // temp and cache folders should exist and contain index.php + expect(is_dir(Env::$temp_path))->true(); + expect(file_exists(Env::$temp_path . '/index.php'))->true(); + expect(file_get_contents(Env::$temp_path . '/index.php'))->contains('true(); + expect(file_get_contents(Env::$cache_path . '/index.php'))->contains('