Browse Source

refactor Cache_File to not cause side effects at construction

Puskás Zsolt 6 years ago
parent
commit
1d434a5428

+ 24 - 13
modules/cache/classes/Kohana/Cache/File.php

@@ -61,28 +61,22 @@ class Kohana_Cache_File extends Cache implements Cache_GarbageCollect {
 	protected $_cache_dir;
 
 	/**
-	 * Constructs the file cache driver. This method cannot be invoked externally. The file cache driver must
-	 * be instantiated using the `Cache::instance()` method.
+	 * @var  boolean  does the cache directory is avaliable
+	 */
+	protected $_inited = FALSE;
+
+	/**
+	 * Creates the cache directory.
 	 *
-	 * @param   array  $config  config
 	 * @throws  Cache_Exception
 	 */
-	protected function __construct(array $config)
+	protected function _init()
 	{
-		// Setup parent
-		parent::__construct($config);
-
 		try
 		{
 			$directory = Arr::get($this->_config, 'cache_dir', Kohana::$cache_dir);
 			$this->_cache_dir = new SplFileInfo($directory);
 		}
-		// PHP < 5.3 exception handle
-		catch (ErrorException $e)
-		{
-			$this->_cache_dir = $this->_make_directory($directory, 0777, TRUE);
-		}
-		// PHP >= 5.3 exception handle
 		catch (UnexpectedValueException $e)
 		{
 			$this->_cache_dir = $this->_make_directory($directory, 0777, TRUE);
@@ -105,6 +99,8 @@ class Kohana_Cache_File extends Cache implements Cache_GarbageCollect {
 		{
 			throw new Cache_Exception('Unable to write to the cache directory :resource', [':resource' => $this->_cache_dir->getRealPath()]);
 		}
+
+		$this->_inited = TRUE;
 	}
 
 	/**
@@ -123,6 +119,9 @@ class Kohana_Cache_File extends Cache implements Cache_GarbageCollect {
 	 */
 	public function get($id, $default = NULL)
 	{
+		// Make sure cache is inited
+		$this->_inited or $this->_init();
+
 		$filename = Cache_File::filename($this->_sanitize_id($id));
 		$directory = $this->_resolve_directory($filename);
 
@@ -197,6 +196,9 @@ class Kohana_Cache_File extends Cache implements Cache_GarbageCollect {
 	 */
 	public function set($id, $data, $lifetime = NULL)
 	{
+		// Make sure cache is inited
+		$this->_inited or $this->_init();
+
 		$filename = Cache_File::filename($this->_sanitize_id($id));
 		$directory = $this->_resolve_directory($filename);
 
@@ -251,6 +253,9 @@ class Kohana_Cache_File extends Cache implements Cache_GarbageCollect {
 	 */
 	public function delete($id)
 	{
+		// Make sure cache is inited
+		$this->_inited or $this->_init();
+
 		$filename = Cache_File::filename($this->_sanitize_id($id));
 		$directory = $this->_resolve_directory($filename);
 
@@ -271,6 +276,9 @@ class Kohana_Cache_File extends Cache implements Cache_GarbageCollect {
 	 */
 	public function delete_all()
 	{
+		// Make sure cache is inited
+		$this->_inited or $this->_init();
+
 		return $this->_delete_file($this->_cache_dir, TRUE);
 	}
 
@@ -282,6 +290,9 @@ class Kohana_Cache_File extends Cache implements Cache_GarbageCollect {
 	 */
 	public function garbage_collect()
 	{
+		// Make sure cache is inited
+		$this->_inited or $this->_init();
+
 		$this->_delete_file($this->_cache_dir, TRUE, FALSE, TRUE);
 		return;
 	}

+ 3 - 5
modules/cache/tests/cache/CacheTest.php

@@ -20,8 +20,6 @@ class Kohana_CacheTest extends Unittest_TestCase {
 	 */
 	public function provider_instance()
 	{
-		$tmp = realpath(sys_get_temp_dir());
-
 		$base = [];
 
 		if (Kohana::$config->load('cache.file'))
@@ -40,11 +38,11 @@ class Kohana_CacheTest extends Unittest_TestCase {
 			];
 		}
 
-		return $base + [
+		return $base + [[
 			// Test bad group definition
 			Kohana_CacheTest::BAD_GROUP_DEFINITION,
 			'Failed to load Kohana Cache group: 1010'
-		];
+		]];
 	}
 
 	/**
@@ -63,7 +61,7 @@ class Kohana_CacheTest extends Unittest_TestCase {
 		catch (Cache_Exception $e)
 		{
 			$this->assertSame($expected, $e->getMessage());
-			throw $e;
+			return;
 		}
 
 		$this->assertInstanceOf(get_class($expected), $cache);

+ 1 - 23
modules/unittest/bootstrap.php

@@ -121,28 +121,6 @@ if (($ob_len = ob_get_length()) !== FALSE)
 	}
 }
 
-// Preset cache config if not set
-$cache_config = Kohana::$config->load('cache');
-
-if (($cache_config->get('default') === NULL AND $cache_config->get('file') === NULL) OR
-	($cache_config->get('default') === 'file' AND $cache_config->get('file') === NULL))
-{
-	$cache_config->set(
-		'file',
-		[
-			'driver' => 'file',
-			'cache_dir' => APPPATH.'cache',
-			'default_expire' => 3600,
-			'ignore_on_delete' => [
-				'file_we_want_to_keep.cache',
-				'.gitignore',
-				'.git',
-				'.svn'
-			]
-		]
-	);
-}
-
 // Enable all modules we can find
 $modules_iterator = new DirectoryIterator(MODPATH);
 
@@ -158,4 +136,4 @@ foreach ($modules_iterator as $module)
 
 Kohana::modules($modules);
 
-unset($cache_config, $modules_iterator, $modules, $module);
+unset($modules_iterator, $modules, $module);

+ 0 - 2
modules/unittest/classes/Kohana/Unittest/Tests.php

@@ -42,8 +42,6 @@ class Kohana_Unittest_Tests {
 		restore_error_handler();
 
 		spl_autoload_register(['Unittest_tests', 'autoload']);
-
-		Unittest_tests::$cache = (($cache = Kohana::cache('unittest_whitelist_cache')) === NULL) ? [] : $cache;
 	}
 
 	/**