Browse Source

Remove Helper::$request use View::getRequest()/setRequest() instead.

ADmad 7 years ago
parent
commit
6a5fd9b840

+ 18 - 9
src/View/Helper.php

@@ -65,13 +65,6 @@ class Helper implements EventListenerInterface
     protected $_helperMap = [];
 
     /**
-     * Request object
-     *
-     * @var \Cake\Http\ServerRequest
-     */
-    public $request;
-
-    /**
      * Unused.
      *
      * @var array
@@ -103,8 +96,6 @@ class Helper implements EventListenerInterface
     public function __construct(View $View, array $config = [])
     {
         $this->_View = $View;
-        $this->request = $View->getRequest();
-
         $this->setConfig($config);
 
         if (!empty($this->helpers)) {
@@ -156,6 +147,14 @@ class Helper implements EventListenerInterface
             return $this->_View->{$method}();
         }
 
+        if ($name === 'request') {
+            deprecationWarning(
+                'Helper::$%s is removed. Use $view->%s() instead. Use $helper->getView()->getRequest() instead.'
+            );
+
+            return $this->_View->getRequest();
+        }
+
         if ($name === 'helpers') {
             deprecationWarning(
                 'Helper::$helpers is now protected and should not be accessed from outside a helper class.'
@@ -190,6 +189,16 @@ class Helper implements EventListenerInterface
             return;
         }
 
+        if ($name === 'request') {
+            deprecationWarning(
+                'Helper::$%s is removed. Use $view->%s() instead. Use $helper->getView()->setRequest() instead.'
+            );
+
+            $this->_View->setRequest($value);
+
+            return;
+        }
+
         if ($name === 'helpers') {
             deprecationWarning(
                 'Helper::$helpers is now protected and should not be accessed from outside a helper class.'

+ 5 - 3
src/View/Helper/FlashHelper.php

@@ -70,18 +70,20 @@ class FlashHelper extends Helper
      */
     public function render($key = 'flash', array $options = [])
     {
-        if (!$this->request->getSession()->check("Flash.$key")) {
+        $session = $this->_View->getRequest()->getSession();
+
+        if (!$session->check("Flash.$key")) {
             return null;
         }
 
-        $flash = $this->request->getSession()->read("Flash.$key");
+        $flash = $session->read("Flash.$key");
         if (!is_array($flash)) {
             throw new UnexpectedValueException(sprintf(
                 'Value for flash setting key "%s" must be an array.',
                 $key
             ));
         }
-        $this->request->getSession()->delete("Flash.$key");
+        $session->delete("Flash.$key");
 
         $out = '';
         foreach ($flash as $message) {

+ 19 - 14
src/View/Helper/FormHelper.php

@@ -451,7 +451,7 @@ class FormHelper extends Helper
         unset($options['templates']);
 
         if ($options['action'] === false || $options['url'] === false) {
-            $url = $this->request->getRequestTarget();
+            $url = $this->_View->getRequest()->getRequestTarget();
             $action = null;
         } else {
             $url = $this->_formUrl($context, $options);
@@ -529,8 +529,10 @@ class FormHelper extends Helper
      */
     protected function _formUrl($context, $options)
     {
+        $request = $this->_View->getRequest();
+
         if ($options['action'] === null && $options['url'] === null) {
-            return $this->request->getRequestTarget();
+            return $request->getRequestTarget();
         }
 
         if (is_string($options['url']) ||
@@ -545,8 +547,8 @@ class FormHelper extends Helper
 
         $actionDefaults = [
             'plugin' => $this->_View->getPlugin(),
-            'controller' => $this->request->getParam('controller'),
-            'action' => $this->request->getParam('action'),
+            'controller' => $request->getParam('controller'),
+            'action' => $request->getParam('action'),
         ];
 
         $action = (array)$options['url'] + $actionDefaults;
@@ -585,17 +587,19 @@ class FormHelper extends Helper
      */
     protected function _csrfField()
     {
-        if ($this->request->getParam('_Token.unlockedFields')) {
-            foreach ((array)$this->request->getParam('_Token.unlockedFields') as $unlocked) {
+        $request = $this->_View->getRequest();
+
+        if ($request->getParam('_Token.unlockedFields')) {
+            foreach ((array)$request->getParam('_Token.unlockedFields') as $unlocked) {
                 $this->_unlockedFields[] = $unlocked;
             }
         }
-        if (!$this->request->getParam('_csrfToken')) {
+        if (!$request->getParam('_csrfToken')) {
             return '';
         }
 
         return $this->hidden('_csrfToken', [
-            'value' => $this->request->getParam('_csrfToken'),
+            'value' => $request->getParam('_csrfToken'),
             'secure' => static::SECURE_SKIP,
             'autocomplete' => 'off',
         ]);
@@ -616,7 +620,7 @@ class FormHelper extends Helper
     {
         $out = '';
 
-        if ($this->requestType !== 'get' && $this->request->getParam('_Token')) {
+        if ($this->requestType !== 'get' && $this->_View->getRequest()->getParam('_Token')) {
             $out .= $this->secure($this->fields, $secureAttributes);
             $this->fields = [];
             $this->_unlockedFields = [];
@@ -649,7 +653,7 @@ class FormHelper extends Helper
      */
     public function secure(array $fields = [], array $secureAttributes = [])
     {
-        if (!$this->request->getParam('_Token')) {
+        if (!$this->_View->getRequest()->getParam('_Token')) {
             return '';
         }
         $debugSecurity = Configure::read('debug');
@@ -1096,7 +1100,7 @@ class FormHelper extends Helper
 
         if ($legend === true) {
             $isCreate = $context->isCreate();
-            $modelName = Inflector::humanize(Inflector::singularize($this->request->getParam('controller')));
+            $modelName = Inflector::humanize(Inflector::singularize($this->_View->getRequest()->getParam('controller')));
             if (!$isCreate) {
                 $legend = __d('cake', 'Edit {0}', $modelName);
             } else {
@@ -2654,7 +2658,7 @@ class FormHelper extends Helper
     protected function _initInputField($field, $options = [])
     {
         if (!isset($options['secure'])) {
-            $options['secure'] = (bool)$this->request->getParam('_Token');
+            $options['secure'] = (bool)$this->_View->getRequest()->getParam('_Token');
         }
         $context = $this->_getContext();
 
@@ -2830,7 +2834,8 @@ class FormHelper extends Helper
         }
         $data += ['entity' => null];
 
-        return $this->_context = $this->contextFactory()->get($this->request, $data);
+        return $this->_context = $this->contextFactory()
+            ->get($this->_View->getRequest(), $data);
     }
 
     /**
@@ -2950,7 +2955,7 @@ class FormHelper extends Helper
             }
             if (isset($valueMap[$valuesSource])) {
                 $method = $valueMap[$valuesSource];
-                $value = $this->request->{$method}($fieldname);
+                $value = $this->_View->getRequest()->{$method}($fieldname);
                 if ($value !== null) {
                     return $value;
                 }

+ 18 - 11
src/View/Helper/PaginatorHelper.php

@@ -100,11 +100,11 @@ class PaginatorHelper extends Helper
     {
         parent::__construct($View, $config);
 
-        $query = $this->request->getQueryParams();
+        $query = $this->_View->getRequest()->getQueryParams();
         unset($query['page'], $query['limit'], $query['sort'], $query['direction']);
         $this->setConfig(
             'options.url',
-            array_merge($this->request->getParam('pass', []), ['?' => $query])
+            array_merge($this->_View->getRequest()->getParam('pass', []), ['?' => $query])
         );
     }
 
@@ -116,14 +116,16 @@ class PaginatorHelper extends Helper
      */
     public function params($model = null)
     {
+        $request = $this->_View->getRequest();
+
         if (empty($model)) {
             $model = $this->defaultModel();
         }
-        if (!$this->request->getParam('paging') || !$this->request->getParam('paging.' . $model)) {
+        if (!$request->getParam('paging') || !$request->getParam('paging.' . $model)) {
             return [];
         }
 
-        return $this->request->getParam('paging.' . $model);
+        return $request->getParam('paging.' . $model);
     }
 
     /**
@@ -152,22 +154,27 @@ class PaginatorHelper extends Helper
      */
     public function options(array $options = [])
     {
+        $request = $this->_View->getRequest();
+
         if (!empty($options['paging'])) {
-            $this->request = $this->request->withParam(
+            $request = $request->withParam(
                 'paging',
-                $options['paging'] + $this->request->getParam('paging', [])
+                $options['paging'] + $request->getParam('paging', [])
             );
             unset($options['paging']);
         }
-        $model = $this->defaultModel();
 
+        $model = $this->defaultModel();
         if (!empty($options[$model])) {
-            $this->request = $this->request->withParam(
+            $request = $request->withParam(
                 'paging.' . $model,
-                $options[$model] + (array)$this->request->getParam('paging.' . $model, [])
+                $options[$model] + (array)$request->getParam('paging.' . $model, [])
             );
             unset($options[$model]);
         }
+
+        $this->_View->setRequest($request);
+
         $this->_config['options'] = array_filter($options + $this->_config['options']);
         if (empty($this->_config['options']['url'])) {
             $this->_config['options']['url'] = [];
@@ -647,10 +654,10 @@ class PaginatorHelper extends Helper
         if ($this->_defaultModel) {
             return $this->_defaultModel;
         }
-        if (!$this->request->getParam('paging')) {
+        if (!$this->_View->getRequest()->getParam('paging')) {
             return null;
         }
-        list($this->_defaultModel) = array_keys($this->request->getParam('paging'));
+        list($this->_defaultModel) = array_keys($this->_View->getRequest()->getParam('paging'));
 
         return $this->_defaultModel;
     }

+ 6 - 4
src/View/Helper/UrlHelper.php

@@ -238,7 +238,7 @@ class UrlHelper extends Helper
         $timestampEnabled = $timestamp === 'force' || ($timestamp === true && Configure::read('debug'));
         if ($timestampEnabled && strpos($path, '?') === false) {
             $filepath = preg_replace(
-                '/^' . preg_quote($this->request->getAttribute('webroot'), '/') . '/',
+                '/^' . preg_quote($this->_View->getRequest()->getAttribute('webroot'), '/') . '/',
                 '',
                 urldecode($path)
             );
@@ -268,9 +268,11 @@ class UrlHelper extends Helper
      */
     public function webroot($file)
     {
+        $request = $this->_View->getRequest();
+
         $asset = explode('?', $file);
         $asset[1] = isset($asset[1]) ? '?' . $asset[1] : null;
-        $webPath = $this->request->getAttribute('webroot') . $asset[0];
+        $webPath = $request->getAttribute('webroot') . $asset[0];
         $file = $asset[0];
 
         if (!empty($this->_View->getTheme())) {
@@ -282,12 +284,12 @@ class UrlHelper extends Helper
             }
 
             if (file_exists(Configure::read('App.wwwRoot') . $theme . $file)) {
-                $webPath = $this->request->getAttribute('webroot') . $theme . $asset[0];
+                $webPath = $request->getAttribute('webroot') . $theme . $asset[0];
             } else {
                 $themePath = Plugin::path($this->_View->getTheme());
                 $path = $themePath . 'webroot/' . $file;
                 if (file_exists($path)) {
-                    $webPath = $this->request->getAttribute('webroot') . $theme . $asset[0];
+                    $webPath = $request->getAttribute('webroot') . $theme . $asset[0];
                 }
             }
         }

File diff suppressed because it is too large
+ 204 - 145
tests/TestCase/View/Helper/FormHelperTest.php


+ 22 - 20
tests/TestCase/View/Helper/HtmlHelperTest.php

@@ -64,14 +64,15 @@ class HtmlHelperTest extends TestCase
     public function setUp()
     {
         parent::setUp();
+
+        $request = new ServerRequest([
+            'webroot' => '',
+        ]);
         $this->View = $this->getMockBuilder('Cake\View\View')
             ->setMethods(['append'])
+            ->setConstructorArgs([$request])
             ->getMock();
         $this->Html = new HtmlHelper($this->View);
-        $this->Html->request = new ServerRequest([
-            'webroot' => '',
-        ]);
-        $this->Html->Url->request = $this->Html->request;
 
         Plugin::load(['TestTheme']);
         static::setAppNamespace();
@@ -117,7 +118,7 @@ class HtmlHelperTest extends TestCase
     {
         Router::connect('/:controller/:action/*');
 
-        $this->Html->request = $this->Html->request->withAttribute('webroot', '');
+        $this->View->setRequest($this->View->getRequest()->withAttribute('webroot', ''));
 
         $result = $this->Html->link('/home');
         $expected = ['a' => ['href' => '/home'], 'preg:/\/home/', '/a'];
@@ -381,10 +382,10 @@ class HtmlHelperTest extends TestCase
      */
     public function testImageDataUriBaseDir()
     {
-        $request = $this->Html->request
+        $request = $this->View->getRequest()
             ->withAttribute('base', 'subdir')
             ->withAttribute('webroot', 'subdir/');
-        $this->Html->Url->request = $this->Html->request = $request;
+        $this->View->setRequest($request);
         Router::pushRequest($request);
 
         $data = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4' .
@@ -464,9 +465,10 @@ class HtmlHelperTest extends TestCase
         $expected = ['img' => ['src' => $here . 'img/sub/test.gif', 'alt' => '']];
         $this->assertHtml($expected, $result);
 
-        $this->Html->Url->request = $this->Html->request
+        $this->View->setRequest($this->View->getRequest()
             ->withAttribute('webroot', '/myproject/')
-            ->withAttribute('base', '/myproject');
+            ->withAttribute('base', '/myproject')
+        );
 
         $result = $this->Html->image('sub/test.gif', ['fullBase' => true]);
         $here = $this->Html->Url->build('/', true);
@@ -483,7 +485,7 @@ class HtmlHelperTest extends TestCase
     {
         Configure::write('Asset.timestamp', 'force');
 
-        $this->Html->Url->request = $this->Html->request->withAttribute('webroot', '/');
+        $this->View->setRequest($this->View->getRequest()->withAttribute('webroot', '/'));
         $result = $this->Html->image('cake.icon.png');
         $expected = ['img' => ['src' => 'preg:/\/img\/cake\.icon\.png\?\d+/', 'alt' => '']];
         $this->assertHtml($expected, $result);
@@ -495,7 +497,7 @@ class HtmlHelperTest extends TestCase
         $expected = ['img' => ['src' => 'preg:/\/img\/cake\.icon\.png\?\d+/', 'alt' => '']];
         $this->assertHtml($expected, $result);
 
-        $this->Html->Url->request = $this->Html->request->withAttribute('webroot', '/testing/longer/');
+        $this->View->setRequest($this->View->getRequest()->withAttribute('webroot', '/testing/longer/'));
         $result = $this->Html->image('cake.icon.png');
         $expected = [
             'img' => ['src' => 'preg:/\/testing\/longer\/img\/cake\.icon\.png\?[0-9]+/', 'alt' => '']
@@ -518,7 +520,7 @@ class HtmlHelperTest extends TestCase
         Configure::write('Asset.timestamp', true);
         Configure::write('debug', true);
 
-        $this->Html->Url->request = $this->Html->request->withAttribute('webroot', '/');
+        $this->View->setRequest($this->View->getRequest()->withAttribute('webroot', '/'));
         $this->Html->Url->getView()->setTheme('TestTheme');
         $result = $this->Html->image('__cake_test_image.gif');
         $expected = [
@@ -529,7 +531,7 @@ class HtmlHelperTest extends TestCase
         ];
         $this->assertHtml($expected, $result);
 
-        $this->Html->Url->request = $this->Html->request->withAttribute('webroot', '/testing/');
+        $this->View->setRequest($this->View->getRequest()->withAttribute('webroot', '/testing/'));
         $result = $this->Html->image('__cake_test_image.gif');
         $expected = [
         'img' => [
@@ -769,12 +771,12 @@ class HtmlHelperTest extends TestCase
         $expected['link']['href'] = 'preg:/.*css\/cake\.generic\.css\?[0-9]+/';
         $this->assertHtml($expected, $result);
 
-        $this->Html->Url->request = $this->Html->request->withAttribute('webroot', '/testing/');
+        $this->View->setRequest($this->View->getRequest()->withAttribute('webroot', '/testing/'));
         $result = $this->Html->css('cake.generic', ['once' => false]);
         $expected['link']['href'] = 'preg:/\/testing\/css\/cake\.generic\.css\?[0-9]+/';
         $this->assertHtml($expected, $result);
 
-        $this->Html->Url->request = $this->Html->request->withAttribute('webroot', '/testing/longer/');
+        $this->View->setRequest($this->View->getRequest()->withAttribute('webroot', '/testing/longer/'));
         $result = $this->Html->css('cake.generic', ['once' => false]);
         $expected['link']['href'] = 'preg:/\/testing\/longer\/css\/cake\.generic\.css\?[0-9]+/';
         $this->assertHtml($expected, $result);
@@ -812,12 +814,12 @@ class HtmlHelperTest extends TestCase
         $expected['link']['href'] = 'preg:/.*test_plugin\/css\/test_plugin_asset\.css\?[0-9]+/';
         $this->assertHtml($expected, $result);
 
-        $this->Html->Url->request = $this->Html->request->withAttribute('webroot', '/testing/');
+        $this->View->setRequest($this->View->getRequest()->withAttribute('webroot', '/testing/'));
         $result = $this->Html->css('TestPlugin.test_plugin_asset', ['once' => false]);
         $expected['link']['href'] = 'preg:/\/testing\/test_plugin\/css\/test_plugin_asset\.css\?[0-9]+/';
         $this->assertHtml($expected, $result);
 
-        $this->Html->Url->request = $this->Html->request->withAttribute('webroot', '/testing/longer/');
+        $this->View->setRequest($this->View->getRequest()->withAttribute('webroot', '/testing/longer/'));
         $result = $this->Html->css('TestPlugin.test_plugin_asset', ['once' => false]);
         $expected['link']['href'] = 'preg:/\/testing\/longer\/test_plugin\/css\/test_plugin_asset\.css\?[0-9]+/';
         $this->assertHtml($expected, $result);
@@ -1127,7 +1129,7 @@ class HtmlHelperTest extends TestCase
         $testfile = WWW_ROOT . '/test_theme/js/__test_js.js';
         $File = new File($testfile, true);
 
-        $this->Html->Url->request = $this->Html->request->withAttribute('webroot', '/');
+        $this->View->setRequest($this->View->getRequest()->withAttribute('webroot', '/'));
         $this->Html->Url->getView()->setTheme('TestTheme');
         $result = $this->Html->script('__test_js.js');
         $expected = [
@@ -1800,7 +1802,7 @@ class HtmlHelperTest extends TestCase
         ];
         $this->assertHtml($expected, $result);
 
-        $this->Html->request = $this->Html->Url->request = $this->Html->request->withAttribute('webroot', '/testing/');
+        $this->View->setRequest($this->View->getRequest()->withAttribute('webroot', '/testing/'));
         $result = $this->Html->meta('icon');
         $expected = [
             'link' => ['href' => '/testing/favicon.ico', 'type' => 'image/x-icon', 'rel' => 'icon'],
@@ -1832,7 +1834,7 @@ class HtmlHelperTest extends TestCase
         ];
         $this->assertHtml($expected, $result);
 
-        $this->Html->request = $this->Html->Url->request = $this->Html->request->withAttribute('webroot', '/testing/');
+        $this->View->setRequest($this->View->getRequest()->withAttribute('webroot', '/testing/'));
         $result = $this->Html->meta('icon');
         $expected = [
             'link' => ['href' => '/testing/test_theme/favicon.ico', 'type' => 'image/x-icon', 'rel' => 'icon'],

File diff suppressed because it is too large
+ 193 - 160
tests/TestCase/View/Helper/PaginatorHelperTest.php


+ 9 - 8
tests/TestCase/View/Helper/UrlHelperTest.php

@@ -43,9 +43,8 @@ class UrlHelperTest extends TestCase
         parent::setUp();
 
         Router::reload();
-        $this->View = new View();
+        $this->View = new View(new ServerRequest());
         $this->Helper = new UrlHelper($this->View);
-        $this->Helper->request = new ServerRequest();
 
         static::setAppNamespace();
         Plugin::load(['TestTheme']);
@@ -185,7 +184,7 @@ class UrlHelperTest extends TestCase
         $result = $this->Helper->assetTimestamp(Configure::read('App.cssBaseUrl') . 'cake.generic.css?someparam');
         $this->assertEquals(Configure::read('App.cssBaseUrl') . 'cake.generic.css?someparam', $result);
 
-        $this->Helper->request = $this->Helper->request->withAttribute('webroot', '/some/dir/');
+        $this->View->setRequest($this->View->getRequest()->withAttribute('webroot', '/some/dir/'));
         $result = $this->Helper->assetTimestamp('/some/dir/' . Configure::read('App.cssBaseUrl') . 'cake.generic.css');
         $this->assertRegExp('/' . preg_quote(Configure::read('App.cssBaseUrl') . 'cake.generic.css?', '/') . '[0-9]+/', $result);
     }
@@ -239,11 +238,11 @@ class UrlHelperTest extends TestCase
      */
     public function testAssetUrlDataUri()
     {
-        $request = $this->Helper->request
+        $request = $this->View->getRequest()
             ->withAttribute('base', 'subdir')
             ->withAttribute('webroot', 'subdir/');
 
-        $this->Helper->request = $request;
+        $this->View->setRequest($request);
         Router::pushRequest($request);
 
         $data = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4' .
@@ -263,10 +262,10 @@ class UrlHelperTest extends TestCase
      */
     public function testAssetUrlNoRewrite()
     {
-        $this->Helper->request = $this->Helper->request
+        $this->View->setRequest($this->View->getRequest()
             ->withAttribute('base', '/cake_dev/index.php')
             ->withAttribute('webroot', '/cake_dev/app/webroot/')
-            ->withRequestTarget('/cake_dev/index.php/tasks');
+            ->withRequestTarget('/cake_dev/index.php/tasks'));
         $result = $this->Helper->assetUrl('img/cake.icon.png', ['fullBase' => true]);
         $expected = Configure::read('App.fullBaseUrl') . '/cake_dev/app/webroot/img/cake.icon.png';
         $this->assertEquals($expected, $result);
@@ -493,7 +492,9 @@ class UrlHelperTest extends TestCase
      */
     public function testWebrootPaths()
     {
-        $this->Helper->request = $this->Helper->request->withAttribute('webroot', '/');
+        $this->View->setRequest(
+            $this->View->getRequest()->withAttribute('webroot', '/')
+        );
         $result = $this->Helper->webroot('/img/cake.power.gif');
         $expected = '/img/cake.power.gif';
         $this->assertEquals($expected, $result);