Skip to content

Commit

Permalink
Initial Cake 4 commit
Browse files Browse the repository at this point in the history
  • Loading branch information
zunnu committed Oct 21, 2022
1 parent 5f0b683 commit 7160eba
Show file tree
Hide file tree
Showing 18 changed files with 1,038 additions and 368 deletions.
8 changes: 6 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
/composer.lock
/composer.phar
/phpunit.xml
/vendor
config/Migrations/schema-dump-default.lock
/.phpunit.result.cache
/phpunit.phar
/config/Migrations/schema-dump-default.lock
/vendor/
/.idea/
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
"type": "cakephp-plugin",
"license": "MIT",
"require": {
"cakephp/cakephp": "^3.5"
"php": ">=7.2",
"cakephp/cakephp": "^4.0"
},
"require-dev": {
"phpunit/phpunit": "^5.7.14|^6.0"
"phpunit/phpunit": "^8.5 || ^9.3"
},
"autoload": {
"psr-4": {
Expand Down
6 changes: 0 additions & 6 deletions config/bootstrap.php

This file was deleted.

18 changes: 0 additions & 18 deletions config/routes.php

This file was deleted.

98 changes: 98 additions & 0 deletions src/AssociationsDebuggerPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php
declare(strict_types=1);

namespace AssociationsDebugger;

use Cake\Console\CommandCollection;
use Cake\Core\BasePlugin;
use Cake\Core\ContainerInterface;
use Cake\Core\PluginApplicationInterface;
use Cake\Http\MiddlewareQueue;
use Cake\Routing\RouteBuilder;
use Cake\Core\Configure;

/**
* Plugin for AssociationsDebugger
*/
class AssociationsDebuggerPlugin extends BasePlugin
{
/**
* Load all the plugin configuration and bootstrap logic.
*
* The host application is provided as an argument. This allows you to load
* additional plugin dependencies, or attach events.
*
* @param \Cake\Core\PluginApplicationInterface $app The host application
* @return void
*/
public function bootstrap(PluginApplicationInterface $app): void
{
if (Configure::read('debug')) {
// dd(Configure::read('DebugKit.panels'));
Configure::write('DebugKit.panels', ['AssociationsDebugger.Associations']);
}
}

/**
* Add routes for the plugin.
*
* If your plugin has many routes and you would like to isolate them into a separate file,
* you can create `$plugin/config/routes.php` and delete this method.
*
* @param \Cake\Routing\RouteBuilder $routes The route builder to update.
* @return void
*/
public function routes(RouteBuilder $routes): void
{
$routes->plugin(
'AssociationsDebugger',
['path' => '/associations-debugger'],
function (RouteBuilder $builder) {
$builder->connect('/', ['controller' => 'Associations', 'action' => 'index']);
$builder->connect('/{action}/*', ['controller' => 'Associations']);
$builder->fallbacks();
}
);
parent::routes($routes);
}

/**
* Add middleware for the plugin.
*
* @param \Cake\Http\MiddlewareQueue $middlewareQueue The middleware queue to update.
* @return \Cake\Http\MiddlewareQueue
*/
public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue
{
// Add your middlewares here

return $middlewareQueue;
}

/**
* Add commands for the plugin.
*
* @param \Cake\Console\CommandCollection $commands The command collection to update.
* @return \Cake\Console\CommandCollection
*/
public function console(CommandCollection $commands): CommandCollection
{
// Add your commands here

$commands = parent::console($commands);

return $commands;
}

/**
* Register application container services.
*
* @param \Cake\Core\ContainerInterface $container The Container to update.
* @return void
* @link https://book.cakephp.org/4/en/development/dependency-injection.html#dependency-injection
*/
public function services(ContainerInterface $container): void
{
// Add your services here
}
}
1 change: 1 addition & 0 deletions src/Controller/AppController.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);

namespace AssociationsDebugger\Controller;

Expand Down
178 changes: 162 additions & 16 deletions src/Controller/AssociationsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,38 +14,184 @@
*/
class AssociationsController extends AppController
{
public function initialize() {
/**
* initialize method
*
* @return void
*/
public function initialize(): void
{
parent::initialize();
$this->Gate = new Gate();
}

public function index() {
$this->viewBuilder()->setLayout(false);
/**
* Index method
*/
public function index()
{
$this->viewBuilder()->disableAutoLayout();
$conditions = [];
$showDeepChildren = true;
$search = [];
$selectedTypes = [];

// SEARCH
// search
if ($this->request->is('get')) {
if(!empty($this->request->getQueryParams())) {
$data = $this->request->getQueryParams();
$search = !empty($data['search']) ? json_decode($data['search'], true) : [];
if(!empty($data['deepChildren'])) $showDeepChildren = filter_var($data['deepChildren'], FILTER_VALIDATE_BOOLEAN);
$conditions = $this->_parseConditions($data);

if(!empty($data['plugins']) && $data['plugins'] !== 'undefined') {
$plugins = explode(',', $data['plugins']);
$conditions['plugins'] = $plugins;
}

if(!empty($data['associationTypes']) && $data['associationTypes'] !== 'undefined') {
$associationTypes = explode(',', $data['associationTypes']);
$conditions['associationTypes'] = $associationTypes;
}
if(!empty($data['associationTypes'])) $selectedTypes = explode(',', $data['associationTypes']);
if(!empty($search)) $data = $data + $search;
}
}

$this->set('associationCollections', $this->Gate->associations($conditions)->array());
$this->set('associationTypes', $this->Gate->getAssociationTypes());
$associations = $this->Gate->associations($conditions)->array();
$associationsCollection = $this->_parseSearch($associations, (!empty($data) ? $data : []));

$this->set('associationCollections', $associationsCollection);
$this->set('associationTypes', $this->Gate->associationTypes);
$this->set('activePlugins', $this->Gate->getPlugins());

$selectedNode = 'Root';
if(!empty($data['targetPlugin'])) {
if(!empty($data['targetModel'])) {
$selectedNode = $data['targetPlugin'] . '-' . $data['targetModel'];
} else {
$selectedNode = $data['targetPlugin'];
}
}

$assocationSearchSelect = ['Root' => 'Root'];
foreach($associations as $plugin => $assocation) {
$keys = array_keys($assocation);
$first = true;

foreach($keys as $k) {
if($first) {
$first = false;
$assocationSearchSelect[$plugin][$plugin] = $plugin . ' (plugin)';
}

$assocationSearchSelect[$plugin][$plugin . '-' . $k] = $k;
}
}

$this->set(compact('assocationSearchSelect', 'showDeepChildren', 'selectedTypes', 'selectedNode'));

if($this->request->is('ajax')) {
$this->render('Element/associationTree');
$this->render('element/associationTree');
}
}

/**
* Details methdod
* Will return more details about the selected association tree
*/
public function details()
{
$this->viewBuilder()->disableAutoLayout();
$data = [];
$associationsCollection = [];
$conditions = [];
$showDeepChildren = true;

if($this->request->is('ajax')) {
$data = $this->request->getData();
$query = $this->request->getQueryParams();
$data = $data + $query;

if(!empty($this->request->getQueryParams())) {
$conditions = $this->_parseConditions($this->request->getQueryParams());
}
} elseif($this->request->is('get') && !empty($this->request->getQueryParams())) {
$data = $this->request->getQueryParams();
}

if(!empty($data)) {
$associations = $this->Gate->associations($conditions)->array();
$associationsCollection = $this->_parseSearch($associations, (!empty($data) ? $data : []));
}

if(!empty($data['deepChildren'])) $showDeepChildren = filter_var($data['deepChildren'], FILTER_VALIDATE_BOOLEAN);

$this->set('associationCollections', $associationsCollection);
$this->set('associationTypes', $this->Gate->associationTypes);
$this->set('activePlugins', $this->Gate->getPlugins());
$this->set('showDeepChildren', $showDeepChildren);

// if($this->request->is('ajax')) {
$this->render('element/associationTree');
// }
}

/**
* Filter associations with target model, target plugin etc
* @param array $associations List of associations
* @param array $data Data from the ui, including target model, target plugin
* @return array Filtered data
*/
private function _parseSearch($associations, $data): array
{
$associationsCollection = [];

if(!empty($data['targetPlugin']) && !empty($data['targetModel'])) {
if(!empty($associations[$data['targetPlugin']][$data['targetModel']])) {
$associationsCollection = [
$data['targetPlugin'] => [
$data['targetModel'] => $associations[$data['targetPlugin']][$data['targetModel']]
]
];
}
} elseif(!empty($data['targetPlugin']) && empty($data['targetModel'])) {
if(!empty($associations[$data['targetPlugin']])) {
$associationsCollection = [
$data['targetPlugin'] => $associations[$data['targetPlugin']]
];
}
} elseif(!empty($data['plugin']) && !empty($data['currentModel'])) {
if(!empty($associations[$data['plugin']][$data['currentModel']])) {
$associationsCollection = [
$data['plugin'] => [
$data['currentModel'] => $associations[$data['plugin']][$data['currentModel']]
]
];
}
} elseif(!empty($data['plugin']) && empty($data['currentModel'])) {
if(!empty($associations[$data['plugin']])) {
$associationsCollection = [
$data['plugin'] => $associations[$data['plugin']]
];
}
} else {
$associationsCollection = $associations;
}

return $associationsCollection;
}

/**
* Format conditions
* @param array $data Ui data to be formated
* @return array Formated version of conditions
*/
private function _parseConditions($data): array
{
$conditions = [];

if(!empty($data['plugins']) && $data['plugins'] !== 'undefined') {
$plugins = explode(',', $data['plugins']);
$conditions['plugins'] = $plugins;
}

if(!empty($data['associationTypes']) && $data['associationTypes'] !== 'undefined') {
$associationTypes = explode(',', $data['associationTypes']);
$conditions['associationTypes'] = $associationTypes;
}

return $conditions;
}
}
Loading

0 comments on commit 7160eba

Please sign in to comment.