Skip to content

Commit

Permalink
Remove constructor overriding.
Browse files Browse the repository at this point in the history
Normalize listeners and actions config in `setConfig()`.
  • Loading branch information
ADmad committed Oct 21, 2024
1 parent f52d017 commit e3e8d9e
Showing 1 changed file with 36 additions and 24 deletions.
60 changes: 36 additions & 24 deletions src/Controller/Component/CrudComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
namespace Crud\Controller\Component;

use Cake\Controller\Component;
use Cake\Controller\ComponentRegistry;
use Cake\Controller\Controller;
use Cake\Core\App;
use Cake\Datasource\EntityInterface;
Expand Down Expand Up @@ -138,22 +137,48 @@ class CrudComponent extends Component
];

/**
* Constructor
* Initialize the component
*
* @param \Cake\Controller\ComponentRegistry<\Cake\Controller\Controller> $collection A ComponentCollection this component
* can use to lazy load its components.
* @param array $config Array of configuration settings.
* @param array $config Configuration values for component.
* @return void
*/
public function __construct(ComponentRegistry $collection, array $config = [])
public function initialize(array $config): void
{
$config += ['actions' => [], 'listeners' => []];
$config['actions'] = $this->normalizeArray($config['actions']);
$config['listeners'] = $this->normalizeArray($config['listeners']);
parent::initialize($config);

$this->_controller = $collection->getController();
$this->_controller = $this->getController();
$this->_eventManager = $this->_controller->getEventManager();
$this->_action = $this->_controller->getRequest()->getParam('action');
}

parent::__construct($collection, $config);
/**
* Set component config.
*
* It normalizes the 'actions' and 'listeners' arrays.
*
* @param array<string, mixed>|string $key The key to set, or a complete array of configs.
* @param mixed|null $value The value to set.
* @param bool $merge Whether to recursively merge or overwrite existing config, defaults to true.
* @return $this
*/
public function setConfig(array|string $key, mixed $value = null, bool $merge = true)
{
if (is_array($key)) {
if (isset($key['actions'])) {
$key['actions'] = $this->normalizeArray($key['actions']);
}
if (isset($key['listeners'])) {
$key['listeners'] = $this->normalizeArray($key['listeners']);
}
} elseif ($key === 'actions') {
assert(is_array($value));
$value = $this->normalizeArray($value);
} elseif ($key === 'listeners') {
assert(is_array($value));
$value = $this->normalizeArray($value);
}

return parent::setConfig($key, $value, $merge);
}

/**
Expand Down Expand Up @@ -182,19 +207,6 @@ public function normalizeArray(array $array): array
return $normal;
}

/**
* Add self to list of components capable of dispatching an action.
*
* @param array $config Configuration values for component.
* @return void
*/
public function initialize(array $config): void
{
parent::initialize($config);

$this->_action = $this->getController()->getRequest()->getParam('action');
}

/**
* Loads listeners
*
Expand Down

0 comments on commit e3e8d9e

Please sign in to comment.