Skip to content

Commit

Permalink
Merge pull request #8 from infoseci/for-upstream-contribution-latest-…
Browse files Browse the repository at this point in the history
…changes

Adding Cake 4 Support
  • Loading branch information
rochamarcelo authored Oct 10, 2021
2 parents e648f52 + d2bc402 commit 1cf9251
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 124 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
"pimple/pimple": "~3.0"
},
"require-dev": {
"cakephp/cakephp": "~3.0",
"phpunit/phpunit": "^5.7.14|^6.0"
"cakephp/cakephp": "~4.0",
"phpunit/phpunit": "^8.0"
},
"autoload": {
"psr-4": {
Expand Down
34 changes: 0 additions & 34 deletions config/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,3 @@
use Cake\Core\Configure;
use RochaMarcelo\CakePimpleDi\Di\Di;

$config = Configure::consume('CakePimpleDi');
$scopes = [];
if ( !isset($config['scopes']) ) {
$scopes = [
'default' => (array)$config
];
}

foreach ( $scopes as $name => $config ) {
$Di = Di::instance($name);

$config = $config + [
'providers' => []
];

foreach ($config['providers'] as $provider ) {
if ( is_string($provider) ) {
$provider = new $provider;
}

$Di->register($provider);
}

if ( isset($config['services']) && is_array($config['services']) ) {
$Di->setMany($config['services']);
}
}
if (!empty($config['actionInjections'])) {
\Cake\Event\EventManager::instance()->on(new \RochaMarcelo\CakePimpleDi\Event\ActionInjectionListener($config['actionInjections']));
}

if (isset($config['useRequest']) && $config['useRequest']) {
\Cake\Event\EventManager::instance()->on(new \RochaMarcelo\CakePimpleDi\Event\ContainerDispatchListener());
}
4 changes: 2 additions & 2 deletions src/Controller/Component/DiComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class DiComponent extends Component
*
* @return null
*/
public function startup(Event $event)
public function startup(\Cake\Event\EventInterface $event)
{
$controller = $this->_registry->getController();
$request = $controller->getRequest();
Expand All @@ -48,7 +48,7 @@ public function startup(Event $event)
*
* @return void
*/
public function beforeRender(Event $event)
public function beforeRender(\Cake\Event\EventInterface $event)
{
$controller = $this->_registry->getController();
$injections = $this->getConfig('injections');
Expand Down
4 changes: 2 additions & 2 deletions src/Event/ActionInjectionListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ public function __construct(array $injectionsMap)
*
* @return array
*/
public function implementedEvents()
public function implementedEvents(): array
{
return [
'Controller.beforeCallAction' => 'injectDependency',
'Controller.startup' => 'injectDependency',
];
}

Expand Down
38 changes: 0 additions & 38 deletions src/Event/ContainerDispatchListener.php

This file was deleted.

36 changes: 36 additions & 0 deletions src/Middleware/ContainerizeRequestMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
namespace RochaMarcelo\CakePimpleDi\Middleware;

use Cake\Http\Response;
use Cake\Http\ServerRequest;
use Psr\Http\Message\ResponseInterface;
use RochaMarcelo\CakePimpleDi\Di\DiTrait;

/**
* Class ContainerizeRequestMiddleware
* @package RochaMarcelo\Middleware
*
* When loaded this middleware provides a key 'request' and
* a key for 'session' in the container.
*/
class ContainerizeRequestMiddleware
{
use DiTrait;

/**
* @param ServerRequest $request ServerRequest
* @param Response $response Response
* @param callable $next callable
* @return ResponseInterface
*/
public function __invoke(ServerRequest $request, Response $response, callable $next)
{
$di = $this->di();
$di->set('request', $request);
$di->set('session', $request->getSession());

$response = $next($request, $response);

return $response;
}
}
59 changes: 59 additions & 0 deletions src/Plugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace RochaMarcelo\CakePimpleDi;

use Cake\Core\BasePlugin;
use Cake\Core\Configure;
use Cake\Core\PluginApplicationInterface;
use RochaMarcelo\CakePimpleDi\Di\Di;

/**
* Class Plugin
* @package RochaMarcelo\CakePimpleDi
*/
class Plugin extends BasePlugin
{
public function getTemplatePath(): string
{
return '';
}

/**
* @param PluginApplicationInterface $app
*/
public function bootstrap(PluginApplicationInterface $app): void
{
parent::bootstrap($app); // TODO: Change the autogenerated stub

$config = Configure::consume('CakePimpleDi');
$scopes = [];
if ( !isset($config['scopes']) ) {
$scopes = [
'default' => (array)$config
];
}

foreach ( $scopes as $name => $config ) {
$Di = Di::instance($name);

$config = $config + [
'providers' => []
];

foreach ($config['providers'] as $provider ) {
if ( is_string($provider) ) {
$provider = new $provider;
}

$Di->register($provider);
}

if ( isset($config['services']) && is_array($config['services']) ) {
$Di->setMany($config['services']);
}
}
if (!empty($config['actionInjections'])) {
\Cake\Event\EventManager::instance()->on(new \RochaMarcelo\CakePimpleDi\Event\ActionInjectionListener($config['actionInjections']));
}
}
}
6 changes: 3 additions & 3 deletions tests/TestCase/Di/DiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class DiTest extends TestCase
*
* @return void
*/
public function setUp()
public function setUp(): void
{
parent::setUp();
$this->Test = new Di();
Expand All @@ -27,7 +27,7 @@ public function setUp()
*
* @return void
*/
public function tearDown()
public function tearDown(): void
{
unset($this->Test);

Expand Down Expand Up @@ -231,4 +231,4 @@ function () {

$Di->setMany($services);
}
}
}
43 changes: 0 additions & 43 deletions tests/TestCase/Event/ContainerDispatchListenerTest.php

This file was deleted.

0 comments on commit 1cf9251

Please sign in to comment.