Browse Source

Split View::x() into getter/setter

Michael Hoffmann 9 years ago
parent
commit
63b801e8cf
3 changed files with 223 additions and 7 deletions
  1. 4 4
      src/Mailer/Email.php
  2. 143 3
      src/View/View.php
  3. 76 0
      tests/TestCase/View/ViewTest.php

+ 4 - 4
src/Mailer/Email.php

@@ -2605,8 +2605,8 @@ class Email implements JsonSerializable, Serializable
 
         $View = $this->createView();
 
-        list($templatePlugin) = pluginSplit($View->template());
-        list($layoutPlugin) = pluginSplit($View->layout());
+        list($templatePlugin) = pluginSplit($View->getTemplate());
+        list($layoutPlugin) = pluginSplit($View->getLayout());
         if ($templatePlugin) {
             $View->plugin = $templatePlugin;
         } elseif ($layoutPlugin) {
@@ -2619,8 +2619,8 @@ class Email implements JsonSerializable, Serializable
 
         foreach ($types as $type) {
             $View->hasRendered = false;
-            $View->templatePath('Email' . DIRECTORY_SEPARATOR . $type);
-            $View->layoutPath('Email' . DIRECTORY_SEPARATOR . $type);
+            $View->setTemplatePath('Email' . DIRECTORY_SEPARATOR . $type);
+            $View->setLayoutPath('Email' . DIRECTORY_SEPARATOR . $type);
 
             $render = $View->render();
             $render = str_replace(["\r\n", "\r"], "\n", $render);

+ 143 - 3
src/View/View.php

@@ -47,7 +47,7 @@ use RuntimeException;
  * ```
  * public function beforeRender(\Cake\Event\Event $event)
  * {
- *      $this->viewBuilder()->theme('SuperHot');
+ *      $this->viewBuilder()->setTheme('SuperHot');
  * }
  * ```
  *
@@ -323,10 +323,10 @@ class View implements EventDispatcherInterface
         array $viewOptions = []
     ) {
         if (isset($viewOptions['view'])) {
-            $this->template($viewOptions['view']);
+            $this->setTemplate($viewOptions['view']);
         }
         if (isset($viewOptions['viewPath'])) {
-            $this->templatePath($viewOptions['viewPath']);
+            $this->setTemplatePath($viewOptions['viewPath']);
         }
         foreach ($this->_passedVars as $var) {
             if (isset($viewOptions[$var])) {
@@ -363,8 +363,30 @@ class View implements EventDispatcherInterface
     }
 
     /**
+     * Get path for templates files.
+     *
+     * @return string
+     */
+    public function getTemplatePath()
+    {
+        return $this->templatePath;
+    }
+
+    /**
+     * Set path for templates files.
+     *
+     * @param string $path Path for template files.
+     * @return void
+     */
+    public function setTemplatePath($path)
+    {
+        $this->templatePath = $path;
+    }
+
+    /**
      * Get/set path for templates files.
      *
+     * @deprecated 3.5.0 Use getTemplatePath()/setTemplatePath() instead.
      * @param string|null $path Path for template files. If null returns current path.
      * @return string|null
      */
@@ -378,8 +400,30 @@ class View implements EventDispatcherInterface
     }
 
     /**
+     * Get path for layout files.
+     *
+     * @return string
+     */
+    public function getLayoutPath()
+    {
+        return $this->layoutPath;
+    }
+
+    /**
+     * Set path for layout files.
+     *
+     * @param string $path Path for layout files.
+     * @return void
+     */
+    public function setLayoutPath($path)
+    {
+        $this->layoutPath = $path;
+    }
+
+    /**
      * Get/set path for layout files.
      *
+     * @deprecated 3.5.0 Use getLayoutPath()/setLayoutPath() instead.
      * @param string|null $path Path for layout files. If null returns current path.
      * @return string|null
      */
@@ -393,10 +437,34 @@ class View implements EventDispatcherInterface
     }
 
     /**
+     * Get the current state of auto layout.
+     *
+     * @return bool
+     */
+    public function getAutoLayout()
+    {
+        return $this->autoLayout;
+    }
+
+    /**
      * Turns on or off CakePHP's conventional mode of applying layout files.
      * On by default. Setting to off means that layouts will not be
      * automatically applied to rendered templates.
      *
+     * @param bool $autoLayout Boolean to turn on/off.
+     * @return void
+     */
+    public function setAutoLayout($autoLayout)
+    {
+        $this->autoLayout = $autoLayout;
+    }
+
+    /**
+     * Turns on or off CakePHP's conventional mode of applying layout files.
+     * On by default. Setting to off means that layouts will not be
+     * automatically applied to rendered templates.
+     *
+     * @deprecated 3.5.0 Use getAutoLayout()/setAutoLayout() instead.
      * @param bool|null $autoLayout Boolean to turn on/off. If null returns current value.
      * @return bool|null
      */
@@ -410,8 +478,30 @@ class View implements EventDispatcherInterface
     }
 
     /**
+     * Get the current view theme.
+     *
+     * @return string
+     */
+    public function getTheme()
+    {
+        return $this->theme;
+    }
+
+    /**
+     * Set the view theme to use.
+     *
+     * @param string $theme Theme name.
+     * @return void
+     */
+    public function setTheme($theme)
+    {
+        $this->theme = $theme;
+    }
+
+    /**
      * The view theme to use.
      *
+     * @deprecated 3.5.0 Use getTheme()/setTheme() instead.
      * @param string|null $theme Theme name. If null returns current theme.
      * @return string|null
      */
