浏览代码

Remove setting of dynamic properties.

ADmad 3 年之前
父节点
当前提交
8adaa6c424

+ 18 - 5
src/Controller/Component.php

@@ -16,7 +16,6 @@ declare(strict_types=1);
  */
 namespace Cake\Controller;
 
-use AllowDynamicProperties;
 use Cake\Core\InstanceConfigTrait;
 use Cake\Event\EventListenerInterface;
 use Cake\Log\LogTrait;
@@ -59,7 +58,6 @@ use Cake\Log\LogTrait;
  * @link https://book.cakephp.org/4/en/controllers/components.html
  * @see \Cake\Controller\Controller::$components
  */
-#[AllowDynamicProperties]
 class Component implements EventListenerInterface
 {
     use InstanceConfigTrait;
@@ -96,6 +94,13 @@ class Component implements EventListenerInterface
     protected array $_componentMap = [];
 
     /**
+     * Loaded component instances.
+     *
+     * @var array<string, \Cake\Controller\Component>
+     */
+    protected array $componentInstances = [];
+
+    /**
      * Constructor
      *
      * @param \Cake\Controller\ComponentRegistry $registry A component registry
@@ -145,12 +150,20 @@ class Component implements EventListenerInterface
      */
     public function __get(string $name): ?Component
     {
-        if (isset($this->_componentMap[$name]) && !isset($this->{$name})) {
+        if (isset($this->componentInstances[$name])) {
+            return $this->componentInstances[$name];
+        }
+
+        if (isset($this->_componentMap[$name])) {
             $config = (array)$this->_componentMap[$name]['config'] + ['enabled' => false];
-            $this->{$name} = $this->_registry->load($this->_componentMap[$name]['class'], $config);
+
+            return $this->componentInstances[$name] = $this->_registry->load(
+                $this->_componentMap[$name]['class'],
+                $config
+            );
         }
 
-        return $this->{$name} ?? null;
+        return null;
     }
 
     /**

+ 13 - 5
src/View/Helper.php

@@ -16,7 +16,6 @@ declare(strict_types=1);
  */
 namespace Cake\View;
 
-use AllowDynamicProperties;
 use Cake\Core\InstanceConfigTrait;
 use Cake\Event\EventListenerInterface;
 
@@ -41,7 +40,6 @@ use Cake\Event\EventListenerInterface;
  * - `afterRenderFile(EventInterface $event, $viewFile, $content)` - Called after any view fragment is rendered.
  *   If a listener returns a non-null value, the output of the rendered file will be set to that.
  */
-#[AllowDynamicProperties]
 class Helper implements EventListenerInterface
 {
     use InstanceConfigTrait;
@@ -68,6 +66,13 @@ class Helper implements EventListenerInterface
     protected array $_helperMap = [];
 
     /**
+     * Loaded helper instances.
+     *
+     * @var array<string, \Cake\View\Helper>
+     */
+    protected array $helperInstances = [];
+
+    /**
      * The View instance this helper is attached to
      *
      * @var \Cake\View\View
@@ -100,11 +105,14 @@ class Helper implements EventListenerInterface
      */
     public function __get(string $name): ?Helper
     {
-        if (isset($this->_helperMap[$name]) && !isset($this->{$name})) {
+        if (isset($this->helperInstances[$name])) {
+            return $this->helperInstances[$name];
+        }
+
+        if (isset($this->_helperMap[$name])) {
             $config = ['enabled' => false] + (array)$this->_helperMap[$name]['config'];
-            $this->{$name} = $this->_View->loadHelper($this->_helperMap[$name]['class'], $config);
 
-            return $this->{$name};
+            return $this->helperInstances[$name] = $this->_View->loadHelper($this->_helperMap[$name]['class'], $config);
         }
 
         return null;

+ 0 - 3
tests/TestCase/View/Helper/FormHelperTest.php

@@ -2136,7 +2136,6 @@ class FormHelperTest extends TestCase
         $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
         $this->assertEquals($expected, $result);
 
-        $this->Form->fields = [];
         $this->Form->select('Model.select', $options, ['multiple' => true]);
         $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
         $this->assertEquals($expected, $result);
@@ -5922,7 +5921,6 @@ class FormHelperTest extends TestCase
         $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
         $this->assertEquals($expected, $result);
 
-        $this->Form->fields = [];
         $this->Form->date('published');
         $expected = ['date', 'published'];
         $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
@@ -5946,7 +5944,6 @@ class FormHelperTest extends TestCase
         $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
         $this->assertEquals($expected, $result);
 
-        $this->Form->fields = [];
         $this->Form->date('published', ['secure' => false]);
         $expected = [];
         $result = $this->Form->getFormProtector()->__debugInfo()['fields'];

+ 0 - 7
tests/TestCase/View/Helper/UrlHelperTest.php

@@ -212,7 +212,6 @@ class UrlHelperTest extends TestCase
      */
     public function testAssetUrl(): void
     {
-        $this->Helper->webroot = '';
         $result = $this->Helper->assetUrl('js/post.js', ['fullBase' => true]);
         $this->assertSame(Router::fullBaseUrl() . '/js/post.js', $result);
 
@@ -281,7 +280,6 @@ class UrlHelperTest extends TestCase
      */
     public function testAssetUrlPlugin(): void
     {
-        $this->Helper->webroot = '';
         $this->loadPlugins(['TestPlugin']);
 
         $result = $this->Helper->assetUrl('TestPlugin.style', ['ext' => '.css']);
@@ -310,7 +308,6 @@ class UrlHelperTest extends TestCase
      */
     public function testAssetUrlTimestampForce(): void
     {
-        $this->Helper->webroot = '';
         Configure::write('Asset.timestamp', 'force');
 
         $result = $this->Helper->assetUrl('cake.generic.css', ['pathPrefix' => Configure::read('App.cssBaseUrl')]);
@@ -322,7 +319,6 @@ class UrlHelperTest extends TestCase
      */
     public function testAssetTimestampConfigureOverride(): void
     {
-        $this->Helper->webroot = '';
         Configure::write('Asset.timestamp', 'force');
         $timestamp = false;
 
@@ -356,7 +352,6 @@ class UrlHelperTest extends TestCase
      */
     public function testScript(): void
     {
-        $this->Helper->webroot = '';
         $result = $this->Helper->script(
             'post.js',
             ['fullBase' => true]
@@ -369,7 +364,6 @@ class UrlHelperTest extends TestCase
      */
     public function testScriptTimestampForce(): void
     {
-        $this->Helper->webroot = '';
         Configure::write('Asset.timestamp', 'force');
 
         $result = $this->Helper->script('script.js');
@@ -547,7 +541,6 @@ class UrlHelperTest extends TestCase
     public function testAppAssetPresent(): void
     {
         $Url = new UrlHelper($this->View, ['assetUrlClassName' => Asset::class]);
-        $Url->webroot = '';
 
         $result = $Url->assetUrl('cake.generic.css', ['pathPrefix' => '/']);
         $this->assertSame('/cake.generic.css?appHash', $result);

+ 1 - 2
tests/TestCase/View/HelperTest.php

@@ -110,8 +110,7 @@ class HelperTest extends TestCase
         $resultA = $Helper->Html;
         $resultB = $Helper->Html;
 
-        $resultA->testprop = 1;
-        $this->assertSame($resultA->testprop, $resultB->testprop);
+        $this->assertSame($resultA, $resultB);
     }
 
     /**