Navigation Menu

Skip to content

Commit

Permalink
Deprecate view, layout, theme, autoLayout properties of controller.
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Jun 16, 2015
1 parent 00d6e25 commit d058fe1
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 33 deletions.
52 changes: 37 additions & 15 deletions src/Controller/Controller.php
Expand Up @@ -183,21 +183,18 @@ class Controller implements EventListenerInterface, EventDispatcherInterface

/**
* The path to this controllers view templates.
* Example `Articles`
*
* Set automatically using conventions in Controller::__construct().
* Example `Articles`.
*
* @var string
*/
public $viewPath;

/**
* The name of the view file to render. The name specified
* is the filename in /app/Template/<SubFolder> without the .ctp extension.
* The name of the layouts subfolder containing layouts.
*
* @var string
*/
public $view = null;
public $layoutPath;

/**
* Instance of the View created during rendering. Won't be set until after
Expand All @@ -215,8 +212,7 @@ class Controller implements EventListenerInterface, EventDispatcherInterface
* @see \Cake\View\View
*/
protected $_validViewOptions = [
'viewVars', 'autoLayout', 'helpers', 'view', 'layout', 'name', 'theme', 'layoutPath',
'viewPath', 'plugin', 'passedArgs'
'viewVars', 'helpers', 'name', 'layoutPath', 'viewPath', 'plugin', 'passedArgs'
];

/**
Expand Down Expand Up @@ -334,26 +330,51 @@ public function loadComponent($name, array $config = [])
*/
public function __get($name)
{
if (in_array($name, ['layout', 'view', 'theme', 'autoLayout'], true)) {
trigger_error(
sprintf('Controller::$%s is deprecated. Use $this->getView()->$%s instead.', $name, $name),
E_USER_DEPRECATED
);
return $this->getView()->{$name};
}

list($plugin, $class) = pluginSplit($this->modelClass, true);
if ($class !== $name) {
return false;
}
return $this->loadModel($plugin . $class);
}

/**
* Magic setter for removed properties.
*
* @param string $name Property name.
* @param mixed $value Value to set.
* @return void
*/
public function __set($name, $value)
{
if (in_array($name, ['layout', 'view', 'theme', 'autoLayout'], true)) {
trigger_error(
sprintf('Controller::$%s is deprecated. Use $this->getView()->$%s instead.', $name, $name),
E_USER_DEPRECATED
);
$this->getView()->{$name} = $value;
return;
}

$this->{$name} = $value;
}

