moving some bootstrapped stuff to dispatch and updating tests, added paginator counter to bake index template, added option to do "bake controller Posts admin" for baking just admin methods

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5490 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
gwoo 2007-08-05 02:03:18 +00:00
parent a03b688062
commit 43b3b1a0fe
8 changed files with 213 additions and 192 deletions

View file

@ -650,7 +650,7 @@
*/
function listClasses($path) {
$dir = opendir($path);
$classes=array();
$classes = array();
while (false !== ($file = readdir($dir))) {
if ((substr($file, -3, 3) == 'php') && substr($file, 0, 1) != '.') {
$classes[] = $file;
@ -977,81 +977,20 @@
return $r;
}
/**
* Returns the REQUEST_URI from the server environment, or, failing that,
* constructs a new one, using the PHP_SELF constant and other variables.
* see Dispatcher::uri();
*
* @return string URI
* @deprecated
*/
function setUri() {
if (env('HTTP_X_REWRITE_URL')) {
$uri = env('HTTP_X_REWRITE_URL');
} elseif (env('REQUEST_URI')) {
$uri = env('REQUEST_URI');
} else {
if ($uri = env('argv')) {
if (defined('SERVER_IIS') && SERVER_IIS) {
if (key($_GET) && strpos(key($_GET), '?') !== false) {
unset($_GET[key($_GET)]);
}
$uri = preg_split('/\?/', $uri[0], 2);
if (isset($uri[1])) {
foreach (preg_split('/&/', $uri[1]) as $var) {
@list($key, $val) = explode('=', $var);
$_GET[$key] = $val;
}
}
$uri = BASE_URL . $uri[0];
} else {
$uri = env('PHP_SELF') . '/' . $uri[0];
}
} else {
$uri = env('PHP_SELF') . '/' . env('QUERY_STRING');
}
}
return str_replace('//', '/', preg_replace('/\?url=/', '/', $uri));
return null;
}
/**
* Returns and sets the $_GET[url] derived from the REQUEST_INFO
* see Dispatcher::getUrl();
*
* @param string $uri
* @return string URL
* @deprecated
*/
function setUrl($uri = null, $script = null) {
if ($uri == null) {
$uri = setUri();
}
if ($script == null) {
if (defined('BASE_URL')) {
$script = BASE_URL;
} else {
$script = env('SCRIPT_NAME');
}
}
$url = null;
if ($uri === '/' || $uri === $script || $uri === '/'.APP_DIR.'/') {
$url = $_GET['url'] = '/';
} else {
if (strpos($uri, $script) !== false) {
$elements = explode($script, $uri);
} elseif (strpos($uri, APP_DIR) !== false) {
$elements = explode(APP_DIR, $uri);
} elseif (preg_match('/^[\/\?\/|\/\?|\?\/]/', $uri)) {
$elements = array(1 => preg_replace('/^[\/\?\/|\/\?|\?\/]/', '', $uri));
} else {
$elements = array();
}
if (!empty($elements[1])) {
$_GET['url'] = $elements[1];
$url = $elements[1];
} else {
$url = $_GET['url'] = '/';
}
if (strpos($url, '/') === 0 && $url != '/') {
$url = $_GET['url'] = substr($url, 1);
}
}
return $url;
function setUrl() {
return null;
}
/**
* Gets an environment variable from available sources, and provides emulation
@ -1577,13 +1516,12 @@
return $string;
}
/**
* chmod recursively on a directory
* See Folder::chmod
*
* @param string $path Path to chmod
* @param int $mode Mode to apply
* @return boolean Success
* @deprecated
*/
function chmodr($path, $mode = 0755) {
trigger_error("Deprecated. See Folder::chmod()", E_USER_ERROR);
if (!is_dir($path)) {
return chmod($path, $mode);
}

View file

@ -34,17 +34,16 @@ if (!defined('PHP5')) {
*/
if (!isset($bootstrap)) {
require CORE_PATH . 'cake' . DS . 'basics.php';
$TIME_START = getMicrotime();
require APP_PATH . 'config' . DS . 'core.php';
require CORE_PATH . 'cake' . DS . 'config' . DS . 'paths.php';
require LIBS . 'object.php';
require LIBS . 'configure.php';
}
$TIME_START = getMicrotime();
require LIBS . 'cache.php';
require LIBS . 'session.php';
require LIBS . 'security.php';
require LIBS . 'inflector.php';
$paths = Configure::getInstance();
if (isset($cakeCache)) {
$cache = 'File';
@ -60,6 +59,7 @@ if (!defined('PHP5')) {
} else {
Cache::engine();
}
Configure::store(null, 'class.paths');
Configure::load('class.paths');
Configure::write('debug', DEBUG);
@ -69,53 +69,7 @@ if (!defined('PHP5')) {
if (!defined('SERVER_IIS') && php_sapi_name() == 'isapi') {
define('SERVER_IIS', true);
}
/**
* Get the application path and request URL
*/
if (empty($uri) && defined('BASE_URL')) {
$url = setUrl();
} else {
if (empty($_GET['url'])) {
$url = null;
} else {
$url = $_GET['url'];
}
}
if (strpos($url, 'ccss/') === 0) {
include WWW_ROOT . DS . 'css.php';
exit();
}
$folders = array('js' => 'text/javascript', 'css' => 'text/css');
$requestPath = explode('/', $url);
if (in_array($requestPath[0], array_keys($folders))) {
if (file_exists(VENDORS . join(DS, $requestPath))) {
header('Content-type: ' . $folders[$requestPath[0]]);
include (VENDORS . join(DS, $requestPath));
exit();
}
}
require CAKE . 'dispatcher.php';
if (defined('CACHE_CHECK') && CACHE_CHECK === true) {
if (empty($uri)) {
$uri = setUri();
}
$filename = CACHE . 'views' . DS . convertSlash($uri) . '.php';
if (file_exists($filename)) {
uses('controller' . DS . 'component', DS . 'view' . DS . 'view');
$v = null;
$view = new View($v);
$view->renderCache($filename, $TIME_START);
} elseif (file_exists(CACHE . 'views' . DS . convertSlash($uri) . '_index.php')) {
uses('controller' . DS . 'component', DS . 'view' . DS . 'view');
$v = null;
$view = new View($v);
$view->renderCache(CACHE . 'views' . DS . convertSlash($uri) . '_index.php', $TIME_START);
}
}
?>

View file

@ -64,7 +64,7 @@ class ControllerTask extends Shell {
} else {
$actions = 'scaffold';
}
if (isset($this->args[2]) && $this->args[2] == 'admin') {
if ((isset($this->args[1]) && $this->args[1] == 'admin') || (isset($this->args[2]) && $this->args[2] == 'admin')) {
if ($admin = $this->getAdmin()) {
$this->out('Adding ' . CAKE_ADMIN .' methods');
if ($actions == 'scaffold') {

View file

@ -26,6 +26,13 @@
?>
<div class="<?php echo $pluralVar;?>">
<h2><?php echo "<?php __('{$pluralHumanName}');?>";?></h2>
<p>
<?php echo "<?php
echo $paginator->counter(array(
'format' => 'Page %page% of %pages%, showing %current% records out of %count% total, starting on record %start%, ending on %end%'
));
?>";?>
</p>
<table cellpadding="0" cellspacing="0">
<tr>
<?php foreach ($fields as $field):?>

View file

@ -47,6 +47,13 @@ class Dispatcher extends Object {
* @access public
*/
var $base = false;
/**
* webroot path
*
* @var string
* @access public
*/
var $webroot = '/';
/**
* Current URL
*
@ -88,13 +95,10 @@ class Dispatcher extends Object {
*/
function __construct($url = null, $base = false) {
parent::__construct();
if($base !== false) {
Configure::write('App.base', $base);
}
$this->base = Configure::read('App.base');
if ($url !== null) {
return $this->dispatch($url);
}
@ -112,13 +116,22 @@ class Dispatcher extends Object {
* @return boolean Success
* @access public
*/
function dispatch($url, $additionalParams = array()) {
$this->params = array_merge($this->parseParams($url), $additionalParams);
$missingAction = $missingView = $privateAction = false;
function dispatch($url = null, $additionalParams = array()) {
if ($this->base === false) {
$this->base = $this->baseUrl();
$controller = $this->__getController();
}
if ($url !== null) {
$_GET['url'] = $url;
}
$url = $this->getUrl();
$this->here = $this->base . '/' . $url;
$this->cached();
$this->params = array_merge($this->parseParams($url), $additionalParams);
$controller = $this->__getController();
if(!is_object($controller)) {
if (preg_match('/([\\.]+)/', $controller)) {
Router::setRequestInfo(array($this->params, array('base' => $this->base, 'webroot' => $this->webroot)));
@ -139,23 +152,21 @@ class Dispatcher extends Object {
}
}
$missingAction = $missingView = $privateAction = false;
if (empty($this->params['action'])) {
$this->params['action'] = 'index';
}
if (defined('CAKE_ADMIN')) {
if (isset($this->params[CAKE_ADMIN])) {
$this->admin = '/'.CAKE_ADMIN ;
$url = preg_replace('/'.CAKE_ADMIN.'(\/|$)/', '', $url);
$this->params['action'] = CAKE_ADMIN.'_'.$this->params['action'];
} elseif (strpos($this->params['action'], CAKE_ADMIN) === 0) {
$this->admin = CAKE_ADMIN ;
if (isset($this->params[$this->admin])) {
$this->params['action'] = $this->admin.'_'.$this->params['action'];
} elseif (strpos($this->params['action'], $this->admin) === 0) {
$privateAction = true;
}
}
$this->here = $this->base . $this->admin . '/' . $url;
$protected = array('constructclasses', 'redirect', 'set', 'setAction', 'isauthorized', 'validate', 'validateerrors',
'render', 'referer', 'disablecache', 'flash', 'generatefieldnames', 'postconditions', 'cleanupfields',
'paginate', 'beforefilter', 'beforerender', 'afterfilter', 'object', 'tostring', 'requestaction', 'log',
@ -402,9 +413,10 @@ class Dispatcher extends Object {
$base = '';
$this->webroot = '/';
$baseUrl = Configure::read('App.baseUrl');
$app = Configure::read('App.dir');
$webroot = Configure::read('App.webroot');
$config = Configure::read('App');
$baseUrl = $config['baseUrl'];
$app = $config['dir'];
$webroot = $config['webroot'];
$file = $script = null;
if (!$baseUrl) {
@ -417,18 +429,13 @@ class Dispatcher extends Object {
}
$base = dirname($base);
if (in_array($base, array(DS, '.'))) {
$base = '';
$this->webroot = '/';
return $base . $file;
}
if(strpos($script, $app) !== false && $app === 'app') {
$base = str_replace('/'.$app, '', $base);
}
if ($webroot === 'webroot') {
if (!$baseUrl && $webroot === 'webroot') {
$base = str_replace('/'.$webroot, '', $base);
}
@ -441,7 +448,6 @@ class Dispatcher extends Object {
if (strpos($this->webroot, $app) === false) {
$this->webroot .= $app . '/' ;
}
if (strpos($this->webroot, $webroot) === false) {
$this->webroot .= $webroot . '/';
}
@ -507,7 +513,7 @@ class Dispatcher extends Object {
return $controller;
}
if(!class_exists(low($ctrlClass))) {
if (!class_exists(low($ctrlClass))) {
$controller = Inflector::camelize($this->params['controller']);
$this->plugin = null;
return $controller;
@ -516,6 +522,121 @@ class Dispatcher extends Object {
$this->params = $params;
return $controller;
}
/**
* Returns the REQUEST_URI from the server environment, or, failing that,
* constructs a new one, using the PHP_SELF constant and other variables.
*
* @return string URI
*/
function uri() {
if ($uri = env('HTTP_X_REWRITE_URL')) {
} elseif ($uri = env('REQUEST_URI')) {
} else {
if ($uri = env('argv')) {
if (defined('SERVER_IIS') && SERVER_IIS) {
if (key($_GET) && strpos(key($_GET), '?') !== false) {
unset($_GET[key($_GET)]);
}
$uri = preg_split('/\?/', $uri[0], 2);
if (isset($uri[1])) {
foreach (preg_split('/&/', $uri[1]) as $var) {
@list($key, $val) = explode('=', $var);
$_GET[$key] = $val;
}
}
$uri = $this->base . $uri[0];
} else {
$uri = env('PHP_SELF') . '/' . $uri[0];
}
} else {
$uri = env('PHP_SELF') . '/' . env('QUERY_STRING');
}
}
return str_replace('//', '/', preg_replace('/\?url=/', '/', $uri));
}
/**
* Returns and sets the $_GET[url] derived from the REQUEST_URI
*
* @param string $uri
* @param string $script
* @return string URL
*/
function getUrl($uri = null, $base = null) {
if (empty($_GET['url'])) {
if ($uri == null) {
$uri = $this->uri();
}
if ($base == null) {
$base = $this->base;
}
$url = null;
if ($uri === '/' || $uri == dirname($base).'/' || $url == $base) {
$url = $_GET['url'] = '/';
} else {
if (strpos($uri, $base) !== false) {
$elements = explode($base, $uri);
} elseif (preg_match('/^[\/\?\/|\/\?|\?\/]/', $uri)) {
$elements = array(1 => preg_replace('/^[\/\?\/|\/\?|\?\/]/', '', $uri));
} else {
$elements = array();
}
if (!empty($elements[1])) {
$_GET['url'] = $elements[1];
$url = $elements[1];
} else {
$url = $_GET['url'] = '/';
}
if (strpos($url, '/') === 0 && $url != '/') {
$url = $_GET['url'] = substr($url, 1);
}
}
} else {
$url = $_GET['url'];
}
if($url{0} == '/') {
$url = substr($url, 1);
}
return $url;
}
/**
* Outputs cached dispatch for js, css, view cache
*
* @param string $url
* @return string URL
*/
function cached($uri = null) {
if($uri == null) {
$uri = $this->here;
}
if (strpos($uri, 'ccss/') === 0) {
include WWW_ROOT . DS . 'css.php';
exit();
}
$folders = array('js' => 'text/javascript', 'css' => 'text/css');
$requestPath = explode('/', $uri);
if (in_array($requestPath[0], array_keys($folders))) {
if (file_exists(VENDORS . join(DS, $requestPath))) {
header('Content-type: ' . $folders[$requestPath[0]]);
include (VENDORS . join(DS, $requestPath));
exit();
}
}
if (defined('CACHE_CHECK') && CACHE_CHECK === true) {
$filename = CACHE . 'views' . DS . convertSlash($uri) . '.php';
if (!file_exists($filename)) {
$filename = CACHE . 'views' . DS . convertSlash($uri) . '_index.php';
}
if (file_exists($filename)) {
uses('controller' . DS . 'component', DS . 'view' . DS . 'view');
$v = null;
$view = new View($v);
$view->renderCache($filename, getMicrotime());
}
}
}
}
?>

View file

@ -24,7 +24,11 @@
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
?>
<?php Debugger::checkSessionKey(); ?>
<?php
if(Configure::read() > 0):
Debugger::checkSessionKey();
endif;
?>
<p>
<span class="notice">
<?php

View file

@ -195,24 +195,28 @@ class DispatcherTest extends UnitTestCase {
$this->assertPattern('/\\A(?:0000400)\\z/', $test['pass'][5]);
}
function testSetUrl() {
function testGetUrl() {
$dispatcher =& new Dispatcher();
$dispatcher->base = '/app/webroot/index.php';
$uri = '/app/webroot/index.php/posts/add';
$_SERVER['SCRIPT_NAME'] = '/app/webroot/index.php';
$result = setUrl($uri);
$result = $dispatcher->getUrl($uri);
$expected = 'posts/add';
$this->assertEqual($expected, $result);
$uri = APP_DIR . '/posts/add';
$_SERVER['SCRIPT_NAME'] = APP_DIR . '/webroot/index.php';
$result = setUrl($uri);
$expected = 'posts/add';
$this->assertEqual($expected, $result);
Configure::write('App.baseUrl', '/app/webroot/index.php');
$uri = '/posts/add';
$_SERVER['SCRIPT_NAME'] = '/app/webroot/index.php';
$result = setUrl($uri);
$result = $dispatcher->getUrl($uri);
$expected = 'posts/add';
$this->assertEqual($expected, $result);
$_GET['url'] = array();
Configure::write('App.base', '/control');
$dispatcher =& new Dispatcher();
$uri = '/control/students/browse';
$result = $dispatcher->getUrl($uri);
$expected = 'students/browse';
$this->assertEqual($expected, $result);
}
function testBaseUrlAndWebrootWithModRewrite() {
@ -299,14 +303,14 @@ class DispatcherTest extends UnitTestCase {
Configure::write('App.baseUrl', '/app/webroot/index.php');
$result = $dispatcher->baseUrl();
$expected = '/app/index.php';
$expected = '/app/webroot/index.php';
$this->assertEqual($expected, $result);
$expectedWebroot = '/app/webroot/';
$this->assertEqual($expectedWebroot, $dispatcher->webroot);
Configure::write('App.baseUrl', '/app/webroot/test.php');
$result = $dispatcher->baseUrl();
$expected = '/app/test.php';
$expected = '/app/webroot/test.php';
$this->assertEqual($expected, $result);
$expectedWebroot = '/app/webroot/';
$this->assertEqual($expectedWebroot, $dispatcher->webroot);
@ -322,12 +326,12 @@ class DispatcherTest extends UnitTestCase {
$result = $dispatcher->baseUrl();
$expected = '/index.php';
$this->assertEqual($expected, $result);
$expectedWebroot = '/';
$expectedWebroot = '/app/webroot/';
$this->assertEqual($expectedWebroot, $dispatcher->webroot);
Configure::write('App.baseUrl', '/CakeBB/app/webroot/index.php');
$result = $dispatcher->baseUrl();
$expected = '/CakeBB/app/index.php';
$expected = '/CakeBB/app/webroot/index.php';
$this->assertEqual($expected, $result);
$expectedWebroot = '/CakeBB/app/webroot/';
$this->assertEqual($expectedWebroot, $dispatcher->webroot);
@ -339,7 +343,6 @@ class DispatcherTest extends UnitTestCase {
$expectedWebroot = '/CakeBB/app/webroot/';
$this->assertEqual($expectedWebroot, $dispatcher->webroot);
Configure::write('App.baseUrl', '/CakeBB/index.php');
$result = $dispatcher->baseUrl();
$expected = '/CakeBB/index.php';
@ -377,9 +380,8 @@ class DispatcherTest extends UnitTestCase {
function testMissingController() {
$dispatcher =& new TestDispatcher();
$dispatcher->base = '/index.php';
$url = setUrl('/some_controller/home/param:value/param2:value2');
Configure::write('App.baseUrl','/index.php');
$url = 'some_controller/home/param:value/param2:value2';
restore_error_handler();
$controller = $dispatcher->dispatch($url, array('return'=> 1));
set_error_handler('simpleTestErrorHandler');
@ -390,8 +392,8 @@ class DispatcherTest extends UnitTestCase {
function testPrivate() {
$dispatcher =& new TestDispatcher();
$dispatcher->base = '/index.php';
$url = setUrl('/some_pages/redirect/param:value/param2:value2');
Configure::write('App.baseUrl','/index.php');
$url = 'some_pages/redirect/param:value/param2:value2';
restore_error_handler();
@$controller = $dispatcher->dispatch($url, array('return'=> 1));
@ -403,8 +405,8 @@ class DispatcherTest extends UnitTestCase {
function testMissingAction() {
$dispatcher =& new TestDispatcher();
$dispatcher->base = '/index.php';
$url = setUrl('/some_pages/home/param:value/param2:value2');
Configure::write('App.baseUrl','/index.php');
$url = 'some_pages/home/param:value/param2:value2';
restore_error_handler();
@$controller = $dispatcher->dispatch($url, array('return'=> 1));
@ -415,8 +417,8 @@ class DispatcherTest extends UnitTestCase {
function testDispatch() {
$dispatcher =& new TestDispatcher();
$dispatcher->base = '/index.php';
$url = setUrl('/pages/home/param:value/param2:value2');
Configure::write('App.baseUrl','/index.php');
$url = 'pages/home/param:value/param2:value2';
restore_error_handler();
@$controller = $dispatcher->dispatch($url, array('return'=> 1));
@ -431,17 +433,12 @@ class DispatcherTest extends UnitTestCase {
function testAdminDispatch() {
$_POST = array();
if (!defined('CAKE_ADMIN')) {
define('CAKE_ADMIN', 'admin');
}
$_SERVER['DOCUMENT_ROOT'] = '';
$_SERVER['SCRIPT_FILENAME'] = '/cake/repo/branches/1.2.x.x/app/webroot/index.php';
$dispatcher =& new TestDispatcher();
$dispatcher->base = false;
$url = setUrl('/admin/test_dispatch_pages/index/param:value/param2:value2');
Configure::write('App.baseUrl','/cake/repo/branches/1.2.x.x/index.php');
$url = 'admin/test_dispatch_pages/index/param:value/param2:value2';
Router::reload();
$Router =& Router::getInstance();
if (defined('CAKE_ADMIN')) {
@ -468,10 +465,10 @@ class DispatcherTest extends UnitTestCase {
$expected = 'admin';
$this->assertIdentical($expected, $controller->params['admin']);
$expected = '/cake/repo/branches/1.2.x.x/admin/test_dispatch_pages/index/param:value/param2:value2';
$expected = '/cake/repo/branches/1.2.x.x/index.php/admin/test_dispatch_pages/index/param:value/param2:value2';
$this->assertIdentical($expected, $controller->here);
$expected = '/cake/repo/branches/1.2.x.x';
$expected = '/cake/repo/branches/1.2.x.x/index.php';
$this->assertIdentical($expected, $controller->base);
}
@ -486,7 +483,7 @@ class DispatcherTest extends UnitTestCase {
Router::connect('/my_plugin/:controller/*', array('plugin'=>'my_plugin', 'controller'=>'pages', 'action'=>'display'));
$dispatcher->base = false;
$url = setUrl('/my_plugin/some_pages/home/param:value/param2:value2');
$url = 'my_plugin/some_pages/home/param:value/param2:value2';
restore_error_handler();
@$controller = $dispatcher->dispatch($url, array('return'=> 1));
@ -530,7 +527,7 @@ class DispatcherTest extends UnitTestCase {
$dispatcher =& new TestDispatcher();
$dispatcher->base = false;
$url = setUrl('/my_plugin/other_pages/index/param:value/param2:value2');
$url = 'my_plugin/other_pages/index/param:value/param2:value2';
restore_error_handler();
@$controller = $dispatcher->dispatch($url, array('return'=> 1));
@ -564,7 +561,7 @@ class DispatcherTest extends UnitTestCase {
$dispatcher =& new TestDispatcher();
$dispatcher->base = false;
$url = setUrl('/my_plugin/add/param:value/param2:value2');
$url = 'my_plugin/add/param:value/param2:value2';
restore_error_handler();
@$controller = $dispatcher->dispatch($url, array('return'=> 1));
@ -589,7 +586,7 @@ class DispatcherTest extends UnitTestCase {
$dispatcher =& new TestDispatcher();
$dispatcher->base = false;
$url = setUrl('/my_plugin/param:value/param2:value2');
$url = 'my_plugin/param:value/param2:value2';
restore_error_handler();
@$controller = $dispatcher->dispatch($url, array('return'=> 1));
set_error_handler('simpleTestErrorHandler');

View file

@ -52,13 +52,13 @@
define('CORE_PATH', CAKE_CORE_INCLUDE_PATH . DS);
}
require CORE_PATH . 'cake' . DS . 'basics.php';
$TIME_START = getMicrotime();
require APP_PATH . 'config' . DS . 'core.php';
require CORE_PATH . 'cake' . DS . 'config' . DS . 'paths.php';
require LIBS . 'object.php';
require LIBS . 'configure.php';
$bootstrap = true;
$url = setUrl();
$url = null;
require APP_DIR . DS . WEBROOT_DIR . DS . 'index.php';
?>