Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

Commit

Permalink
feat: Working on the new version
Browse files Browse the repository at this point in the history
  • Loading branch information
allanmcarvalho committed Jun 28, 2020
1 parent d194a34 commit 2fb13dc
Show file tree
Hide file tree
Showing 10 changed files with 208 additions and 23 deletions.
3 changes: 2 additions & 1 deletion src/Controller/Component/DataTablesComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use DataTables\Table\Builder;
use DataTables\Table\Columns;
use DataTables\Table\ConfigBundle;
use DataTables\Table\Configure as TableConfigure;
use DataTables\Table\Option\MainOption;
use DataTables\Table\QueryBaseState;
use DataTables\Tools\Functions;
Expand All @@ -35,7 +36,7 @@ class DataTablesComponent extends Component {
*/
public function initialize(array $config): void {
parent::initialize($config);
$forceCache = (bool)Configure::read('DataTables.StorageEngine.forceCache');
$forceCache = TableConfigure::getInstance()->isForceCache();
if (Configure::read('debug') === true && $forceCache === false) {
$this->_cache = false;
}
Expand Down
15 changes: 13 additions & 2 deletions src/Table/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Cake\Routing\Router;
use Cake\Utility\Inflector;
use DataTables\StorageEngine\StorageEngineInterface;
use DataTables\Table\Configure as TableConfigure;
use DataTables\Tools\Functions;

/**
Expand Down Expand Up @@ -50,7 +51,7 @@ public static function getInstance() {
* @return \DataTables\StorageEngine\StorageEngineInterface
*/
public function getStorageEngine(): StorageEngineInterface {
$class = Configure::read('DataTables.StorageEngine.class');
$class = TableConfigure::getInstance()->getStorageEngine();

return new $class();
}
Expand All @@ -67,6 +68,16 @@ public function getConfigBundle(string $dataTables, bool $cache = true): ConfigB
$dataTablesFQN = $this->parseClassNameToFQN($dataTables);
$dataTablesName = explode('\\', $dataTables);
$dataTablesName = array_pop($dataTablesName);
$configure = TableConfigure::getInstance();
$appDataTablesClassFqn = Configure::read('App.namespace') . '\\DataTables\\AppDataTables';
if (class_exists($appDataTablesClassFqn)) {
$appDataTablesClass = new $appDataTablesClassFqn();
if (method_exists($appDataTablesClass, 'configure')) {
$appDataTablesClass->configure($configure);
}
} else {
throw new FatalErrorException(sprintf('The AppDataTables class was not found in "%s".', $appDataTablesClassFqn));
}
$storageEngine = $this->getStorageEngine();
$md5 = Functions::getInstance()->getClassAndVersionMd5($dataTablesFQN);
$cacheKey = Inflector::underscore($dataTablesName);
Expand All @@ -77,7 +88,7 @@ public function getConfigBundle(string $dataTables, bool $cache = true): ConfigB
Assets::getInstance()->applyConfig($configBundle->Assets);
}
if (empty($configBundle) || $configBundle->getCheckMd5() !== $md5) {
$configBundle = new ConfigBundle($md5, $dataTablesFQN);
$configBundle = new ConfigBundle($md5, $dataTablesFQN, $appDataTablesClass);
if ($cache && !$storageEngine->save($cacheKey, $configBundle)) {
throw new FatalErrorException('Unable to save the ConfigBundle cache.');
}
Expand Down
13 changes: 5 additions & 8 deletions src/Table/ConfigBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

namespace DataTables\Table;

use Cake\Core\Configure;
use Cake\Error\FatalErrorException;
use Cake\Routing\Router;
use Cake\Utility\Inflector;
Expand Down Expand Up @@ -63,13 +62,15 @@ final class ConfigBundle {
/**
* @param string $checkMd5 The md5 used to check changes.
* @param string $dataTablesFQN Tables class name.
* @param object $appDataTablesClass The AppDataTables class from application.
* @throws \Twig\Error\LoaderError
* @throws \Twig\Error\RuntimeError
* @throws \Twig\Error\SyntaxError
*/
public function __construct(
string $checkMd5,
string $dataTablesFQN
string $dataTablesFQN,
object $appDataTablesClass
) {
$this->_checkMd5 = $checkMd5;
$this->_dataTableFQN = $dataTablesFQN;
Expand All @@ -83,12 +84,8 @@ public function __construct(
$this->Assets = Assets::getInstance();
$this->Options->setColumns($this->Columns);
$this->_dataTables->config($this);
if (file_exists(APP . 'DataTables' . DS . 'AppDataTables.php')) {
$class = Configure::read('App.namespace') . '\\DataTables\\AppDataTables';
$appDataTables = new $class();
if (method_exists($appDataTables, 'mainConfig')) {
$appDataTables->mainConfig($this->Options, $this->Assets);
}
if (method_exists($appDataTablesClass, 'mainConfig')) {
$appDataTablesClass->mainConfig($this->Options, $this->Assets);
}
}

Expand Down
124 changes: 124 additions & 0 deletions src/Table/Configure.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php
/**
* Copyright (c) Allan Carvalho 2020.
* Under Mit License
* link: https://github.com/wsssoftware/cakephp-data-renderer
* author: Allan Carvalho <allan.m.carvalho@outlook.com>
* license: MIT License https://github.com/wsssoftware/cakephp-datatables/blob/master/LICENSE
*/
declare(strict_types = 1);

namespace DataTables\Table;

use Cake\Core\Configure as CakePhpConfigure;

/**
* Class Configure
* Created by allancarvalho in june 28, 2020
*/
class Configure {

/**
* @var \DataTables\Table\Configure
*/
private static $_instance;

/**
* @var bool
*/
private $columnAutoGeneratedWarning;

/**
* @var string
*/
private $storageEngine;

/**
* @var bool
*/
private $forceCache;

public function __construct() {
$this->setColumnAutoGeneratedWarning(CakePhpConfigure::read('DataTables.columnAutoGeneratedWarning'));
$this->setStorageEngine(CakePhpConfigure::read('DataTables.StorageEngine.class'));
$this->setForceCache(CakePhpConfigure::read('DataTables.StorageEngine.forceCache'));
}

/**
* Return a instance of builder object.
*
* @param bool $reset
* @return \DataTables\Table\Configure
*/
public static function getInstance(bool $reset = false) {
if (static::$_instance === null || $reset === true) {
static::$_instance = new self();
}

return static::$_instance;
}

/**
* Check if the table columns data is auto generated.
*
* @return bool
*/
public function isColumnAutoGeneratedWarning(): bool {
return $this->columnAutoGeneratedWarning;
}

/**
* Set if the plugin must show a blink alert when the table columns data is auto generated.
*
* @param bool $columnAutoGeneratedWarning
* @return $this
*/
public function setColumnAutoGeneratedWarning(bool $columnAutoGeneratedWarning) {
$this->columnAutoGeneratedWarning = $columnAutoGeneratedWarning;

return $this;
}

/**
* Get the current StorageEngine class FQN
*
* @return string
*/
public function getStorageEngine(): string {
return $this->storageEngine;
}

/**
* Set the StorageEngine class FQN
*
* @param string $storageEngine
* @return $this
*/
public function setStorageEngine(string $storageEngine) {
$this->storageEngine = $storageEngine;

return $this;
}

/**
* Check if the plugin must do the cache even debug is on.
*
* @return bool
*/
public function isForceCache(): bool {
return $this->forceCache;
}

/**
* Set if the plugin must do the cache even debug is on.
*
* @param bool $forceCache
* @return $this
*/
public function setForceCache(bool $forceCache) {
$this->forceCache = $forceCache;

return $this;
}

}
3 changes: 1 addition & 2 deletions src/Table/Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

namespace DataTables\Table;

use Cake\Core\Configure;
use Cake\Datasource\EntityInterface;
use Cake\I18n\Number;
use Cake\ORM\Association\BelongsTo;
Expand Down Expand Up @@ -111,7 +110,7 @@ private function getFormattedColumn(string $columnName, Column $column, EntityIn
}
}
$pattern = '%s';
if (Configure::read('DataTables.columnAutoGeneratedWarning', true)) {
if (Configure::getInstance()->isColumnAutoGeneratedWarning()) {
$title = __d('data_tables', 'this column data was auto generated.');
$pattern = "<span class='data-tables-plugin auto-loaded' title='$title'>%s</span>";
}
Expand Down
10 changes: 6 additions & 4 deletions src/View/Helper/DataTablesHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Cake\View\Helper;
use DataTables\Table\Assets;
use DataTables\Table\Builder;
use DataTables\Table\Configure as TableConfigure;
use DataTables\Table\UrlQueryBuilder;

/**
Expand All @@ -32,7 +33,6 @@ class DataTablesHelper extends Helper {
protected $_defaultConfig = [
'cache' => true,
'minify' => true,
'autoLoadLibraries' => true,
];

/**
Expand All @@ -45,17 +45,19 @@ class DataTablesHelper extends Helper {
*/
public function initialize(array $config): void {
parent::initialize($config);
$this->setConfig('autoLoadLibraries', Configure::read('DataTables.autoLoadLibraries'));
$forceCache = (bool)Configure::read('DataTables.StorageEngine.forceCache');
$forceCache = TableConfigure::getInstance()->isForceCache();
if (Configure::read('debug') === true && $forceCache === false) {
$this->setConfig('cache', false);
}
$this->setConfig($config);
EventManager::instance()->on('View.beforeLayout', function ()
{
$assets = Assets::getInstance();
if (TableConfigure::getInstance()->isColumnAutoGeneratedWarning()) {
$this->getView()->Html->css('DataTables.plugin/data-tables.min.css', ['block' => $assets->getCssBlock()]);
}
if (!empty($this->_configBundles)) {
$tablesScript = $this->renderJs();
$assets = Assets::getInstance();
if ($assets->isEnabled()) {
$this->getView()->Html->css(Router::url($assets->getCssUrlArray(), true), ['block' => $assets->getCssBlock()]);
$this->getView()->Html->script(Router::url($assets->getScriptUrlArray(), true), ['block' => $assets->getScriptBlock()]);
Expand Down
12 changes: 10 additions & 2 deletions templates/bake/app_config.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace {{ namespace }}\DataTables;
use DataTables\Table\Option\MainOption;
use DataTables\Table\Assets;
use DataTables\Table\Configure;
/**
* Class {{ name }}DataTables
Expand All @@ -17,8 +18,15 @@ class {{ name }}DataTables {
* @param \DataTables\Table\Option\MainOption $options Options of DataTable library.
* @param \DataTables\Table\Assets $assets Options for asset load.
*/
public function mainConfig(MainOption $options, Assets $assets): void
{
public function mainConfig(MainOption $options, Assets $assets): void {
// TODO: Configure here the rules that will be applied to all tables.
}
/**
* This is the plugin configuration that will be applied to all DataTables tables.
* @param \DataTables\Table\Configure $configure Plugin base configurations.
*/
public function configure(Configure $configure): void {
// TODO: Configure here the plugin configuration that will be applied to all tables.
}
}
6 changes: 2 additions & 4 deletions templates/bake/config.twig
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ class {{ name }}DataTables extends DataTables {
/**
* @param \DataTables\Table\ConfigBundle $configBundle
*/
public function config(ConfigBundle $configBundle): void
{
public function config(ConfigBundle $configBundle): void {
// TODO: Configure your table here.
}
Expand All @@ -30,8 +29,7 @@ class {{ name }}DataTables extends DataTables {
* @param \Cake\Datasource\EntityInterface|\{{ namespace }}\Model\Entity\{{ entityName }} $entity
* @param \DataTables\Table\Renderer $renderer
*/
public function rowRenderer(DataTablesView $appView, EntityInterface $entity, Renderer $renderer): void
{
public function rowRenderer(DataTablesView $appView, EntityInterface $entity, Renderer $renderer): void {
// TODO: Configure each row render here.
}
}
1 change: 1 addition & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@

class_alias(TestApp\Controller\AppController::class, 'App\Controller\AppController');
class_alias(TestApp\DataTables\CategoriesDataTables::class, 'App\DataTables\CategoriesTables');
class_alias(TestApp\DataTables\AppDataTables::class, 'App\DataTables\AppDataTables');

if (file_exists($root . '/config/bootstrap.php')) {
require $root . '/config/bootstrap.php';
Expand Down
44 changes: 44 additions & 0 deletions tests/test_app/src/DataTables/AppDataTables.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* Copyright (c) Allan Carvalho 2020.
* Under Mit License
* php version 7.2
*
* link https://github.com/allanmcarvalho/cakephp-data-renderer
* author Allan Carvalho <allan.m.carvalho@outlook.com>
*/
declare(strict_types = 1);

namespace TestApp\DataTables;

use DataTables\Table\Assets;
use DataTables\Table\Configure;
use DataTables\Table\Option\MainOption;

/**
* Class AppDataTables
*/
class AppDataTables {

/**
* This will be applied to all DataTables tables.
*
* @param \DataTables\Table\Option\MainOption $options Options of DataTable library.
* @param \DataTables\Table\Assets $assets Options for asset load.
* @return void
*/
public function mainConfig(MainOption $options, Assets $assets): void {
// TODO: Configure here the rules that will be applied to all tables.
}

/**
* This is the plugin configuration that will be applied to all DataTables tables.
*
* @param \DataTables\Table\Configure $configure Plugin base configurations.
* @return void
*/
public function configure(Configure $configure): void {
// TODO: Configure here the plugin configuration that will be applied to all tables.
}

}

0 comments on commit 2fb13dc

Please sign in to comment.