@@ -425,9 +515,33 @@ class View implements EventDispatcherInterface
     }
 
     /**
+     * Get the name of the template file to render. The name specified is the
+     * filename in /src/Template/<SubFolder> without the .ctp extension.
+     *
+     * @return string
+     */
+    public function getTemplate()
+    {
+        return $this->template;
+    }
+
+    /**
+     * Set the name of the template file to render. The name specified is the
+     * filename in /src/Template/<SubFolder> without the .ctp extension.
+     *
+     * @param string $name Template file name to set.
+     * @return void
+     */
+    public function setTemplate($name)
+    {
+        $this->template = $name;
+    }
+
+    /**
      * Get/set the name of the template file to render. The name specified is the
      * filename in /src/Template/<SubFolder> without the .ctp extension.
      *
+     * @deprecated 3.5.0 Use getTemplate()/setTemplate() instead.
      * @param string|null $name Template file name to set. If null returns current name.
      * @return string|null
      */
@@ -441,10 +555,36 @@ class View implements EventDispatcherInterface
     }
 
     /**
+     * Get the name of the layout file to render the template inside of.
+     * The name specified is the filename of the layout in /src/Template/Layout
+     * without the .ctp extension.
+     *
+     * @return string
+     */
+    public function getLayout()
+    {
+        return $this->layout;
+    }
+
+    /**
+     * Set the name of the layout file to render the template inside of.
+     * The name specified is the filename of the layout in /src/Template/Layout
+     * without the .ctp extension.
+     *
+     * @param string $name Layout file name to set.
+     * @return void
+     */
+    public function setLayout($name)
+    {
+        $this->layout = $name;
+    }
+
+    /**
      * Get/set the name of the layout file to render the template inside of.
      * The name specified is the filename of the layout in /src/Template/Layout
      * without the .ctp extension.
      *
+     * @deprecated 3.5.0 Use getLayout()/setLayout() instead.
      * @param string|null $name Layout file name to set. If null returns current name.
      * @return string|null
      */

+ 76 - 0
tests/TestCase/View/ViewTest.php

@@ -1998,6 +1998,82 @@ TEXT;
     }
 
     /**
+     * Test getTemplatePath() and setTemplatePath().
+     *
+     * @return void
+     */
+    public function testGetSetTemplatePath()
+    {
+        $this->View->setTemplatePath('foo');
+        $templatePath = $this->View->getTemplatePath();
+        $this->assertSame($templatePath, 'foo');
+    }
+
+    /**
+     * Test getLayoutPath() and setLayoutPath().
+     *
+     * @return void
+     */
+    public function testGetSetLayoutPath()
+    {
+        $this->View->setLayoutPath('foo');
+        $layoutPath = $this->View->getLayoutPath();
+        $this->assertSame($layoutPath, 'foo');
+    }
+
+    /**
+     * Test getAutoLayout() and setAutoLayout().
+     *
+     * @return void
+     */
+    public function testGetSetAutoLayout()
+    {
+        $this->View->setAutoLayout(false);
+        $autoLayout = $this->View->getAutoLayout();
+        $this->assertSame($autoLayout, false);
+
+        $this->View->setAutoLayout(true);
+        $autoLayout = $this->View->getAutoLayout();
+        $this->assertSame($autoLayout, true);
+    }
+
+    /**
+     * Test getTheme() and setTheme().
+     *
+     * @return void
+     */
+    public function testGetSetTheme()
+    {
+        $this->View->setTheme('foo');
+        $theme = $this->View->getTheme();
+        $this->assertSame($theme, 'foo');
+    }
+
+    /**
+     * Test getTemplate() and setTemplate().
+     *
+     * @return void
+     */
+    public function testGetSetTemplate()
+    {
+        $this->View->setTemplate('foo');
+        $template = $this->View->getTemplate();
+        $this->assertSame($template, 'foo');
+    }
+
+    /**
+     * Test setLayout() and getLayout().
+     *
+     * @return void
+     */
+    public function testGetSetLayout()
+    {
+        $this->View->setLayout('foo');
+        $layout = $this->View->getLayout();
+        $this->assertSame($layout, 'foo');
+    }
+
+    /**
      * Test magic getter and setter for removed properties.
      *
      * @return void