Skip to content

Commit

Permalink
Merge branch '4.x-dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
zunnu committed Oct 21, 2022
2 parents 754ee6a + f1d6c41 commit 5f5e2e8
Show file tree
Hide file tree
Showing 15 changed files with 170 additions and 69 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.

14 changes: 0 additions & 14 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
23 changes: 14 additions & 9 deletions src/Controller/AssociationsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,18 @@ class AssociationsController extends AppController
*
* @return void
*/
public function initialize() {
public function initialize(): void
{
parent::initialize();
$this->Gate = new Gate();
}

/**
* Index method
*/
public function index() {
$this->viewBuilder()->setLayout(false);
public function index()
{
$this->viewBuilder()->disableAutoLayout();
$conditions = [];
$showDeepChildren = true;
$search = [];
Expand Down Expand Up @@ -81,16 +83,17 @@ public function index() {
$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()->setLayout(false);
public function details()
{
$this->viewBuilder()->disableAutoLayout();
$data = [];
$associationsCollection = [];
$conditions = [];
Expand Down Expand Up @@ -121,7 +124,7 @@ public function details() {
$this->set('showDeepChildren', $showDeepChildren);

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

Expand All @@ -131,7 +134,8 @@ public function details() {
* @param array $data Data from the ui, including target model, target plugin
* @return array Filtered data
*/
private function _parseSearch($associations, $data) {
private function _parseSearch($associations, $data): array
{
$associationsCollection = [];

if(!empty($data['targetPlugin']) && !empty($data['targetModel'])) {
Expand Down Expand Up @@ -174,7 +178,8 @@ private function _parseSearch($associations, $data) {
* @param array $data Ui data to be formated
* @return array Formated version of conditions
*/
private function _parseConditions($data) {
private function _parseConditions($data): array
{
$conditions = [];

if(!empty($data['plugins']) && $data['plugins'] !== 'undefined') {
Expand Down
42 changes: 21 additions & 21 deletions src/Gate.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,30 +162,30 @@ private function _associations($model, $plugin = null) {

if($associations = $setModel->associations()) {
foreach ($associations->normalizeKeys($associations->getIterator()) as $key => $association) {
$source = $association->source();
$source = $association->getSource();
$sourceRegistery = 'App';
$targetRegistery = 'App';

// error handling for association registeration
try {
$target = $association->target();
$target = $association->getTarget();
} catch (\Exception $e) {
$association->target()->setRegistryAlias(ucfirst($association->getProperty()) . $association->getName());
$association->target()->setAlias(ucfirst($association->getProperty()) . $association->getName());
$association->getTarget()->setRegistryAlias(ucfirst($association->getProperty()) . $association->getName());
$association->getTarget()->setAlias(ucfirst($association->getProperty()) . $association->getName());
$association->setName(ucfirst($association->getProperty()) . $association->getName());
$target = $association->target();
$target = $association->getTarget();
}

$sourceRegistery = 'App';
$targetRegistery = 'App';

if(strpos($source->registryAlias(), '.') !== false) {
$sourceRegistery = strtok($source->registryAlias(), '.');
if(strpos($source->getRegistryAlias(), '.') !== false) {
$sourceRegistery = strtok($source->getRegistryAlias(), '.');

if($sourceRegistery !== 'App' && !in_array(strtolower($sourceRegistery), $activePluginsLower)) {
$sourceRegistery = 'App';
}
}

if(strpos($source->registryAlias(), '.') !== false) {
$targetRegistery = strtok($target->registryAlias(), '.');
if(strpos($source->getRegistryAlias(), '.') !== false) {
$targetRegistery = strtok($target->getRegistryAlias(), '.');

if($targetRegistery !== 'App' && !in_array(strtolower($targetRegistery), $activePluginsLower)) {
$targetRegistery = 'App';
Expand All @@ -204,18 +204,18 @@ private function _associations($model, $plugin = null) {

$associationsArray[$type][] = [
'source' => [
'table' => $source->table(),
'alias' => $source->alias(),
'connectionName' => $source->connection()->configName(),
'table' => $source->getTable(),
'alias' => $source->getAlias(),
'connectionName' => $source->getConnection()->configName(),
'location' => $sourceRegistery,
'model' => $model,
],
'target' => [
'table' => $target->table(),
'alias' => $target->alias(),
'connectionName' => $target->connection()->configName(),
'table' => $target->getTable(),
'alias' => $target->getAlias(),
'connectionName' => $target->getConnection()->configName(),
'location' => $targetRegistery,
'model' => $this->convertTableName($target->registryAlias()),
'model' => $this->convertTableName($target->getRegistryAlias()),
// 'model' => $this->convertTableName($target->entityClass()),
]
];
Expand All @@ -235,9 +235,9 @@ private function _associations($model, $plugin = null) {
*/
private function getPath($plugin) {
if (!$plugin || $plugin == 'App') {
$path = App::path('Model/Table');
$path = App::classPath('Model/Table');
} else {
$path = App::path('Model/Table', $plugin);
$path = App::classPath('Model/Table', $plugin);
}

return $path;
Expand Down
2 changes: 2 additions & 0 deletions src/Panel/AssociationsPanel.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);

namespace AssociationsDebugger\Panel;
use DebugKit\DebugPanel;
use AssociationsDebugger\Gate;
Expand Down
12 changes: 0 additions & 12 deletions src/Plugin.php

This file was deleted.

File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
</script>

<?php
if(!isset($showDeepChildren)) $showDeepChildren = true;
$this->StructureBuilder = $this->loadHelper('AssociationsDebugger.StructureBuilder', ['showDeepChildren' => $showDeepChildren]);
$structure = $this->StructureBuilder->build($associationCollections);
?>
Expand Down Expand Up @@ -56,7 +57,7 @@ function getNodeInfo(id) {
render(svgGroup, g);

function nodeDetailsRequest(plugin, currentModel, targetModel, targetPlugin) {
var csrfToken = <?= json_encode($this->request->getParam('_csrfToken')) ?>;
var csrfToken = <?= json_encode($this->request->getAttribute('csrfToken')) ?>;

return $.ajax({
type: "POST",
Expand Down Expand Up @@ -98,6 +99,9 @@ function nodeDetailsRequest(plugin, currentModel, targetModel, targetPlugin) {
model = nodeInfo.model;
}

$('#general-search').removeAttr("checked");
$('#general-search').multiselect('deselectAll');

if(!plugin) {
$('#general-search').multiselect('select', 'Root', true);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
</style>

<!-- draw area -->
<svg id="canvas" width="100%" height="100%"></svg>
<svg id="canvas" width="100%" height="1000px"></svg>

<?= $this->element('AssociationsDebugger.associationTree', [
'associationCollections' => $associationCollections,
Expand Down
19 changes: 18 additions & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);

/**
* Test suite bootstrap for AssociationsDebugger.
*
Expand All @@ -15,7 +17,7 @@
}
} while ($root !== $lastRoot);

throw new Exception("Cannot find the root of the application, unable to run tests");
throw new Exception('Cannot find the root of the application, unable to run tests');
};
$root = $findRoot(__FILE__);
unset($findRoot);
Expand All @@ -36,3 +38,18 @@

return;
}

/**
* Load schema from a SQL dump file.
*
* If your plugin does not use database fixtures you can
* safely delete this.
*
* If you want to support multiple databases, consider
* using migrations to provide schema for your plugin,
* and using \Migrations\TestSuite\Migrator to load schema.
*/
use Cake\TestSuite\Fixture\SchemaLoader;

// Load a schema dump file.
(new SchemaLoader())->loadSqlFiles('tests/schema.sql', 'test');
1 change: 1 addition & 0 deletions tests/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-- Test database schema for AssociationsDebugger

0 comments on commit 5f5e2e8

Please sign in to comment.