/**
* Sets the request objects and configures a number of controller properties
* based on the contents of the request. Controller acts as a proxy for certain View variables
* which must also be updated here. The properties that get set are:
*
* - $this->request - To the $request parameter
* - $this->plugin - To the $request->params['plugin']
* - $this->autoRender - To false if $request->params['return'] == 1
* - $this->passedArgs - The combined results of params['named'] and params['pass]
* - View::$passedArgs - $this->passedArgs
* - $this->passedArgs - Same as $request->params['pass]
* - View::$plugin - $this->plugin
* - View::$view - To the $request->params['action']
* - View::$autoLayout - To the false if $request->params['bare']; is set.
*
* @param \Cake\Network\Request $request Request instance.
* @return void
Expand All @@ -362,7 +383,6 @@ public function setRequest(Request $request)
{
$this->request = $request;
$this->plugin = isset($request->params['plugin']) ? $request->params['plugin'] : null;
$this->view = isset($request->params['action']) ? $request->params['action'] : null;

if (isset($request->params['pass'])) {
$this->passedArgs = $request->params['pass'];
Expand Down Expand Up @@ -544,7 +564,6 @@ public function redirect($url, $status = 302)
public function setAction($action)
{
$this->request->params['action'] = $action;
$this->view = $action;
$args = func_get_args();
unset($args[0]);
return call_user_func_array([&$this, $action], $args);
Expand Down Expand Up @@ -577,6 +596,9 @@ public function render($view = null, $layout = null)

$this->autoRender = false;
$this->View = $this->getView();
if ($this->_view->view === null) {
$this->_view->view = isset($this->request->params['action']) ? $this->request->params['action'] : null;
}
$this->response->body($this->_view->render($view, $layout));
return $this->response;
}
Expand Down
10 changes: 10 additions & 0 deletions src/View/ViewVarsTrait.php
Expand Up @@ -59,6 +59,11 @@ public function getView($viewClass = null)
{
if ($viewClass === null && $this->_view) {
$this->_view->viewVars = $this->viewVars;
foreach (['viewPath', 'layoutPath'] as $var) {
if (isset($this->{$var})) {
$this->_view->{$var} = $this->{$var};
}
}
return $this->_view;
}

Expand All @@ -83,6 +88,11 @@ public function getView($viewClass = null)

if ($this->_view && $this->_view instanceof $className) {
$this->_view->viewVars = $this->viewVars;
foreach (['viewPath', 'layoutPath'] as $var) {
if (isset($this->{$var})) {
$this->_view->{$var} = $this->{$var};
}
}
return $this->_view;
}

Expand Down
4 changes: 1 addition & 3 deletions tests/TestCase/Controller/ControllerTest.php
Expand Up @@ -384,15 +384,14 @@ public function testRender()
$result = $Controller->render('index');
$this->assertRegExp('/posts index/', (string)$result);

$Controller->view = 'index';
$Controller->getView()->view = 'index';
$Controller->getView()->hasRendered = false;
$result = $Controller->render();
$this->assertRegExp('/posts index/', (string)$result);

$Controller->getView()->hasRendered = false;
$result = $Controller->render('/Element/test_element');
$this->assertRegExp('/this is the test element/', (string)$result);
$Controller->view = null;
}

/**
Expand Down Expand Up @@ -642,7 +641,6 @@ public function testSetAction()
$expected = ['testId' => 1, 'test2Id' => 2];
$this->assertSame($expected, $TestController->request->data);
$this->assertSame('view', $TestController->request->params['action']);
$this->assertSame('view', $TestController->view);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions tests/TestCase/View/ViewTest.php
Expand Up @@ -1275,8 +1275,8 @@ public function testRender()
*/
public function testRenderUsingViewProperty()
{
$this->PostsController->view = 'cache_form';
$View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
$View->view = 'cache_form';

$this->assertEquals('cache_form', $View->view);
$result = $View->render();
Expand Down Expand Up @@ -1828,10 +1828,10 @@ public function testMemoryLeakInPaths()
$this->ThemeController->plugin = null;
$this->ThemeController->name = 'Posts';
$this->ThemeController->viewPath = 'Posts';
$this->ThemeController->layout = 'whatever';
$this->ThemeController->theme = 'TestTheme';

$View = $this->ThemeController->createView();
$View->layout = 'whatever';
$View->theme = 'TestTheme';
$View->element('test_element');

$start = memory_get_usage();
Expand Down
4 changes: 0 additions & 4 deletions tests/TestCase/View/ViewVarsTraitTest.php
Expand Up @@ -169,14 +169,10 @@ public function testUptoDateViewVars()
{
$expected = ['one' => 'one'];
$this->subject->set($expected);
$this->subject->getView();

$this->assertEquals($expected, $this->subject->getView()->viewVars);

$expected = ['one' => 'one', 'two' => 'two'];
$this->subject->set($expected);
$this->subject->getView();

$this->assertEquals($expected, $this->subject->getView()->viewVars);
}

Expand Down
Expand Up @@ -25,13 +25,6 @@
class RequestHandlerTestController extends Controller
{

/**
* uses property
*
* @var mixed
*/
public $uses = null;

/**
* test method for ajax redirection
*
Expand Down Expand Up @@ -63,7 +56,7 @@ public function param_method($one = null, $two = null)
*/
public function ajax2_layout()
{
$this->layout = 'ajax2';
$this->getView()->layout = 'ajax2';
$this->destination();
}
}

0 comments on commit d058fe1

Please sign in to comment.