Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/3.0' into 3.0-move-time
Browse files Browse the repository at this point in the history
Conflicts:
	config/bootstrap.php
  • Loading branch information
lorenzo committed Aug 31, 2014
2 parents c21382d + be10795 commit 415aef9
Show file tree
Hide file tree
Showing 31 changed files with 150 additions and 106 deletions.
2 changes: 2 additions & 0 deletions config/bootstrap.php
Expand Up @@ -27,6 +27,8 @@ class_alias('Cake\Network\Exception\NotFoundException', 'Cake\Error\NotFoundExce
class_alias('Cake\Network\Exception\NotImplementedException', 'Cake\Error\NotImplementedException');
class_alias('Cake\Network\Exception\SocketException', 'Cake\Error\SocketException');
class_alias('Cake\Network\Exception\UnauthorizedException', 'Cake\Error\UnauthorizedException');
class_alias('Cake\Filesystem\File', 'Cake\Utility\File');
class_alias('Cake\Filesystem\Folder', 'Cake\Utility\Folder');
class_alias('Cake\I18n\Time', 'Cake\Utiliy\Time');

require CAKE . 'basics.php';
53 changes: 37 additions & 16 deletions src/Cache/Engine/FileEngine.php
Expand Up @@ -16,8 +16,9 @@

use Cake\Cache\CacheEngine;
use Cake\Core\Configure;
use Cake\Core\Exception\Exception;
use Cake\Utility\Inflector;
use Exception;
use LogicException;

/**
* File Storage engine for cache. Filestorage is the slowest cache storage
Expand Down Expand Up @@ -147,7 +148,9 @@ public function write($key, $data) {
}

$this->_File->rewind();
$success = $this->_File->ftruncate(0) && $this->_File->fwrite($contents) && $this->_File->fflush();
$success = $this->_File->ftruncate(0) &&
$this->_File->fwrite($contents) &&
$this->_File->fflush();

if ($this->_config['lock']) {
$this->_File->flock(LOCK_UN);
Expand All @@ -161,7 +164,8 @@ public function write($key, $data) {
* Read a key from the cache
*
* @param string $key Identifier for the data
* @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it
* @return mixed The cached data, or false if the data doesn't exist, has
* expired, or if there was an error fetching it
*/
public function read($key) {
$key = $this->_key($key);
Expand All @@ -178,7 +182,9 @@ public function read($key) {
$time = time();
$cachetime = intval($this->_File->current());

if ($cachetime !== false && ($cachetime < $time || ($time + $this->_config['duration']) < $cachetime)) {
if ($cachetime !== false &&
($cachetime < $time || ($time + $this->_config['duration']) < $cachetime)
) {
if ($this->_config['lock']) {
$this->_File->flock(LOCK_UN);
}
Expand Down Expand Up @@ -211,7 +217,8 @@ public function read($key) {
* Delete a key from the cache
*
* @param string $key Identifier for the data
* @return bool True if the value was successfully deleted, false if it didn't exist or couldn't be removed
* @return bool True if the value was successfully deleted, false if it didn't
* exist or couldn't be removed
*/
public function delete($key) {
$key = $this->_key($key);
Expand Down Expand Up @@ -249,7 +256,10 @@ public function clear($check) {
$this->_clearDirectory($this->_config['path'], $now, $threshold);

$directory = new \RecursiveDirectoryIterator($this->_config['path']);
$contents = new \RecursiveIteratorIterator($directory, \RecursiveIteratorIterator::SELF_FIRST);
$contents = new \RecursiveIteratorIterator(
$directory,
\RecursiveIteratorIterator::SELF_FIRST
);
$cleared = [];
foreach ($contents as $path) {
if ($path->isFile()) {
Expand Down Expand Up @@ -288,7 +298,7 @@ protected function _clearDirectory($path, $now, $threshold) {

try {
$file = new \SplFileObject($path . $entry, 'r');
} catch (\Exception $e) {
} catch (Exception $e) {
continue;
}

Expand Down Expand Up @@ -321,10 +331,10 @@ protected function _clearDirectory($path, $now, $threshold) {
* @param string $key The key to decrement
* @param int $offset The number to offset
* @return void
* @throws \Cake\Core\Exception\Exception
* @throws \LogicException
*/
public function decrement($key, $offset = 1) {
throw new Exception('Files cannot be atomically decremented.');
throw new LogicException('Files cannot be atomically decremented.');
}

/**
Expand All @@ -333,10 +343,10 @@ public function decrement($key, $offset = 1) {
* @param string $key The key to decrement
* @param int $offset The number to offset
* @return void
* @throws \Cake\Core\Exception\Exception
* @throws \LogicException
*/
public function increment($key, $offset = 1) {
throw new Exception('Files cannot be atomically incremented.');
throw new LogicException('Files cannot be atomically incremented.');
}

/**
Expand Down Expand Up @@ -366,13 +376,15 @@ protected function _setKey($key, $createKey = false) {
$exists = file_exists($path->getPathname());
try {
$this->_File = $path->openFile('c+');
} catch (\Exception $e) {
} catch (Exception $e) {
trigger_error($e->getMessage(), E_USER_WARNING);
return false;
}
unset($path);

if (!$exists && !chmod($this->_File->getPathname(), (int)$this->_config['mask'])) {
if (!$exists &&
!chmod($this->_File->getPathname(), (int)$this->_config['mask'])
) {
trigger_error(sprintf(
'Could not apply permission mask "%s" on cache file "%s"',
$this->_File->getPathname(), $this->_config['mask']
Expand All @@ -397,7 +409,9 @@ protected function _active() {
}
if ($this->_init && !($dir->isDir() && $dir->isWritable())) {
$this->_init = false;
trigger_error(sprintf('%s is not writable', $this->_config['path']), E_USER_WARNING);
trigger_error(sprintf(
'%s is not writable', $this->_config['path']
), E_USER_WARNING);
return false;
}
return true;
Expand All @@ -414,7 +428,11 @@ public function key($key) {
return false;
}

$key = Inflector::underscore(str_replace([DS, '/', '.', '<', '>', '?', ':', '|', '*', '"'], '_', strval($key)));
$key = Inflector::underscore(str_replace(
[DS, '/', '.', '<', '>', '?', ':', '|', '*', '"'],
'_',
strval($key)
));
return $key;
}

Expand All @@ -427,7 +445,10 @@ public function key($key) {
public function clearGroup($group) {
$this->_File = null;
$directoryIterator = new \RecursiveDirectoryIterator($this->_config['path']);
$contents = new \RecursiveIteratorIterator($directoryIterator, \RecursiveIteratorIterator::CHILD_FIRST);
$contents = new \RecursiveIteratorIterator(
$directoryIterator,
\RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($contents as $object) {
$containsGroup = strpos($object->getPathName(), DS . $group . DS) !== false;
$hasPrefix = true;
Expand Down
74 changes: 48 additions & 26 deletions src/Cache/Engine/MemcachedEngine.php
Expand Up @@ -15,9 +15,9 @@
namespace Cake\Cache\Engine;

use Cake\Cache\CacheEngine;
use Cake\Core\Exception\Exception;
use Cake\Utility\Inflector;
use \Memcached;
use InvalidArgumentException;
use Memcached;

/**
* Memcached storage engine for cache. Memcached has some limitations in the amount of
Expand Down Expand Up @@ -94,7 +94,8 @@ class MemcachedEngine extends CacheEngine {
*
* @param array $config array of setting for the engine
* @return bool True if the engine has been successfully initialized, false if not
* @throws \Cake\Core\Exception\Exception when you try use authentication without Memcached compiled with SASL support
* @throws InvalidArgumentException When you try use authentication without
* Memcached compiled with SASL support
*/
public function init(array $config = []) {
if (!class_exists('Memcached')) {
Expand Down Expand Up @@ -128,7 +129,8 @@ public function init(array $config = []) {
return true;
}

$this->_Memcached = new Memcached($this->_config['persistent'] ? (string)$this->_config['persistent'] : null);
$this->_Memcached = new Memcached($this->_config['persistent'] ?
(string)$this->_config['persistent'] : null);
$this->_setOptions();

if (count($this->_Memcached->getServerList())) {
Expand All @@ -152,11 +154,14 @@ public function init(array $config = []) {

if ($this->_config['login'] !== null && $this->_config['password'] !== null) {
if (!method_exists($this->_Memcached, 'setSaslAuthData')) {
throw new Exception(
throw new InvalidArgumentException(
'Memcached extension is not build with SASL support'
);
}
$this->_Memcached->setSaslAuthData($this->_config['login'], $this->_config['password']);
$this->_Memcached->setSaslAuthData(
$this->_config['login'],
$this->_config['password']
);
}

return true;
Expand All @@ -166,32 +171,46 @@ public function init(array $config = []) {
* Settings the memcached instance
*
* @return void
* @throws \Cake\Core\Exception\Exception when the Memcached extension is not built with the desired serializer engine
* @throws InvalidArgumentException When the Memcached extension is not built
* with the desired serializer engine.
*/
protected function _setOptions() {
$this->_Memcached->setOption(Memcached::OPT_LIBKETAMA_COMPATIBLE, true);

$serializer = strtolower($this->_config['serialize']);
if (!isset($this->_serializers[$serializer])) {
throw new Exception(
throw new InvalidArgumentException(
sprintf('%s is not a valid serializer engine for Memcached', $serializer)
);
}

if ($serializer !== 'php' && !constant('Memcached::HAVE_' . strtoupper($serializer))) {
throw new Exception(
if ($serializer !== 'php' &&
!constant('Memcached::HAVE_' . strtoupper($serializer))
) {
throw new InvalidArgumentException(
sprintf('Memcached extension is not compiled with %s support', $serializer)
);
}

$this->_Memcached->setOption(Memcached::OPT_SERIALIZER, $this->_serializers[$serializer]);
$this->_Memcached->setOption(
Memcached::OPT_SERIALIZER,
$this->_serializers[$serializer]
);

// Check for Amazon ElastiCache instance
if (defined('Memcached::OPT_CLIENT_MODE') && defined('Memcached::DYNAMIC_CLIENT_MODE')) {
$this->_Memcached->setOption(Memcached::OPT_CLIENT_MODE, Memcached::DYNAMIC_CLIENT_MODE);
if (defined('Memcached::OPT_CLIENT_MODE') &&
defined('Memcached::DYNAMIC_CLIENT_MODE')
) {
$this->_Memcached->setOption(
Memcached::OPT_CLIENT_MODE,
Memcached::DYNAMIC_CLIENT_MODE
);
}

$this->_Memcached->setOption(Memcached::OPT_COMPRESSION, (bool)$this->_config['compress']);
$this->_Memcached->setOption(
Memcached::OPT_COMPRESSION,
(bool)$this->_config['compress']
);
}

/**
Expand Down Expand Up @@ -224,8 +243,9 @@ protected function _parseServerString($server) {

/**
* Write data for key into cache. When using memcached as your cache engine
* remember that the Memcached pecl extension does not support cache expiry times greater
* than 30 days in the future. Any duration greater than 30 days will be treated as never expiring.
* remember that the Memcached pecl extension does not support cache expiry
* times greater than 30 days in the future. Any duration greater than 30 days
* will be treated as never expiring.
*
* @param string $key Identifier for the data
* @param mixed $value Data to be cached
Expand All @@ -247,7 +267,8 @@ public function write($key, $value) {
* Write many cache entries to the cache at once
*
* @param array $data An array of data to be stored in the cache
* @return array of bools for each key provided, true if the data was successfully cached, false on failure
* @return array of bools for each key provided, true if the data was
* successfully cached, false on failure
*/
public function writeMany($data) {
$cacheData = array();
Expand All @@ -268,7 +289,8 @@ public function writeMany($data) {
* Read a key from the cache
*
* @param string $key Identifier for the data
* @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it
* @return mixed The cached data, or false if the data doesn't exist, has
* expired, or if there was an error fetching it.
*/
public function read($key) {
$key = $this->_key($key);
Expand All @@ -280,8 +302,8 @@ public function read($key) {
* Read many keys from the cache at once
*
* @param array $keys An array of identifiers for the data
* @return An array containing, for each of the given $keys, the cached data or false if cached data could not be
* retreived
* @return An array containing, for each of the given $keys, the cached data or
* false if cached data could not be retreived.
*/
public function readMany($keys) {
$cacheKeys = array();
Expand All @@ -292,7 +314,8 @@ public function readMany($keys) {
$values = $this->_Memcached->getMulti($cacheKeys);
$return = array();
foreach ($keys as &$key) {
$return[$key] = array_key_exists($this->_key($key), $values) ? $values[$this->_key($key)] : false;
$return[$key] = array_key_exists($this->_key($key), $values) ?
$values[$this->_key($key)] : false;
}
return $return;
}
Expand All @@ -303,7 +326,6 @@ public function readMany($keys) {
* @param string $key Identifier for the data
* @param int $offset How much to increment
* @return bool|int New incremented value, false otherwise
* @throws \Cake\Core\Exception\Exception when you try to increment with compress = true
*/
public function increment($key, $offset = 1) {
$key = $this->_key($key);
Expand All @@ -317,7 +339,6 @@ public function increment($key, $offset = 1) {
* @param string $key Identifier for the data
* @param int $offset How much to subtract
* @return bool|int New decremented value, false otherwise
* @throws \Cake\Core\Exception\Exception when you try to decrement with compress = true
*/
public function decrement($key, $offset = 1) {
$key = $this->_key($key);
Expand All @@ -329,7 +350,8 @@ public function decrement($key, $offset = 1) {
* Delete a key from the cache
*
* @param string $key Identifier for the data
* @return bool True if the value was successfully deleted, false if it didn't exist or couldn't be removed
* @return bool True if the value was successfully deleted, false if it didn't
* exist or couldn't be removed.
*/
public function delete($key) {
$key = $this->_key($key);
Expand All @@ -341,8 +363,8 @@ public function delete($key) {
* Delete many keys from the cache at once
*
* @param array $keys An array of identifiers for the data
* @return array of boolean values that are true if the key was successfully deleted, false if it didn't exist or
* couldn't be removed
* @return array of boolean values that are true if the key was successfully
* deleted, false if it didn't exist or couldn't be removed.
*/
public function deleteMany($keys) {
$cacheKeys = array();
Expand Down
2 changes: 1 addition & 1 deletion src/Console/Shell.php
Expand Up @@ -18,9 +18,9 @@
use Cake\Console\Exception\ConsoleException;
use Cake\Core\Configure;
use Cake\Core\Plugin;
use Cake\Filesystem\File;
use Cake\Log\LogTrait;
use Cake\Model\ModelAwareTrait;
use Cake\Utility\File;
use Cake\Utility\Inflector;
use Cake\Utility\MergeVariablesTrait;
use Cake\Utility\String;
Expand Down
2 changes: 1 addition & 1 deletion src/Utility/File.php → src/Filesystem/File.php
Expand Up @@ -12,7 +12,7 @@
* @since 0.2.9
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Utility;
namespace Cake\Filesystem;

/**
* Convenience class for reading, writing and appending to files.
Expand Down
2 changes: 1 addition & 1 deletion src/Utility/Folder.php → src/Filesystem/Folder.php
Expand Up @@ -12,7 +12,7 @@
* @since 0.2.9
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Utility;
namespace Cake\Filesystem;

/**
* Folder structure browser, lists folders and files.
Expand Down

0 comments on commit 415aef9

Please sign in to comment.