diff --git a/cake/console/cake.php b/cake/console/cake.php index ca816879f..97000e02d 100644 --- a/cake/console/cake.php +++ b/cake/console/cake.php @@ -362,7 +362,7 @@ class ShellDispatcher { } $result = fgets($this->stdin); - if($result === false){ + if ($result === false){ exit; } $result = trim($result); @@ -439,22 +439,25 @@ class ShellDispatcher { function __parseParams($params) { $count = count($params); for ($i = 0; $i < $count; $i++) { - if (!empty($params[$i]) && $params[$i]{0} === '-') { - $key = substr($params[$i], 1); - $this->params[$key] = true; - unset($params[$i]); - if(isset($params[++$i])) { - if (!empty($params[$i]) && $params[$i]{0} !== '-') { - $this->params[$key] = str_replace('"', '', $params[$i]); - unset($params[$i]); - } else { - $i--; - $this->__parseParams($params); + if (isset($params[$i])) { + if ($params[$i]{0} === '-') { + $key = substr($params[$i], 1); + $this->params[$key] = true; + unset($params[$i]); + if (isset($params[++$i])) { + if ($params[$i]{0} !== '-') { + $this->params[$key] = str_replace('"', '', $params[$i]); + unset($params[$i]); + } else { + $i--; + $this->__parseParams($params); + } } + } else { + $this->args[] = $params[$i]; + unset($params[$i]); } - } else { - $this->args[] = $params[$i]; - unset($params[$i]); + } } } diff --git a/cake/console/libs/api.php b/cake/console/libs/api.php index 4b4018bb1..8a5f9af70 100644 --- a/cake/console/libs/api.php +++ b/cake/console/libs/api.php @@ -66,72 +66,74 @@ class ApiShell extends Shell { if (empty($this->args)) { return $this->help(); } - if (count($this->args) == 1 && in_array($this->args[0], array_keys($this->paths))) { - $this->args[1] = $this->args[0]; + + $type = low($this->args[0]); + + if (!isset($this->paths[$type])) { + $this->err(sprintf(__("%s could not be found", true), $path)); + exit(); } - if (count($this->args) > 1) { - $path = $this->args[0]; - $class = $this->args[1]; + $path = $this->paths[$type]; - $this->__loadDependencies($path); - - if (in_array(low($path), array('behavior', 'component', 'helper')) && low($path) !== low($class)) { - if (!preg_match('/' . Inflector::camelize($path) . '$/', $class)) { - $class .= Inflector::camelize($path); - } - } elseif (low($path) === low($class)) { - $class = Inflector::camelize($path); - } - - if (isset($this->paths[low($path)])) { - $path = $this->paths[low($path)]; - } - } else { - $class = $this->args[0]; - $path = LIBS; + if (count($this->args) == 1) { + $file = $type; + $class = Inflector::camelize($type); + } elseif (count($this->args) > 1) { + $file = Inflector::underscore($this->args[1]); + $class = Inflector::camelize($file); } - if (!is_readable($path) || !is_dir($path)) { - $this->err(sprintf(__('Path %s could not be accessed', true), $path)); - return; - } - - $File = null; - - $candidates = array( - Inflector::underscore($class), - substr(Inflector::underscore($class), 0, strpos(Inflector::underscore($class), '_')) - ); - - foreach ($candidates as $candidate) { - $File =& new File($path . $candidate . '.php'); - - if ($File->exists()) { - if (!class_exists($class)) { - include_once($File->pwd()); - } - if (class_exists($class)) { - break; + $objects = Configure::listObjects('class', $path); + if (in_array($class, $objects)) { + if (in_array($type, array('behavior', 'component', 'helper')) && $type !== $file) { + if (!preg_match('/' . Inflector::camelize($type) . '$/', $class)) { + $class .= Inflector::camelize($type); } } - $File = null; } - if (empty($File)) { - $this->err(sprintf(__('No file for class %s could be found', true), $class)); - return; - } - - $parsed = $this->__parseClass($File, $class); + $parsed = $this->__parseClass($path . $file .'.php'); if (!empty($parsed)) { - $this->out(ucwords($class)); - $this->hr(); + if (isset($this->params['m'])) { + if (!isset($parsed[$this->params['m']])) { + $this->err(sprintf(__("%s::%s() could not be found", true), $class, $this->params['m'])); + exit(); + } + $method = $parsed[$this->params['m']]; + $this->out($class .'::'.$method['method'] . $method['parameters']); + $this->hr(); + $this->out($method['comment'], true); + } else { + $this->out(ucwords($class)); + $this->hr(); + $i = 0; + foreach ($parsed as $method) { + $list[] = ++$i . ". " . $method['method'] . $method['parameters']; + } + $this->out($list); - foreach ($parsed as $method) { - $this->out("\t" . $method['method'] . "(" . $method['parameters'] . ")", true); + $methods = array_keys($parsed); + while ($number = $this->in(__('Select a number to see the more information about a specific method. q to quit. l to list.', true), null, 'q')) { + if ($number === 'q') { + $this->out(__('Done', true)); + exit(); + } + + if ($number === 'l') { + $this->out($list); + } + + if (isset($methods[--$number])) { + $method = $parsed[$methods[$number]]; + $this->hr(); + $this->out($class .'::'.$method['method'] . $method['parameters']); + $this->hr(); + $this->out($method['comment'], true); + } + } } } } @@ -142,7 +144,7 @@ class ApiShell extends Shell { * @access public */ function help() { - $head = "Usage: cake api [] \n"; + $head = "Usage: cake api [] <-m method>\n"; $head .= "-----------------------------------------------\n"; $head .= "Parameters:\n\n"; @@ -182,60 +184,32 @@ class ApiShell extends Shell { * @return array Methods and signatures indexed by method name * @access private */ - function __parseClass(&$File, $class) { + function __parseClass($path) { $parsed = array(); - if (get_parent_class($class)) { - $methods = am(array(), array_diff(get_class_methods($class), get_class_methods(get_parent_class($class)))); - } else { - $methods = get_class_methods($class); + $File = new File($path); + if (!$File->exists()) { + $this->err(sprintf(__("%s could not be found", true), $File->name)); + exit(); } $contents = $File->read(); - foreach ($methods as $method) { - if (strpos($method, '__') !== 0 && strpos($method, '_') !== 0) { - $regex = array( - '/\s+function\s+(' . preg_quote($method, '/') . ')\s*\(([^{]*)\)\s*{/is', - '/\s+function\s+(' . preg_quote('&' . $method, '/') . ')\s*\(([^{]*)\)\s*{/is' - ); + if (preg_match_all('%(/\\*\\*[\\s\\S]*?\\*/)(\\s+function\\s+\\w+)(\\(.+\\))%', $contents, $result, PREG_PATTERN_ORDER)) { + foreach($result[2] as $key => $method) { + $method = str_replace('function ', '', trim($method)); - if (preg_match($regex[0], $contents, $matches) || preg_match($regex[1], $contents, $matches)) { + if (strpos($method, '__') === false && strpos($method, '_') !== 0) { $parsed[$method] = array( - 'method' => trim($matches[1]), - 'parameters' => trim($matches[2]) - ); + 'comment' => r(array('/*', '*/', '*'), '', trim($result[1][$key])), + 'method' => $method, + 'parameters' => trim($result[3][$key]), + ); } } } - sort($parsed); + ksort($parsed); return $parsed; } - -/** - * Load dependencies for given element (controller/component/helper) - * - * @param string $element Element to load dependency for - * @access private - */ - function __loadDependencies($element) { - switch(low($element)) { - case 'behavior': - App::import('Model', 'AppModel'); - App::import('Model', 'ModelBehavior'); - break; - case 'controller': - App::import('Controller', 'AppController'); - break; - case 'component': - break; - case 'helper': - App::import('Helper', 'AppHelper'); - break; - case 'model': - App::import('Model', 'ModelBehavior'); - break; - } - } } ?> \ No newline at end of file diff --git a/cake/libs/configure.php b/cake/libs/configure.php index 1e1b6349d..4f0134fac 100644 --- a/cake/libs/configure.php +++ b/cake/libs/configure.php @@ -36,12 +36,19 @@ */ class Configure extends Object { /** - * Hold array with paths to view files + * Hold array with paths to model files * * @var array * @access public */ - var $viewPaths = array(); + var $modelPaths = array(); +/** + * Hold array with paths to behavior files + * + * @var array + * @access public + */ + var $behaviorPaths = array(); /** * Hold array with paths to controller files * @@ -50,12 +57,19 @@ class Configure extends Object { */ var $controllerPaths = array(); /** - * Hold array with paths to model files + * Hold array with paths to component files * * @var array * @access public */ - var $modelPaths = array(); + var $componentPaths = array(); +/** + * Hold array with paths to view files + * + * @var array + * @access public + */ + var $viewPaths = array(); /** * Hold array with paths to helper files * @@ -64,19 +78,19 @@ class Configure extends Object { */ var $helperPaths = array(); /** - * Hold array with paths to component files + * Hold array with paths to plugins * * @var array * @access public */ - var $componentPaths = array(); + var $pluginPaths = array(); /** - * Hold array with paths to behavior files + * Hold array with paths to vendor files * * @var array * @access public */ - var $behaviorPaths = array(); + var $vendorPaths = array(); /** * Current debug level * @@ -126,7 +140,7 @@ class Configure extends Object { $extension = false; $name = $type; - if($type === 'file' && !$path) { + if ($type === 'file' && !$path) { return false; } elseif ($type === 'file') { $extension = true; @@ -142,9 +156,13 @@ class Configure extends Object { $types = array( 'model' => array('suffix' => '.php', 'base' => 'AppModel'), + 'behavior' => array('suffix' => '.php', 'base' => 'ModelBehavior'), 'controller' => array('suffix' => '_controller.php', 'base' => 'AppController'), + 'component' => array('suffix' => '.php', 'base' => null), + 'view' => array('suffix' => '.php', 'base' => null), 'helper' => array('suffix' => '.php', 'base' => 'AppHelper'), 'plugin' => array('suffix' => '', 'base' => null), + 'vendor' => array('suffix' => '', 'base' => null), 'class' => array('suffix' => '.php', 'base' => null), 'file' => array('suffix' => '.php', 'base' => null) ); @@ -161,7 +179,6 @@ class Configure extends Object { $search = array_merge(array(APP), $_this->corePaths($type)); foreach ($search as $delete) { - if (is_array($path) && in_array($delete, $path)) { $remove = array_flip($path); unset($remove[$delete]); @@ -174,7 +191,7 @@ class Configure extends Object { $objects = am($items, $objects); } - if($type !== 'file') { + if ($type !== 'file') { $objects = array_map(array(&$Inflector, 'camelize'), $objects); } $_this->__objects[$name] = $objects; @@ -190,14 +207,14 @@ class Configure extends Object { * @return array List of directories or files in directory */ function __list($path, $suffix = false, $extension = false) { - if(!class_exists('folder')) { + if (!class_exists('folder')) { uses('folder'); } $items = array(); $Folder =& new Folder($path); $contents = $Folder->read(false, true); if (is_array($contents)) { - if(!$suffix) { + if (!$suffix) { return $contents[0]; } else { foreach($contents[1] as $item) { @@ -357,7 +374,7 @@ class Configure extends Object { } } - if(!$found) { + if (!$found) { return false; } @@ -530,13 +547,15 @@ class Configure extends Object { $_this =& Configure::getInstance(); $core = $_this->corePaths(); $basePaths = array( - 'plugin' => APP . 'plugins' . DS, + 'model' => array(MODELS), 'behavior' => array(BEHAVIORS), - 'component' => array(COMPONENTS), - 'helper' => array(HELPERS), 'controller' => array(CONTROLLERS), + 'component' => array(COMPONENTS), 'view' => array(VIEWS), - 'model' => array(MODELS)); + 'helper' => array(HELPERS), + 'plugin' => array(APP . 'plugins' . DS), + 'vendor' => array(APP . 'vendors' . DS, VENDORS), + ); foreach ($basePaths as $type => $default) { $pathsVar = $type . 'Paths'; @@ -571,7 +590,7 @@ class Configure extends Object { */ function __loadBootstrap($boot) { $_this =& Configure::getInstance(false); - $modelPaths = $viewPaths = $controllerPaths = $helperPaths = $componentPaths = $behaviorPaths = $pluginPaths = null; + $modelPaths = $behaviorPaths = $controllerPaths = $componentPaths = $viewPaths = $helperPaths = $pluginPaths = $vendorPaths = null; if ($boot) { $_this->write('App', array('base' => false, 'baseUrl' => false, 'dir' => APP_DIR, 'webroot' => WEBROOT_DIR)); @@ -586,7 +605,7 @@ class Configure extends Object { if ($_this->read('Cache.disable') !== true) { $cache = Cache::settings(); - if(empty($cache)) { + if (empty($cache)) { trigger_error('Cache not configured properly. Please check Cache::config(); in APP/config/core.php', E_USER_WARNING); list($engine, $cache) = Cache::config('default', array('engine' => 'File')); } @@ -747,7 +766,7 @@ class App extends Object { $tempType = $value[0]; $plugin = $value[1] . '.'; $class = $value[2]; - } elseif ($count === 2 && ($type === 'Core' || $type === 'File')){ + } elseif ($count === 2 && ($type === 'Core' || $type === 'File')) { $tempType = $value[0]; $class = $value[1]; } else { @@ -775,7 +794,7 @@ class App extends Object { if ($load = $_this->__mapped($name . $ext['class'], $type, $plugin)) { if ($_this->__load($load)) { $_this->__overload($type, $name . $ext['class']); - if($_this->return) { + if ($_this->return) { $value = include $load; return $value; } @@ -813,7 +832,7 @@ class App extends Object { $_this->__cache = true; $_this->__map($directory . $file, $name . $ext['class'], $type, $plugin); $_this->__overload($type, $name . $ext['class']); - if( $_this->return) { + if ( $_this->return) { $value = include $directory . $file; return $value; } @@ -894,7 +913,7 @@ class App extends Object { function __load($file) { $_this =& App::getInstance(); if (file_exists($file)) { - if(!$_this->return) { + if (!$_this->return) { require($file); } return true; @@ -1000,9 +1019,10 @@ class App extends Object { } return array('class' => null, 'suffix' => null, 'path' => $path); break; - case 'view': + case 'behavior': + $_this->import($type, 'Behavior', false); if ($plugin) { - $path = $plugin . DS . 'views' . DS; + $path = $plugin . DS . 'models' . DS . 'behaviors' . DS; } return array('class' => $type, 'suffix' => null, 'path' => $path); break; @@ -1014,23 +1034,22 @@ class App extends Object { } return array('class' => $type, 'suffix' => $type, 'path' => $path); break; - case 'helper': - $_this->import($type, 'AppHelper', false); - if ($plugin) { - $path = $plugin . DS . 'views' . DS . 'helpers' . DS; - } - return array('class' => $type, 'suffix' => null, 'path' => $path); - break; case 'component': if ($plugin) { $path = $plugin . DS . 'controllers' . DS . 'components' . DS; } return array('class' => $type, 'suffix' => null, 'path' => $path); break; - case 'behavior': - $_this->import($type, 'Behavior', false); + case 'view': if ($plugin) { - $path = $plugin . DS . 'models' . DS . 'behaviors' . DS; + $path = $plugin . DS . 'views' . DS; + } + return array('class' => $type, 'suffix' => null, 'path' => $path); + break; + case 'helper': + $_this->import($type, 'AppHelper', false); + if ($plugin) { + $path = $plugin . DS . 'views' . DS . 'helpers' . DS; } return array('class' => $type, 'suffix' => null, 'path' => $path); break;