Skip to content

Commit

Permalink
Merge pull request #751 from FriendsOfCake/task/crudcomponent-constru…
Browse files Browse the repository at this point in the history
…ctor

Remove constructor overriding.
  • Loading branch information
ADmad authored Oct 21, 2024
2 parents f52d017 + ce1f1bc commit 0803fce
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 33 deletions.
26 changes: 18 additions & 8 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="4.x-dev@">
<files psalm-version="5.26.1@d747f6500b38ac4f7dfc5edbcae6e4b637d7add0">
<file src="src/Controller/Component/CrudComponent.php">
<RedundantPropertyInitializationCheck>
<code><![CDATA[$this->_action]]></code>
<code><![CDATA[$this->_controller]]></code>
<code><![CDATA[$this->_controller->getEventManager()]]></code>
<code><![CDATA[$this->_controller->getRequest()->getParam('action')]]></code>
<code><![CDATA[$this->_eventManager]]></code>
<code><![CDATA[$this->getController()]]></code>
</RedundantPropertyInitializationCheck>
</file>
<file src="src/Listener/ApiQueryLogListener.php">
<InternalMethod occurrences="1">
<code>new QueryLogger()</code>
<InternalMethod>
<code><![CDATA[new QueryLogger()]]></code>
</InternalMethod>
</file>
<file src="src/Log/QueryLogger.php">
<InternalClass occurrences="2">
<code>CakeQueryLogger</code>
<code>parent::log($level, $message, $context)</code>
<InternalClass>
<code><![CDATA[CakeQueryLogger]]></code>
<code><![CDATA[parent::log($level, $message, $context)]]></code>
</InternalClass>
<InternalMethod occurrences="1">
<code>parent::log($level, $message, $context)</code>
<InternalMethod>
<code><![CDATA[parent::log($level, $message, $context)]]></code>
</InternalMethod>
</file>
</files>
62 changes: 37 additions & 25 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->_eventManager = $this->_controller->getEventManager();
$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 0803fce

Please sign in to comment.