Skip to content

Commit e3e8d9e

Browse files
committed
Remove constructor overriding.
Normalize listeners and actions config in `setConfig()`.
1 parent f52d017 commit e3e8d9e

File tree

1 file changed

+36
-24
lines changed

1 file changed

+36
-24
lines changed

src/Controller/Component/CrudComponent.php

+36-24
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
namespace Crud\Controller\Component;
55

66
use Cake\Controller\Component;
7-
use Cake\Controller\ComponentRegistry;
87
use Cake\Controller\Controller;
98
use Cake\Core\App;
109
use Cake\Datasource\EntityInterface;
@@ -138,22 +137,48 @@ class CrudComponent extends Component
138137
];
139138

140139
/**
141-
* Constructor
140+
* Initialize the component
142141
*
143-
* @param \Cake\Controller\ComponentRegistry<\Cake\Controller\Controller> $collection A ComponentCollection this component
144-
* can use to lazy load its components.
145-
* @param array $config Array of configuration settings.
142+
* @param array $config Configuration values for component.
143+
* @return void
146144
*/
147-
public function __construct(ComponentRegistry $collection, array $config = [])
145+
public function initialize(array $config): void
148146
{
149-
$config += ['actions' => [], 'listeners' => []];
150-
$config['actions'] = $this->normalizeArray($config['actions']);
151-
$config['listeners'] = $this->normalizeArray($config['listeners']);
147+
parent::initialize($config);
152148

153-
$this->_controller = $collection->getController();
149+
$this->_controller = $this->getController();
154150
$this->_eventManager = $this->_controller->getEventManager();
151+
$this->_action = $this->_controller->getRequest()->getParam('action');
152+
}
155153

156-
parent::__construct($collection, $config);
154+
/**
155+
* Set component config.
156+
*
157+
* It normalizes the 'actions' and 'listeners' arrays.
158+
*
159+
* @param array<string, mixed>|string $key The key to set, or a complete array of configs.
160+
* @param mixed|null $value The value to set.
161+
* @param bool $merge Whether to recursively merge or overwrite existing config, defaults to true.
162+
* @return $this
163+
*/
164+
public function setConfig(array|string $key, mixed $value = null, bool $merge = true)
165+
{
166+
if (is_array($key)) {
167+
if (isset($key['actions'])) {
168+
$key['actions'] = $this->normalizeArray($key['actions']);
169+
}
170+
if (isset($key['listeners'])) {
171+
$key['listeners'] = $this->normalizeArray($key['listeners']);
172+
}
173+
} elseif ($key === 'actions') {
174+
assert(is_array($value));
175+
$value = $this->normalizeArray($value);
176+
} elseif ($key === 'listeners') {
177+
assert(is_array($value));
178+
$value = $this->normalizeArray($value);
179+
}
180+
181+
return parent::setConfig($key, $value, $merge);
157182
}
158183

159184
/**
@@ -182,19 +207,6 @@ public function normalizeArray(array $array): array
182207
return $normal;
183208
}
184209

185-
/**
186-
* Add self to list of components capable of dispatching an action.
187-
*
188-
* @param array $config Configuration values for component.
189-
* @return void
190-
*/
191-
public function initialize(array $config): void
192-
{
193-
parent::initialize($config);
194-
195-
$this->_action = $this->getController()->getRequest()->getParam('action');
196-
}
197-
198210
/**
199211
* Loads listeners
200212
*

0 commit comments

Comments
 (0)