From 078317623851ed341988fa3ffeb17e23ab103ec1 Mon Sep 17 00:00:00 2001 From: mark_story Date: Sun, 15 Nov 2009 19:55:20 -0500 Subject: [PATCH] Replacing duplicated code with pluginSplit(). Updating test case. --- cake/console/cake.php | 9 ++------- cake/console/libs/bake.php | 2 +- cake/console/libs/schema.php | 5 +++-- cake/console/libs/shell.php | 6 +----- cake/libs/cache.php | 5 +---- cake/libs/cake_log.php | 6 ++---- cake/libs/class_registry.php | 8 ++++---- cake/libs/configure.php | 14 +++++++------- cake/libs/controller/component.php | 12 ++---------- cake/libs/controller/components/acl.php | 4 +--- cake/libs/controller/components/email.php | 4 +--- cake/libs/controller/controller.php | 15 ++++----------- cake/libs/model/model.php | 22 ++++++---------------- cake/libs/model/model_behavior.php | 5 +---- cake/libs/view/helpers/js.php | 5 ++--- cake/libs/view/view.php | 8 ++------ cake/tests/cases/libs/configure.test.php | 4 ++-- 17 files changed, 42 insertions(+), 92 deletions(-) diff --git a/cake/console/cake.php b/cake/console/cake.php index 27309fbd4..7ec5c19be 100644 --- a/cake/console/cake.php +++ b/cake/console/cake.php @@ -317,13 +317,8 @@ class ShellDispatcher { $this->help(); return true; } - - if (strpos($arg, '.') !== false) { - list($plugin, $shell) = explode('.', $arg); - } else { - $plugin = null; - $shell = $arg; - } + + list($plugin, $shell) = pluginSplit($arg); $this->shell = $shell; $this->shellName = Inflector::camelize($shell); $this->shellClass = $this->shellName . 'Shell'; diff --git a/cake/console/libs/bake.php b/cake/console/libs/bake.php index ff270f086..5be3482d1 100644 --- a/cake/console/libs/bake.php +++ b/cake/console/libs/bake.php @@ -57,7 +57,7 @@ class BakeShell extends Shell { } foreach($this->args as $i => $arg) { if (strpos($arg, '.')) { - list($this->params['plugin'], $this->args[$i]) = explode('.', $arg); + list($this->params['plugin'], $this->args[$i]) = pluginSplit($arg); break; } } diff --git a/cake/console/libs/schema.php b/cake/console/libs/schema.php index b6c69708a..0ab9acd14 100644 --- a/cake/console/libs/schema.php +++ b/cake/console/libs/schema.php @@ -63,9 +63,10 @@ class SchemaShell extends Shell { } elseif (!empty($this->args[0])) { $name = $this->params['name'] = $this->args[0]; } + if (strpos($name, '.')) { - list($this->params['plugin'], $this->params['name']) = explode('.', $name); - $name = $this->params['name']; + list($this->params['plugin'], $splitName) = pluginSplit($name); + $name = $this->params['name'] = $splitName; } if ($name) { diff --git a/cake/console/libs/shell.php b/cake/console/libs/shell.php index 0a481db60..41b24200e 100644 --- a/cake/console/libs/shell.php +++ b/cake/console/libs/shell.php @@ -254,11 +254,7 @@ class Shell extends Object { $this->modelClass = $modelClassName; foreach ($uses as $modelClass) { - $plugin = null; - if (strpos($modelClass, '.') !== false) { - list($plugin, $modelClass) = explode('.', $modelClass); - $plugin = $plugin . '.'; - } + list($plugin, $modelClass) = pluginSplit($modelClass, true); if (PHP5) { $this->{$modelClass} = ClassRegistry::init($plugin . $modelClass); } else { diff --git a/cake/libs/cache.php b/cake/libs/cache.php index af4abec38..fca1c2607 100644 --- a/cake/libs/cache.php +++ b/cake/libs/cache.php @@ -170,11 +170,8 @@ class Cache { * @static */ function engine($name = 'File', $settings = array()) { - $plugin = null; $class = $name; - if (strpos($name, '.') !== false) { - list($plugin, $class) = explode('.', $name); - } + list($plugin, $class) = pluginSplit($name); $cacheClass = $class . 'Engine'; $_this =& Cache::getInstance(); if (!isset($_this->_Engine[$name])) { diff --git a/cake/libs/cake_log.php b/cake/libs/cake_log.php index 802034643..d0d586775 100644 --- a/cake/libs/cake_log.php +++ b/cake/libs/cake_log.php @@ -100,10 +100,8 @@ class CakeLog { * @access protected */ function _getLogger($loggerName) { - $plugin = null; - if (strpos($loggerName, '.') !== false) { - list($plugin, $loggerName) = explode('.', $loggerName); - } + list($plugin, $loggerName) = pluginSplit($loggerName); + if ($plugin) { App::import('Lib', $plugin . '.log/' . $loggerName); } else { diff --git a/cake/libs/class_registry.php b/cake/libs/class_registry.php index b6ebccf65..4d9e98f16 100644 --- a/cake/libs/class_registry.php +++ b/cake/libs/class_registry.php @@ -118,12 +118,12 @@ class ClassRegistry { foreach ($objects as $key => $settings) { if (is_array($settings)) { - $plugin = $pluginPath = null; + $pluginPath = null; $settings = array_merge($defaults, $settings); $class = $settings['class']; - - if (strpos($class, '.') !== false) { - list($plugin, $class) = explode('.', $class); + + list($plugin, $class) = pluginSplit($class); + if ($plugin) { $pluginPath = $plugin . '.'; } diff --git a/cake/libs/configure.php b/cake/libs/configure.php index 7c919de01..bcf748e94 100644 --- a/cake/libs/configure.php +++ b/cake/libs/configure.php @@ -206,14 +206,14 @@ class Configure extends Object { * @access public */ function load($fileName) { - $found = $pluginPath = false; - if (strpos($fileName, '.') !== false) { - $plugin = explode('.', $fileName, 2); - $pluginPath = App::pluginPath($plugin[0]); + $found = $plugin = $pluginPath = false; + list($plugin, $fileName) = pluginSplit($fileName); + if ($plugin) { + $pluginPath = App::pluginPath($plugin); } - if ($pluginPath && file_exists($pluginPath . 'config' . DS . $plugin[1] . '.php')) { - include($pluginPath . 'config' . DS . $plugin[1] . '.php'); + if ($pluginPath && file_exists($pluginPath . 'config' . DS . $fileName . '.php')) { + include($pluginPath . 'config' . DS . $fileName . '.php'); $found = true; } elseif (file_exists(CONFIGS . $fileName . '.php')) { include(CONFIGS . $fileName . '.php'); @@ -266,7 +266,7 @@ class Configure extends Object { * Used to write a config file to disk. * * {{{ - * Configure::store('Model', 'class.paths', array('Users' => array( + * Configure::store('Model', 'class_paths', array('Users' => array( * 'path' => 'users', 'plugin' => true * ))); * }}} diff --git a/cake/libs/controller/component.php b/cake/libs/controller/component.php index 081c81a5e..6c26a9ddd 100644 --- a/cake/libs/controller/component.php +++ b/cake/libs/controller/component.php @@ -189,16 +189,8 @@ class Component extends Object { $normal = Set::merge(array('Session' => null), $normal); } foreach ((array)$normal as $component => $config) { - $plugin = null; - - if (isset($this->__controllerVars['plugin'])) { - $plugin = $this->__controllerVars['plugin'] . '.'; - } - - if (strpos($component, '.') !== false) { - list($plugin, $component) = explode('.', $component); - $plugin = $plugin . '.'; - } + $plugin = isset($this->__controllerVars['plugin']) ? $this->__controllerVars['plugin'] . '.' : null; + list($plugin, $component) = pluginSplit($component, true, $plugin); $componentCn = $component . 'Component'; if (!class_exists($componentCn)) { diff --git a/cake/libs/controller/components/acl.php b/cake/libs/controller/components/acl.php index a50ba1a1d..900ddf879 100644 --- a/cake/libs/controller/components/acl.php +++ b/cake/libs/controller/components/acl.php @@ -46,9 +46,7 @@ class AclComponent extends Object { $name = Inflector::camelize(strtolower(Configure::read('Acl.classname'))); if (!class_exists($name)) { if (App::import('Component', $name)) { - if (strpos($name, '.') !== false) { - list($plugin, $name) = explode('.', $name); - } + list($plugin, $name) = pluginSplit($name); $name .= 'Component'; } else { trigger_error(sprintf(__('Could not find %s.', true), $name), E_USER_WARNING); diff --git a/cake/libs/controller/components/email.php b/cake/libs/controller/components/email.php index 10f2ef41f..21473e63c 100644 --- a/cake/libs/controller/components/email.php +++ b/cake/libs/controller/components/email.php @@ -391,9 +391,7 @@ class EmailComponent extends Object{ $viewClass = $this->Controller->view; if ($viewClass != 'View') { - if (strpos($viewClass, '.') !== false) { - list($plugin, $viewClass) = explode('.', $viewClass); - } + list($plugin, $viewClass) = pluginSplit($viewClass); $viewClass = $viewClass . 'View'; App::import('View', $this->Controller->view); } diff --git a/cake/libs/controller/controller.php b/cake/libs/controller/controller.php index 33688efe0..26e84e562 100644 --- a/cake/libs/controller/controller.php +++ b/cake/libs/controller/controller.php @@ -528,11 +528,7 @@ class Controller extends Object { $plugin = $this->plugin . '.'; } } - - if (strpos($modelClass, '.') !== false) { - list($plugin, $modelClass) = explode('.', $modelClass); - $plugin = $plugin . '.'; - } + list($plugin, $modelClass) = pluginSplit($modelClass, true, $plugin); if ($this->persistModel === true) { $cached = $this->_persist($modelClass, null, $object); @@ -808,9 +804,7 @@ class Controller extends Object { $viewClass = $this->view; if ($this->view != 'View') { - if (strpos($viewClass, '.') !== false) { - list($plugin, $viewClass) = explode('.', $viewClass); - } + list($plugin, $viewClass) = pluginSplit($viewClass); $viewClass = $viewClass . 'View'; App::import('View', $this->view); } @@ -1003,9 +997,8 @@ class Controller extends Object { if (is_string($object)) { $assoc = null; - - if (strpos($object, '.') !== false) { - list($object, $assoc) = explode('.', $object); + if (strpos($object, '.') !== false) { + list($object, $assoc) = pluginSplit($object); } if ($assoc && isset($this->{$object}->{$assoc})) { diff --git a/cake/libs/model/model.php b/cake/libs/model/model.php index e20db4810..953480478 100644 --- a/cake/libs/model/model.php +++ b/cake/libs/model/model.php @@ -641,20 +641,15 @@ class Model extends Overloadable { if (strpos($assoc, '.') !== false) { $value = $this->{$type}[$assoc]; unset($this->{$type}[$assoc]); - list($plugin, $assoc) = explode('.', $assoc); + list($plugin, $assoc) = pluginSplit($assoc, true); $this->{$type}[$assoc] = $value; - $plugin = $plugin . '.'; } } $className = $assoc; - if (isset($value['className']) && !empty($value['className'])) { - $className = $value['className']; - if (strpos($className, '.') !== false) { - list($plugin, $className) = explode('.', $className); - $plugin = $plugin . '.'; - $this->{$type}[$assoc]['className'] = $className; - } + if (!empty($value['className'])) { + list($plugin, $className) = pluginSplit($value['className'], true); + $this->{$type}[$assoc]['className'] = $className; } $this->__constructLinkedModel($assoc, $plugin . $className); } @@ -753,13 +748,8 @@ class Model extends Overloadable { if (is_array($joinClass)) { $joinClass = key($joinClass); } - $plugin = null; - - if (strpos($joinClass, '.') !== false) { - list($plugin, $joinClass) = explode('.', $joinClass); - $plugin = $plugin . '.'; - $this->{$type}[$assocKey]['with'] = $joinClass; - } + list($plugin, $joinClass) = pluginSplit($joinClass, true); + $this->{$type}[$assocKey]['with'] = $joinClass; if (!ClassRegistry::isKeySet($joinClass) && $dynamicWith === true) { $this->{$joinClass} = new AppModel(array( diff --git a/cake/libs/model/model_behavior.php b/cake/libs/model/model_behavior.php index e1bfb3666..e5fd759de 100644 --- a/cake/libs/model/model_behavior.php +++ b/cake/libs/model/model_behavior.php @@ -279,10 +279,7 @@ class BehaviorCollection extends Object { * @access public */ function attach($behavior, $config = array()) { - $name = $behavior; - if (strpos($behavior, '.')) { - list($plugin, $name) = explode('.', $behavior, 2); - } + list($plugin, $name) = pluginSplit($behavior); $class = $name . 'Behavior'; if (!App::import('Behavior', $behavior)) { diff --git a/cake/libs/view/helpers/js.php b/cake/libs/view/helpers/js.php index 1352074c8..ccd955fc1 100644 --- a/cake/libs/view/helpers/js.php +++ b/cake/libs/view/helpers/js.php @@ -87,9 +87,8 @@ class JsHelper extends AppHelper { $className = $settings; } $engineName = $className; - if (strpos($className, '.') !== false) { - list($plugin, $className) = explode('.', $className); - } + list($plugin, $className) = pluginSplit($className); + $this->__engineName = $className . 'Engine'; $engineClass = $engineName . 'Engine'; $this->helpers[] = $engineClass; diff --git a/cake/libs/view/view.php b/cake/libs/view/view.php index af8d49127..3cafd8bb9 100644 --- a/cake/libs/view/view.php +++ b/cake/libs/view/view.php @@ -750,18 +750,14 @@ class View extends Object { $options = $helper; $helper = $i; } - $plugin = $this->plugin; - - if (strpos($helper, '.') !== false) { - list($plugin, $helper) = explode('.', $helper); - } + list($plugin, $helper) = pluginSplit($helper, true, $this->plugin); $helperCn = $helper . 'Helper'; if (!isset($loaded[$helper])) { if (!class_exists($helperCn)) { $isLoaded = false; if (!is_null($plugin)) { - $isLoaded = App::import('Helper', $plugin . '.' . $helper); + $isLoaded = App::import('Helper', $plugin . $helper); } if (!$isLoaded) { if (!App::import('Helper', $helper)) { diff --git a/cake/tests/cases/libs/configure.test.php b/cake/tests/cases/libs/configure.test.php index 6cb18032c..eb73981a2 100644 --- a/cake/tests/cases/libs/configure.test.php +++ b/cake/tests/cases/libs/configure.test.php @@ -262,9 +262,9 @@ class ConfigureTest extends CakeTestCase { $this->assertEqual($config, $expected); $expected = array('data' => array('first' => 'value', 'second' => 'value2')); - Configure::store('AnotherExample', 'test.config', $expected); + Configure::store('AnotherExample', 'test_config', $expected); - Configure::load('test.config'); + Configure::load('test_config'); $config = Configure::read('AnotherExample'); $this->assertEqual($config, $expected); }