Replacing duplicated code with pluginSplit().

Updating test case.
This commit is contained in:
mark_story 2009-11-15 19:55:20 -05:00
parent a292ef0f16
commit 0783176238
17 changed files with 42 additions and 92 deletions

View file

@ -318,12 +318,7 @@ class ShellDispatcher {
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';

View file

@ -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;
}
}

View file

@ -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) {

View file

@ -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 {

View file

@ -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])) {

View file

@ -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 {

View file

@ -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 . '.';
}

View file

@ -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
* )));
* }}}

View file

@ -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)) {

View file

@ -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);

View file

@ -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);
}

View file

@ -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})) {

View file

@ -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(

View file

@ -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)) {

View file

@ -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;

View file

@ -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)) {

View file

@ -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);
}