|
4 | 4 | namespace Crud\Controller\Component;
|
5 | 5 |
|
6 | 6 | use Cake\Controller\Component;
|
7 |
| -use Cake\Controller\ComponentRegistry; |
8 | 7 | use Cake\Controller\Controller;
|
9 | 8 | use Cake\Core\App;
|
10 | 9 | use Cake\Datasource\EntityInterface;
|
@@ -138,22 +137,48 @@ class CrudComponent extends Component
|
138 | 137 | ];
|
139 | 138 |
|
140 | 139 | /**
|
141 |
| - * Constructor |
| 140 | + * Initialize the component |
142 | 141 | *
|
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 |
146 | 144 | */
|
147 |
| - public function __construct(ComponentRegistry $collection, array $config = []) |
| 145 | + public function initialize(array $config): void |
148 | 146 | {
|
149 |
| - $config += ['actions' => [], 'listeners' => []]; |
150 |
| - $config['actions'] = $this->normalizeArray($config['actions']); |
151 |
| - $config['listeners'] = $this->normalizeArray($config['listeners']); |
| 147 | + parent::initialize($config); |
152 | 148 |
|
153 |
| - $this->_controller = $collection->getController(); |
154 |
| - $this->_eventManager = $this->_controller->getEventManager(); |
| 149 | + $this->_controller ??= $this->getController(); |
| 150 | + $this->_eventManager ??= $this->_controller->getEventManager(); |
| 151 | + $this->_action ??= $this->_controller->getRequest()->getParam('action'); |
| 152 | + } |
155 | 153 |
|
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); |
157 | 182 | }
|
158 | 183 |
|
159 | 184 | /**
|
@@ -182,19 +207,6 @@ public function normalizeArray(array $array): array
|
182 | 207 | return $normal;
|
183 | 208 | }
|
184 | 209 |
|
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 |
| - |
198 | 210 | /**
|
199 | 211 | * Loads listeners
|
200 | 212 | *
|
|
0 commit comments