diff --git a/src/Torann/Registry/Cache.php b/src/Torann/Registry/Cache.php index 54be66a..d124cea 100644 --- a/src/Torann/Registry/Cache.php +++ b/src/Torann/Registry/Cache.php @@ -1,6 +1,8 @@ -path = $cachePath.DIRECTORY_SEPARATOR.'torann_registry.json'; + $this->path = $cachePath . DIRECTORY_SEPARATOR . 'torann_registry.json'; // Instantiate timestamp manager if (class_exists($timestampManager)) { @@ -54,19 +56,19 @@ public function __construct($cachePath, $timestampManager) /** * Get a all cached values. * - * @param mixed $default + * @param mixed $default * @return mixed */ public function all($default = array()) { - return (! empty($this->entries) ) ? $this->entries : $default; + return (!empty($this->entries)) ? $this->entries : $default; } /** * Get a key's value from the cache. * - * @param string $key - * @param mixed $default + * @param string $key + * @param mixed $default * @return mixed */ public function get($key = null, $default = null) @@ -77,8 +79,8 @@ public function get($key = null, $default = null) /** * Add a key's value to the cache. * - * @param string $key - * @param string $value + * @param string $key + * @param string $value * @return bool */ public function add($key, $value) @@ -91,7 +93,7 @@ public function add($key, $value) /** * Set value to the cache. * - * @param array $value + * @param array $value * @return bool */ public function set(array $value) @@ -104,7 +106,7 @@ public function set(array $value) /** * Remove a key from cache. * - * @param string $key + * @param string $key * @return bool */ public function remove($key) @@ -174,6 +176,6 @@ public function save() $this->timestampManager->update($updated); } - return (bool) file_put_contents($this->path, json_encode($this->entries)); + return (bool)file_put_contents($this->path, json_encode($this->entries)); } } diff --git a/src/Torann/Registry/Facades/Registry.php b/src/Torann/Registry/Facades/Registry.php index 821d0e9..e6928ac 100644 --- a/src/Torann/Registry/Facades/Registry.php +++ b/src/Torann/Registry/Facades/Registry.php @@ -1,14 +1,18 @@ -database = $database; - $this->config = $config; - $this->cache = $cache; + $this->cache = $cache; + $this->config = $config; // Ensure cache is set $this->setCache(); @@ -52,8 +57,8 @@ public function __construct(DatabaseManager $database, Cache $cache, $config = a /** * Get value from registry * - * @param string $key - * @param string $default + * @param string $key + * @param string $default * @return mixed */ public function get($key, $default = null) @@ -62,15 +67,16 @@ public function get($key, $default = null) $value = $this->fetchValue($baseKey, $searchKey); - return (! is_null($value)) ? $value : $default; + return $value !== null ? $value : $default; } /** * Store value into registry * - * @param string $key - * @param mixed $value + * @param $key + * @param $value * @return bool + * @throws Exception */ public function set($key, $value) { @@ -82,30 +88,34 @@ public function set($key, $value) $registry = $this->cache->get($baseKey); } - if (! is_null($registry)) { + if ($registry !== null) { return $this->overwrite($key, $value); } - if ($baseKey != $searchKey) - { + if ($searchKey !== null && $baseKey !== $searchKey) { $object = array(); - $level = ''; - $keys = explode('.', $searchKey); - - foreach ($keys as $key) - { - $level .= '.'.$key; - (trim($level, '.') == $searchKey) ? array_set($object, trim($level, '.'), $value) : array_set($object, trim($level, '.'), array()); + $level = ''; + $subKeys = explode('.', $searchKey); + + foreach ($subKeys as $subKey) { + $level .= '.' . $subKey; + trim($level, '.') === $searchKey ? + array_set($object, trim($level, '.'), $value) : + array_set($object, trim($level, '.'), array()); } - $this->database->table($this->config['table'])->insert(array('key' => $baseKey, 'value' => json_encode($object))); + $this->database->table($this->config['table'])->insert(array( + 'key' => $baseKey, + 'value' => json_encode($object) + )); // Add to cache $this->cache->add($baseKey, $object); - } - else - { - $this->database->table($this->config['table'])->insert(array('key' => $baseKey, 'value' => json_encode($value))); + } else { + $this->database->table($this->config['table'])->insert(array( + 'key' => $baseKey, + 'value' => json_encode($value) + )); // Add to cache $this->cache->add($baseKey, $value); @@ -117,10 +127,10 @@ public function set($key, $value) /** * Overwrite existing value from registry * - * @param string $key - * @param mixed $value - * @throws Exception + * @param string $key + * @param mixed $value * @return bool + * @throws Exception */ public function overwrite($key, $value) { @@ -132,20 +142,19 @@ public function overwrite($key, $value) $registry = $this->cache->get($baseKey); } - if (is_null($registry)) { + if ($registry === null) { throw new Exception("Item [$key] does not exists"); } - if ($baseKey != $searchKey) - { + if ($baseKey != $searchKey) { array_set($registry, $searchKey, $value); - $this->database->table($this->config['table'])->where('key', '=', $baseKey)->update(array('value' => json_encode($registry))); + $this->database->table($this->config['table'])->where('key', '=', + $baseKey)->update(array('value' => json_encode($registry))); $this->cache->add($baseKey, $registry); - } - else - { - $this->database->table($this->config['table'])->where('key', '=', $baseKey)->update(array('value' => json_encode($value))); + } else { + $this->database->table($this->config['table'])->where('key', '=', + $baseKey)->update(array('value' => json_encode($value))); $this->cache->add($baseKey, $value); } @@ -158,13 +167,12 @@ public function overwrite($key, $value) /** * Store an array * - * @param array $values + * @param array $values * @return bool */ public function store(array $values) { - foreach ($values as $key=>$value) - { + foreach ($values as $key => $value) { // Ensure proper type $value = $this->forceTypes($value); @@ -172,9 +180,10 @@ public function store(array $values) $jsonValue = json_encode($value); // Update - $this->database->statement("INSERT INTO system_registries ( `key`, `value` ) VALUES ( ?, ? ) - ON DUPLICATE KEY UPDATE `key` = ?, `value` = ?", - array($key, $jsonValue, $key, $jsonValue)); + $this->database->statement( + 'INSERT INTO ' . Config::get('registry.table', 'system_registries') . ' ( `key`, `value` ) VALUES ( ?, ? ) ON DUPLICATE KEY UPDATE `key` = ?, `value` = ?', + array($key, $jsonValue, $key, $jsonValue) + ); $this->cache->add($key, $value); } @@ -187,7 +196,7 @@ public function store(array $values) /** * Remove existing value from registry * - * @param string $key + * @param string $key * @throws Exception * @return bool * @throws Exception @@ -202,20 +211,18 @@ public function forget($key) $registry = $this->cache->get($baseKey); } - if (is_null($registry)) { + if ($registry === null) { throw new Exception("Item [$key] does not exists"); } - if ($baseKey !== $searchKey) - { + if ($baseKey !== $searchKey) { array_forget($registry, $searchKey); - $this->database->table($this->config['table'])->where('key', '=', $baseKey)->update(array('value' => json_encode($registry))); + $this->database->table($this->config['table'])->where('key', '=', + $baseKey)->update(array('value' => json_encode($registry))); // Update cache $this->cache->add($baseKey, $registry); - } - else - { + } else { $this->database->table($this->config['table'])->where('key', '=', $baseKey)->delete(); // Remove from cache @@ -253,24 +260,22 @@ public function all($default = array()) /** * Cast values to native PHP variable types. * - * @param mixed $data + * @param mixed $data * @return mixed */ protected function forceTypes($data) { - if (in_array($data, array('true', 'false'))) - { + if (in_array($data, array('true', 'false'))) { $data = ($data === 'true' ? 1 : 0); - } - else if (is_numeric($data)) - { - $data = (int) $data; - } - else if (gettype($data) === 'array') - { - foreach($data as $key=>$value) - { - $data[$key] = $this->forceTypes($value); + } else { + if (is_numeric($data)) { + $data = (int)$data; + } else { + if (is_array($data)) { + foreach ($data as $key => $value) { + $data[$key] = $this->forceTypes($value); + } + } } } @@ -280,13 +285,12 @@ protected function forceTypes($data) /** * Get registry key * - * @param string $key + * @param string $key * @return array */ protected function fetchKey($key) { - if (str_contains($key, '.')) - { + if (str_contains($key, '.')) { $keys = explode('.', $key); $search = array_except($keys, 0); @@ -299,15 +303,15 @@ protected function fetchKey($key) /** * Get key value * - * @param string $key - * @param string $searchKey + * @param string $key + * @param string $searchKey * @return mixed */ protected function fetchValue($key, $searchKey = null) { $object = $this->cache->get($key); - if (is_null($object)) { + if ($object === null) { return null; } @@ -316,8 +320,6 @@ protected function fetchValue($key, $searchKey = null) /** * Set cache - * - * @return array */ protected function setCache() { @@ -330,8 +332,7 @@ protected function setCache() $values = array(); // Get values from database - foreach($this->database->table($this->config['table'])->get() as $setting) - { + foreach ($this->database->table($this->config['table'])->get() as $setting) { $values[$setting->key] = json_decode($setting->value, true); } diff --git a/src/Torann/Registry/RegistryServiceProvider.php b/src/Torann/Registry/RegistryServiceProvider.php index 0d8dcf4..3cc3cf9 100644 --- a/src/Torann/Registry/RegistryServiceProvider.php +++ b/src/Torann/Registry/RegistryServiceProvider.php @@ -1,43 +1,52 @@ -publishes([ - __DIR__.'/../../config/registry.php' => config_path('registry.php'), + __DIR__ . DIRECTORY_SEPARATOR . + '..' . DIRECTORY_SEPARATOR . + '..' . DIRECTORY_SEPARATOR . + 'config' . DIRECTORY_SEPARATOR . + 'registry.php' => config_path('registry.php'), ]); $this->publishes([ - __DIR__ . '/../../migrations/' => base_path('/database/migrations') + __DIR__ . DIRECTORY_SEPARATOR . + '..' . DIRECTORY_SEPARATOR . + '..' . DIRECTORY_SEPARATOR . + 'migrations' . DIRECTORY_SEPARATOR => base_path('/database/migrations') ], 'migrations'); - } + } - /** - * Register the service provider. - * - * @return void - */ - public function register() - { - $this->registerCache(); - $this->registerRegistry(); - } + /** + * Register the service provider. + * + * @return void + */ + public function register() + { + $this->registerCache(); + $this->registerRegistry(); + } /** * Register the collection repository. @@ -46,8 +55,7 @@ public function register() */ protected function registerRegistry() { - $this->app['registry'] = $this->app->share(function($app) - { + $this->app['registry'] = $this->app->share(function ($app) { $config = $app->config->get('registry', array()); return new Registry($app['db'], $app['registry.cache'], $config); @@ -61,22 +69,20 @@ protected function registerRegistry() */ protected function registerCache() { - $this->app['registry.cache'] = $this->app->share(function($app) - { + $this->app['registry.cache'] = $this->app->share(function ($app) { $meta = $app->config->get('registry.cache_path'); $timestampManager = $app->config->get('registry.timestamp_manager'); return new Cache($meta, $timestampManager); }); } - /** - * Get the services provided by the provider. - * - * @return array - */ - public function provides() - { - return array('registry'); - } - -} \ No newline at end of file + /** + * Get the services provided by the provider. + * + * @return array + */ + public function provides() + { + return array('registry'); + } +} diff --git a/src/Torann/Registry/Timestamps/Redis.php b/src/Torann/Registry/Timestamps/Redis.php index 9e150ec..698b536 100644 --- a/src/Torann/Registry/Timestamps/Redis.php +++ b/src/Torann/Registry/Timestamps/Redis.php @@ -1,13 +1,15 @@ -