Merge branch '2.1' of github.com:cakephp/cakephp into 2.1

This commit is contained in:
Jose Lorenzo Rodriguez 2011-11-28 22:30:36 -04:30
commit 76e334ca38
16 changed files with 477 additions and 17 deletions

View file

@ -218,8 +218,7 @@ class SchemaShell extends Shell {
}
}
$db = ConnectionManager::getDataSource($this->Schema->connection);
$contents = "#" . $Schema->name . " sql generated on: " . date('Y-m-d H:i:s') . " : " . time() . "\n\n";
$contents .= $db->dropSchema($Schema) . "\n\n". $db->createSchema($Schema);
$contents = "\n\n" . $db->dropSchema($Schema) . "\n\n". $db->createSchema($Schema);
if ($write) {
if (strpos($write, '.sql') === false) {

View file

@ -20,8 +20,6 @@
*/
?>
<?php echo '<?php' . "\n"; ?>
/* <?php echo $model; ?> Fixture generated on: <?php echo date('Y-m-d H:i:s') . " : ". time(); ?> */
/**
* <?php echo $model; ?>Fixture
*

View file

@ -18,7 +18,6 @@
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
echo "<?php\n";
echo "/* ". $className ." Test cases generated on: " . date('Y-m-d H:i:s') . " : ". time() . "*/\n";
?>
App::uses('<?php echo $fullClassName; ?>', '<?php echo $realType; ?>');

View file

@ -42,9 +42,17 @@ class CakeErrorController extends AppController {
*/
public function __construct($request = null, $response = null) {
parent::__construct($request, $response);
if (count(Router::extensions())) {
$this->components[] = 'RequestHandler';
}
$this->constructClasses();
$this->Components->trigger('initialize', array(&$this));
$this->_set(array('cacheAction' => false, 'viewPath' => 'Errors'));
if (isset($this->RequestHandler)) {
$this->RequestHandler->startup($this);
}
}
/**

View file

@ -556,7 +556,12 @@ class RequestHandlerComponent extends Component {
}
$controller->ext = '.ctp';
if (empty($this->_renderType)) {
$viewClass = Inflector::classify($type);
App::uses($viewClass . 'View', 'View');
if (class_exists($viewClass . 'View')) {
$controller->viewClass = $viewClass;
} elseif (empty($this->_renderType)) {
$controller->viewPath .= DS . $type;
} else {
$remove = preg_replace("/([\/\\\\]{$this->_renderType})$/", DS . $type, $controller->viewPath);

View file

@ -181,6 +181,7 @@ class ExceptionRenderer {
'url' => h($url),
'name' => $error->getMessage(),
'error' => $error,
'serialize' => array('code', 'url', 'name')
));
try {
$this->controller->set($error->getAttributes());
@ -208,7 +209,8 @@ class ExceptionRenderer {
$this->controller->set(array(
'name' => $message,
'url' => h($url),
'error' => $error,
'error' => $error,
'serialize' => array('name', 'url')
));
$this->_outputMessage('error400');
}
@ -231,6 +233,7 @@ class ExceptionRenderer {
'name' => $message,
'message' => h($url),
'error' => $error,
'serialize' => array('name', 'message')
));
$this->_outputMessage('error500');
}
@ -250,6 +253,7 @@ class ExceptionRenderer {
'url' => h($url),
'name' => $error->getMessage(),
'error' => $error,
'serialize' => array('code', 'url', 'name', 'error')
));
try {
$this->_outputMessage($this->template);

View file

@ -379,7 +379,7 @@ class CakeSchema extends Object {
$out .= "}\n";
$file = new SplFileObject($path . DS . $file, 'w+');
$content = "<?php \n/* {$name} schema generated on: " . date('Y-m-d H:i:s') . " : ". time() . "*/\n{$out}?>";
$content = "<?php\n{$out}";
if ($file->fwrite($content)) {
return $content;
}

View file

@ -412,8 +412,7 @@ class RequestHandlerComponentTest extends CakeTestCase {
$this->RequestHandler->renderAs($this->Controller, 'xml', array('attachment' => 'myfile.xml'));
$expected = 'RequestHandlerTest' . DS . 'xml';
$this->assertEquals($expected, $this->Controller->viewPath);
$this->assertEquals('Xml', $this->Controller->viewClass);
}
/**
@ -469,13 +468,13 @@ class RequestHandlerComponentTest extends CakeTestCase {
* @return void
*/
public function testRenderAsCalledTwice() {
$this->RequestHandler->renderAs($this->Controller, 'xml');
$this->assertEquals($this->Controller->viewPath, 'RequestHandlerTest' . DS . 'xml');
$this->assertEquals($this->Controller->layoutPath, 'xml');
$this->RequestHandler->renderAs($this->Controller, 'print');
$this->assertEquals('RequestHandlerTest' . DS . 'print', $this->Controller->viewPath);
$this->assertEquals('print', $this->Controller->layoutPath);
$this->RequestHandler->renderAs($this->Controller, 'js');
$this->assertEquals($this->Controller->viewPath, 'RequestHandlerTest' . DS . 'js');
$this->assertEquals($this->Controller->layoutPath, 'js');
$this->assertEquals('RequestHandlerTest' . DS . 'js', $this->Controller->viewPath);
$this->assertEquals('js', $this->Controller->layoutPath);
$this->assertTrue(in_array('Js', $this->Controller->helpers));
}

View file

@ -290,7 +290,7 @@ class ControllerTestCaseTest extends CakeTestCase {
$controller = $this->Case->generate('TestsApps');
$controller->Components->load('RequestHandler');
$result = $this->Case->testAction('/tests_apps/index.json', array('return' => 'view'));
$result = $this->Case->testAction('/tests_apps/index.json', array('return' => 'contents'));
$result = json_decode($result, true);
$expected = array('cakephp' => 'cool');
$this->assertEquals($expected, $result);

View file

@ -0,0 +1,101 @@
<?php
/**
* JsonViewTest file
*
* PHP 5
*
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
* @package Cake.Test.Case.View
* @since CakePHP(tm) v 2.1.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
App::uses('Controller', 'Controller');
App::uses('CakeRequest', 'Network');
App::uses('CakeResponse', 'Network');
App::uses('JsonView', 'View');
/**
* JsonViewTest
*
* @package Cake.Test.Case.View
*/
class JsonViewTest extends CakeTestCase {
/**
* testRenderWithoutView method
*
* @return void
*/
public function testRenderWithoutView() {
$Request = new CakeRequest();
$Response = new CakeResponse();
$Controller = new Controller($Request, $Response);
$data = array('user' => 'fake', 'list' => array('item1', 'item2'));
$Controller->set(array('data' => $data, '_serialize' => 'data'));
$View = new JsonView($Controller);
$output = $View->render(false);
$this->assertIdentical(json_encode($data), $output);
$this->assertIdentical('application/json', $Response->type());
}
/**
* Test render with an array in _serialize
*
* @return void
*/
public function testRenderWithoutViewMultiple() {
$Request = new CakeRequest();
$Response = new CakeResponse();
$Controller = new Controller($Request, $Response);
$data = array('no' => 'nope', 'user' => 'fake', 'list' => array('item1', 'item2'));
$Controller->set($data);
$Controller->set('_serialize', array('no', 'user'));
$View = new JsonView($Controller);
$output = $View->render(false);
$this->assertIdentical(json_encode(array('no' =>$data['no'], 'user' => $data['user'])), $output);
$this->assertIdentical('application/json', $Response->type());
}
/**
* testRenderWithView method
*
* @return void
*/
public function testRenderWithView() {
App::build(array(
'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
));
$Request = new CakeRequest();
$Response = new CakeResponse();
$Controller = new Controller($Request, $Response);
$Controller->name = $Controller->viewPath = 'Posts';
$data = array(
'User' => array(
'username' => 'fake'
),
'Item' => array(
array('name' => 'item1'),
array('name' => 'item2')
)
);
$Controller->set('user', $data);
$View = new JsonView($Controller);
$output = $View->render('index');
$expected = json_encode(array('user' => 'fake', 'list' => array('item1', 'item2')));
$this->assertIdentical($expected, $output);
$this->assertIdentical('application/json', $Response->type());
}
}

View file

@ -0,0 +1,109 @@
<?php
/**
* XmlViewTest file
*
* PHP 5
*
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
* @package Cake.Test.Case.View
* @since CakePHP(tm) v 2.1.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
App::uses('Controller', 'Controller');
App::uses('CakeRequest', 'Network');
App::uses('CakeResponse', 'Network');
App::uses('XmlView', 'View');
/**
* XmlViewTest
*
* @package Cake.Test.Case.View
*/
class XmlViewTest extends CakeTestCase {
/**
* testRenderWithoutView method
*
* @return void
*/
public function testRenderWithoutView() {
$Request = new CakeRequest();
$Response = new CakeResponse();
$Controller = new Controller($Request, $Response);
$data = array('users' => array('user' => array('user1', 'user2')));
$Controller->set(array('users' => $data, '_serialize' => 'users'));
$View = new XmlView($Controller);
$output = $View->render(false);
$expected = '<?xml version="1.0" encoding="UTF-8"?><users><user>user1</user><user>user2</user></users>';
$this->assertIdentical($expected, str_replace(array("\r", "\n"), '', $output));
$this->assertIdentical('application/xml', $Response->type());
}
/**
* Test render with an array in _serialize
*
* @return void
*/
public function testRenderWithoutViewMultiple() {
$Request = new CakeRequest();
$Response = new CakeResponse();
$Controller = new Controller($Request, $Response);
$data = array('no' => 'nope', 'user' => 'fake', 'list' => array('item1', 'item2'));
$Controller->set($data);
$Controller->set('_serialize', array('no', 'user'));
$View = new XmlView($Controller);
$output = $View->render(false);
$expected = array(
'response' => array('no' =>$data['no'], 'user' => $data['user'])
);
$this->assertIdentical(Xml::build($expected)->asXML(), $output);
$this->assertIdentical('application/xml', $Response->type());
}
/**
* testRenderWithView method
*
* @return void
*/
public function testRenderWithView() {
App::build(array('View' => array(
CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS
)));
$Request = new CakeRequest();
$Response = new CakeResponse();
$Controller = new Controller($Request, $Response);
$Controller->name = $Controller->viewPath = 'Posts';
$data = array(
array(
'User' => array(
'username' => 'user1'
)
),
array(
'User' => array(
'username' => 'user2'
)
)
);
$Controller->set('users', $data);
$View = new XmlView($Controller);
$output = $View->render('index');
$expected = '<?xml version="1.0" encoding="UTF-8"?><users><user>user1</user><user>user2</user></users>';
$this->assertIdentical($expected, str_replace(array("\r", "\n"), '', $output));
$this->assertIdentical('application/xml', $Response->type());
$this->assertInstanceOf('HelperCollection', $View->Helpers);
}
}

View file

@ -0,0 +1,27 @@
<?php
/**
*
* PHP 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package Cake.Test.test_app.View.Json
* @since CakePHP(tm) v 2.1.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
$formatted = array(
'user' => $user['User']['username'],
'list' => array()
);
foreach ($user['Item'] as $item) {
$formatted['list'][] = $item['name'];
}
echo json_encode($formatted);

View file

@ -0,0 +1,6 @@
<?php
$data = array('users' => array('user' => array()));
foreach ($users as $user) {
$data['users']['user'][] = array('@' => $user['User']['username']);
}
echo Xml::fromArray($data)->saveXml();

View file

@ -331,4 +331,4 @@ class Xml {
}
}
}
}

101
lib/Cake/View/JsonView.php Normal file
View file

@ -0,0 +1,101 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
App::uses('View', 'View');
/**
* A view class that is used for JSON responses.
*
* By setting the '_serialize' key in your controller, you can specify a view variable
* that should be serialized to JSON and used as the response for the request.
* This allows you to omit views + layouts, if your just need to emit a single view
* variable as the JSON response.
*
* In your controller, you could do the following:
*
* `$this->set(array('posts' => $posts, '_serialize' => 'posts'));`
*
* When the view is rendered, the `$posts` view variable will be serialized
* into JSON.
*
* You can also define `'_serialize'` as an array. This will create a top level object containing
* all the named view variables:
*
* {{{
* $this->set(compact('posts', 'users', 'stuff'));
* $this->set('_serialize', array('posts', 'users'));
* }}}
*
* The above would generate a JSON object that looks like:
*
* `{"posts": [...], "users": [...]}`
*
* If you don't use the `_serialize` key, you will need a view. You can use extended
* views to provide layout like functionality.
*
* @package Cake.View
* @since CakePHP(tm) v 2.1.0
*/
class JsonView extends View {
/**
* JSON views are always located in the 'json' sub directory for a
* controllers views.
*
* @var string
*/
public $subDir = 'json';
/**
* Constructor
*
* @param Controller $controller
*/
public function __construct($controller) {
parent::__construct($controller);
if (isset($controller->response) && $controller->response instanceof CakeResponse) {
$controller->response->type('json');
}
}
/**
* Render a JSON view.
*
* Uses the special '_serialize' parameter to convert a set of
* view variables into a JSON response. Makes generating simple
* JSON responses very easy. You can omit the '_serialize' parameter,
* and use a normal view + layout as well.
*
* @param string $view The view being rendered.
* @param string $layout The layout being rendered.
* @return string The rendered view.
*/
public function render($view = null, $layout = null) {
if (isset($this->viewVars['_serialize'])) {
$serialize = $this->viewVars['_serialize'];
if (is_array($serialize)) {
$data = array();
foreach ($serialize as $key) {
$data[$key] = $this->viewVars[$key];
}
} else {
$data = isset($this->viewVars[$serialize]) ? $this->viewVars[$serialize] : null;
}
return $this->output = json_encode($data);
}
if ($view !== false && $viewFileName = $this->_getViewFileName($view)) {
return $this->output = $this->_render($viewFileName);
}
}
}

104
lib/Cake/View/XmlView.php Normal file
View file

@ -0,0 +1,104 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
App::uses('View', 'View');
App::uses('Xml', 'Utility');
/**
* A view class that is used for creating XML responses.
*
* By setting the '_serialize' key in your controller, you can specify a view variable
* that should be serialized to XML and used as the response for the request.
* This allows you to omit views + layouts, if your just need to emit a single view
* variable as the XML response.
*
* In your controller, you could do the following:
*
* `$this->set(array('posts' => $posts, '_serialize' => 'posts'));`
*
* When the view is rendered, the `$posts` view variable will be serialized
* into XML.
*
* **Note** The view variable you specify must be compatible with Xml::fromArray().
*
* You can also define `'_serialize'` as an array. This will create an additional
* top level element named `<response>` containing all the named view variables:
*
* {{{
* $this->set(compact('posts', 'users', 'stuff'));
* $this->set('_serialize', array('posts', 'users'));
* }}}
*
* The above would generate a XML object that looks like:
*
* `<response><posts>...</posts><users>...</users></response>`
*
* If you don't use the `_serialize` key, you will need a view. You can use extended
* views to provide layout like functionality.
*
* @package Cake.View
* @since CakePHP(tm) v 2.1.0
*/
class XmlView extends View {
/**
* The subdirectory. XML views are always in xml.
*
* @var string
*/
public $subDir = 'xml';
/**
* Constructor
*
* @param Controller $controller
*/
public function __construct($controller) {
parent::__construct($controller);
if (isset($controller->response) && $controller->response instanceof CakeResponse) {
$controller->response->type('xml');
}
}
/**
* Render a XML view.
*
* Uses the special '_serialize' parameter to convert a set of
* view variables into a XML response. Makes generating simple
* XML responses very easy. You can omit the '_serialize' parameter,
* and use a normal view + layout as well.
*
* @param string $view The view being rendered.
* @param string $layout The layout being rendered.
* @return string The rendered view.
*/
public function render($view = null, $layout = null) {
if (isset($this->viewVars['_serialize'])) {
$serialize = $this->viewVars['_serialize'];
if (is_array($serialize)) {
$data = array('response' => array());
foreach ($serialize as $key) {
$data['response'][$key] = $this->viewVars[$key];
}
} else {
$data = isset($this->viewVars[$serialize]) ? $this->viewVars[$serialize] : null;
}
return $this->output = Xml::fromArray($data)->asXML();
}
if ($view !== false && $viewFileName = $this->_getViewFileName($view)) {
return $this->output = $this->_render($viewFileName);
}
}
}