public function testDeleteRecursive() { $tmp = sys_get_temp_dir() . '/stash/'; $dirOne = $tmp . 'test/delete/recursive'; @mkdir($dirOne, 0770, true); touch($dirOne . '/test'); touch($dirOne . '/test2'); $dirTwo = $tmp . 'recursive/delete/test'; @mkdir($dirTwo, 0770, true); touch($dirTwo . '/test3'); touch($dirTwo . '/test4'); $this->assertTrue(Utilities::deleteRecursive($dirTwo . '/test3'), 'deleteRecursive returned true when removing single file.'); $this->assertFileNotExists($dirTwo . '/test3', 'deleteRecursive removed single file'); $this->assertTrue(Utilities::deleteRecursive($tmp), 'deleteRecursive returned true when removing directories.'); $this->assertFileNotExists($tmp, 'deleteRecursive cleared out the directory'); $this->assertFalse(Utilities::deleteRecursive($tmp), 'deleteRecursive returned false when passed nonexistant directory'); }
/** * Finds the method of encoding that has the cheapest decode needs and encodes the data with that method. * * @param string $data * @return string */ protected function encode($data) { switch (Utilities::encoding($data)) { case 'bool': $dataString = (bool) $data ? 'true' : 'false'; break; case 'string': $dataString = sprintf('"%s"', addcslashes($data, "\t\"\$\\")); break; case 'numeric': $dataString = (string) $data; break; default: case 'serialize': $dataString = 'unserialize(base64_decode(\'' . base64_encode(serialize($data)) . '\'))'; break; } return $dataString; }
public static function tearDownAfterClass() { Utilities::deleteRecursive(Utilities::getBaseDirectory()); }
/** * Turns a key array into a key string. This includes running the indexing functions used to manage the Redis * hierarchical storage. * * When requested the actual path, rather than a normalized value, is returned. * * @param array $key * @param bool $path * @return string */ protected function makeKeyString($key, $path = false) { $key = \Stash\Utilities::normalizeKeys($key); $keyString = 'cache:::'; $pathKey = ':pathdb::'; foreach ($key as $name) { //a. cache:::name //b. cache:::name0:::sub $keyString .= $name; //a. :pathdb::cache:::name //b. :pathdb::cache:::name0:::sub $pathKey = ':pathdb::' . $keyString; $pathKey = md5($pathKey); if (isset($this->keyCache[$pathKey])) { $index = $this->keyCache[$pathKey]; } else { $index = $this->redis->get($pathKey); $this->keyCache[$pathKey] = $index; } //a. cache:::name0::: //b. cache:::name0:::sub1::: $keyString .= '_' . $index . ':::'; } return $path ? $pathKey : md5($keyString); }
/** * This function clears the data from a key. If a key points to both a directory and a file, both are erased. If * passed null, the entire cache directory is removed. * * {@inheritdoc} */ public function clear($key = null) { $path = $this->makePath($key); if (is_file($path)) { $return = true; unlink($path); } $extension = $this->getEncoder()->getExtension(); if (strpos($path, $extension) !== false) { $path = substr($path, 0, -strlen($extension)); } if (is_dir($path)) { return Utilities::deleteRecursive($path, true); } return isset($return); }
/** * Turns a key array into a key string. This includes running the indexing functions used to manage the memcached * hierarchical storage. * * When requested the actual path, rather than a normalized value, is returned. * * @param array $key * @param bool $path * @return string */ protected function makeKeyString($key, $path = false) { $key = \Stash\Utilities::normalizeKeys($key); $keyString = ':cache:::'; $pathKey = ':pathdb::'; $time = microtime(true); if ($time - $this->keyCacheTime >= $this->keyCacheTimeLimit) { $this->keyCacheTime = $time; $this->keyCache = array(); } foreach ($key as $name) { //a. cache:::name //b. cache:::name0:::sub $keyString .= $name; //a. :pathdb::cache:::name //b. :pathdb::cache:::name0:::sub $pathKey = ':pathdb::' . $keyString; $pathKey = md5($pathKey); if (isset($this->keyCache[$pathKey])) { $index = $this->keyCache[$pathKey]; } else { $index = $this->memcache->cas($pathKey, 0); $this->keyCache[$pathKey] = $index; } //a. cache:::name0::: //b. cache:::name0:::sub1::: $keyString .= '_' . $index . ':::'; } return $path ? $pathKey : md5($keyString); }
/** * Clears data from database. If a key is defined only it and it's children are removed. If everything is set to be * cleared then the database itself is deleted off disk. * * @param null|string $key * @return bool */ public function clear($key = null) { // return true if the cache is already empty if (!($driver = $this->getDriver())) { return true; } if (!isset($key)) { unset($driver); $this->driver = null; $this->driver = false; \Stash\Utilities::deleteRecursive($this->path); } else { $driver->query("DELETE FROM cacheStore WHERE key LIKE '{$key}%'"); } return true; }
public function testCheckEmptyDirectory() { $tmp = sys_get_temp_dir() . '/stash/'; $dir2 = $tmp . 'emptytest/'; @mkdir($dir2, 0770, true); $this->assertTrue(Utilities::checkForEmptyDirectory($dir2), 'Returns true for empty directories'); $this->assertFalse(Utilities::checkForEmptyDirectory($tmp), 'Returns false for non-empty directories'); Utilities::deleteRecursive($tmp); }
/** * This function takes an array of strings and turns it into the sqlKey. It does this by iterating through the * array, running the string through sqlite_escape_string() and then combining that string to the keystring with a * delimiter. * * @param array $key * @return string */ public static function makeSqlKey($key) { $key = Utilities::normalizeKeys($key, 'base64_encode'); $path = ''; foreach ($key as $rawPathPiece) { $path .= $rawPathPiece . ':::'; } return $path; }
/** * This function clears the data from a key. If a key points to both a directory and a file, both are erased. If * passed null, the entire cache directory is removed. * * {@inheritdoc} */ public function clear($key = null) { $path = $this->makePath($key); if (is_file($path)) { $return = true; unlink($path); } if (strpos($path, '.php') !== false) { $path = substr($path, 0, -4); } if (is_dir($path)) { return Utilities::deleteRecursive($path); } return isset($return); }
/** * @expectedException Stash\Exception\InvalidArgumentException */ public function testCheckFileSystemPermissionsUnwrittableException() { Utilities::checkFileSystemPermissions('/home', '0644'); }