Skip to content

Commit

Permalink
Added comments to the code, cleaned code
Browse files Browse the repository at this point in the history
  • Loading branch information
zunnu committed Oct 18, 2022
1 parent a293e60 commit a5fbda8
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 64 deletions.
4 changes: 0 additions & 4 deletions config/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
function (RouteBuilder $routes) {
$routes->connect('/', ['controller' => 'Associations', 'action' => 'index']);
$routes->connect('/:action/*', ['controller' => 'Associations']);
// $routes->scope('/', function ($routes) {
// $routes->connect('/:action/*', ['controller' => 'Associations']);
// });

$routes->fallbacks(DashedRoute::class);
}
);
37 changes: 30 additions & 7 deletions src/Controller/AssociationsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
*/
class AssociationsController extends AppController
{
/**
* initialize method
*
* @return void
*/
public function initialize() {
parent::initialize();
$this->Gate = new Gate();
Expand All @@ -25,11 +30,11 @@ public function initialize() {
public function index() {
$this->viewBuilder()->setLayout(false);
$conditions = [];
$showDeepChildren = false;
$showDeepChildren = true;
$search = [];
$selectedTypes = [];

// SEARCH
// search
if ($this->request->is('get')) {
if(!empty($this->request->getQueryParams())) {
$data = $this->request->getQueryParams();
Expand All @@ -43,11 +48,12 @@ public function index() {
}

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

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

Expand All @@ -65,10 +71,12 @@ public function details() {
$data = [];
$associationsCollection = [];
$conditions = [];
$showDeepChildren = false;
$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());
Expand All @@ -79,11 +87,13 @@ public function details() {

if(!empty($data)) {
$associations = $this->Gate->associations($conditions)->array();
$associationsCollection = $this->_parseSearch($associations, $data);
$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->getAssociationTypes());
$this->set('associationTypes', $this->Gate->associationTypes);
$this->set('activePlugins', $this->Gate->getPlugins());
$this->set('showDeepChildren', $showDeepChildren);

Expand All @@ -92,7 +102,15 @@ public function details() {
// }
}

/**
* 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) {
$associationsCollection = [];

if(!empty($data['targetPlugin']) && !empty($data['targetModel'])) {
if(!empty($associations[$data['targetPlugin']][$data['targetModel']])) {
$associationsCollection = [
Expand Down Expand Up @@ -126,6 +144,11 @@ private function _parseSearch($associations, $data) {
return $associationsCollection;
}

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

Expand Down
153 changes: 105 additions & 48 deletions src/Gate.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,47 @@
use Cake\Cache\Cache;

class Gate {
// prefixes
protected $prefixes = [];

// routes
protected $routes = [];

// plugins
/**
* List of loaded plugins
* @var array
*/
protected $plugins = [];

/**
* List of found
* @var array
*/
protected $models = [];

/**
* Config of the gate
* Options:
* associationTypes: List of associations types to filter by
* @var array
*/
protected $config = [];

/**
* List of associations
* @var array
*/
public $associations = [];

/**
* List of available types
* @var [type]
*/
public $associationTypes = [
'oneToMany' => 'hasMany',
'oneToOne' => 'hasOne',
'manyToOne' => 'belongsTo',
'manyToMany' => 'belongsToMany',
];

public function __construct() {
$this->setRoutes();
}

/**
* Set the plugins to scope
* This will also set "App" as a plugin
*/
public function setPlugins() {
$pluginConfig = $this->getConfig('plugins');
$plugins = Plugin::loaded();
Expand All @@ -60,11 +77,19 @@ public function setPlugins() {
$this->plugins = $plugins;
}

// get routes
public function setRoutes() {
$this->routes = Router::routes();
/**
* Return the list of found models in the whole app
* @return array
*/
public function getLoadedModels() {
return $this->models;
}

/**
* Main associations fetch method
* @param array $conditions List of filter conditions
* @return object Current class object
*/
public function associations($conditions = []) {
$this->setConfig($conditions);
$this->setPlugins();
Expand All @@ -73,29 +98,26 @@ public function associations($conditions = []) {
return $this;
}

// return data as json
/**
* Return the associations data as json by using chaining
* @return string
*/
public function json() {
return json_encode($this->associations);
}

// return data as array
/**
* Return the associations data as array by using chaining
* @return array
*/
public function array() {
return $this->associations;
}

// private function buildAssociations() {
// $associationsArray = [];
// dd($this->getAssociations());

// foreach ($this->getAssociations() as $pluginName => $associations) {
// foreach ($associations as $key => $association) {
// dd($associations->get('Users'));

// }
// }
// }

// get associations
/**
* Get the list of assocations
* @return array List of associations
*/
private function getAssociations() {
$associationsArray = [];
$modelList = [];
Expand All @@ -115,11 +137,17 @@ private function getAssociations() {
}
}

$this->models = $modelList;
$associationsArray = $this->_buildChildren($associationsArray, $modelList);
return $associationsArray;
}

// return list of associations
/**
* Worker for getAssociations
* @param string $model Model name
* @param string $plugin Set only if model is under plugin
* @return array Specific model associations
*/
private function _associations($model, $plugin = null) {
$associationTypes = $this->getConfig('associationTypes');

Expand Down Expand Up @@ -165,10 +193,6 @@ private function _associations($model, $plugin = null) {
$type .= ' (' . $this->associationTypes[$type] . ')';
}

// if($target->table() == 'cars') {
// dd([$source, $target]);
// }

$associationsArray[$type][] = [
'source' => [
'table' => $source->table(),
Expand All @@ -195,7 +219,11 @@ private function _associations($model, $plugin = null) {
return false;
}

// return the path to the model dir
/**
* Get the path to the model directory
* @param string $plugin Plugin name, if not a plugin the plugin name will be App
* @return string Path to model dir
*/
private function getPath($plugin) {
if (!$plugin || $plugin == 'App') {
$path = App::path('Model/Table');
Expand All @@ -206,6 +234,10 @@ private function getPath($plugin) {
return $path;
}

/**
* Get the list of the models of the whole app
* @return array
*/
private function getModels() {
// get app models
$holder['App'] = $this->_models();
Expand All @@ -218,6 +250,11 @@ private function getModels() {
return $holder;
}

/**
* Worker for getModels
* @param string $plugin Plugin name where to search the models from
* @return array List of tables
*/
private function _models($plugin = null) {
// find the models
$path = $this->getPath($plugin);
Expand All @@ -226,7 +263,11 @@ private function _models($plugin = null) {
return $models;
}

private function setConfig($conditions) {
/**
* Set config for the gate
* @param array $conditions
*/
public function setConfig($conditions) {
if(!empty($conditions['associationTypes'])) {
$this->config['associationTypes'] = $conditions['associationTypes'];
} else {
Expand All @@ -244,18 +285,23 @@ private function setConfig($conditions) {
}
}

private function getConfig($arrayName = null) {
/**
* Get the gate config
* @param string $arrayName Key of the config
* @return array
*/
public function getConfig($arrayName = null) {
if(empty($arrayName)) {
return $this->config;
} else {
return $this->config[$arrayName];
}
}

public function getAssociationTypes() {
return $this->associationTypes;
}

/**
* Get the list of loaded plugins
* @return array
*/
public function getPlugins() {
$plugins = Plugin::loaded();
$loadedPlugins = [];
Expand All @@ -267,12 +313,18 @@ public function getPlugins() {
return $loadedPlugins;
}

/**
* Worker for getAssociations
* adds the children to association list
* @param array $plugins List of associations inside "plugins"
* @param array $models List of models
* @return array Associations with children
*/
private function _buildChildren($plugins, $models) {
foreach ($plugins as $pluginName => $plugin) {
foreach ($plugin as $modelName => $model) {
foreach ($model as $associationType => $type) {
foreach($type as $key => $association) {
// dd($association);
if(!empty($association['target']) && !empty($models[$association['target']['model']])) {
$childTypes = $plugins[$models[$association['target']['model']]['plugin']][$association['target']['model']];

Expand All @@ -298,15 +350,20 @@ private function _buildChildren($plugins, $models) {
return $plugins;
}

private function convertTableName($entityName) {
if(strpos($entityName, '.') !== false) {
$entityName = substr($entityName, strrpos($entityName, '.') + 1);
/**
* Format the table name
* @param string $modelName Name of the model
* @return string Formated name
*/
private function convertTableName($modelName) {
if(strpos($modelName, '.') !== false) {
$modelName = substr($modelName, strrpos($modelName, '.') + 1);
}

if(substr($entityName, -1) !== 's') {
$entityName = $entityName . 's';
if(substr($modelName, -1) !== 's') {
$modelName = $modelName . 's';
}

return $entityName;
return $modelName;
}
}
Loading

0 comments on commit a5fbda8

Please sign in to comment.