- 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
This commit is contained in:
Vlad
2016-11-14 19:23:32 -05:00
parent 8c3525589c
commit 54f6ab0c79
3 changed files with 28 additions and 5 deletions

View File

@@ -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, '<?php\n\n// Silence is golden')
);
}
return $path;
}
private static function dbSourceName($host, $socket, $port) {
$source_name = array(
(!$socket) ? 'mysql:host=' : 'mysql:unix_socket=',

View File

@@ -16,7 +16,7 @@ class Renderer {
function __construct($caching_enabled = false, $debugging_enabled = false) {
$this->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(

View File

@@ -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('<?php');
expect(is_dir(Env::$cache_path))->true();
expect(file_get_contents(Env::$cache_path . '/index.php'))->contains('<?php');
}
function testItCanGenerateDbSourceName() {
$source_name = ((!ENV::$db_socket) ? 'mysql:host=' : 'mysql:unix_socket=') .
ENV::$db_host . ';port=' . ENV::$db_port . ';dbname=' . DB_NAME;