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 27, 2020
1 parent 21b619f commit d194a34
Show file tree
Hide file tree
Showing 16 changed files with 135 additions and 10 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"twig/twig": "^3.0",
"matthiasmullie/minify": "^1.3.50",
"ext-json": "*",
"ext-intl": "*"
"ext-intl": "*",
"ext-fileinfo": "*"
},
"require-dev": {
"phpunit/phpunit": "^8.0",
Expand Down
1 change: 1 addition & 0 deletions config/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
use Cake\Routing\Router;

Router::plugin('DataTables', ['path' => '/data-tables'], function (RouteBuilder $builder) {
$builder->connect('/images/*', ['controller' => 'Assets', 'action' => 'images']);
$builder->fallbacks(DashedRoute::class);
});
26 changes: 26 additions & 0 deletions src/Command/DataTablesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public function execute(Arguments $args, ConsoleIo $io): ?int {

return static::CODE_SUCCESS;
}
$this->bakeAppConfig($args, $io);
$this->bakeConfig($args, $io);
$this->bakeConfigTest($args, $io);
} else {
Expand Down Expand Up @@ -154,6 +155,31 @@ public function bakeConfig(Arguments $args, ConsoleIo $io) {
$this->deleteEmptyFile($emptyFile, $io);
}

/**
* Bake a config class.
*
* @param \Cake\Console\Arguments $args
* @param \Cake\Console\ConsoleIo $io
* @return void
*/
public function bakeAppConfig(Arguments $args, ConsoleIo $io) {
$filename = $this->getPath($args) . $this->fileName('App');
if (!file_exists($filename)) {
$this->_template = 'DataTables.app_config';
$renderer = new TemplateRenderer();
$renderer->set('name', 'App');
$templateData = $this->templateData($args);
$renderer->set($templateData);
$contents = $renderer->generate($this->template());
$io->createFile($filename, $contents, (bool)$args->getOption('force'));

$emptyFile = $this->getPath($args) . '.gitkeep';
$this->deleteEmptyFile($emptyFile, $io);
} else {
$io->info(sprintf('"%s" will not baked because it already exists.', $this->fileName('App')));
}
}

/**
* Bake a config class test.
*
Expand Down
30 changes: 28 additions & 2 deletions src/Controller/AssetsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

namespace DataTables\Controller;

use Cake\Http\Exception\NotFoundException;

/**
* Class AssetsController
* Created by allancarvalho in june 26, 2020
Expand Down Expand Up @@ -30,7 +32,10 @@ public function css() {
$query = $this->getRequest()->getQuery();
$body = $this->Css->getFilesBody($query, 'css');

return $this->getResponse()->withType('text/css;charset=UTF-8')->withStringBody($body);
return $this->getResponse()
->withCache('-1 minute', '+30 days')
->withType('text/css;charset=UTF-8')
->withStringBody($body);
}

/**
Expand All @@ -42,7 +47,28 @@ public function script() {
$query = $this->getRequest()->getQuery();
$body = $this->Js->getFilesBody($query, 'js');

return $this->getResponse()->withType('application/javascript')->withStringBody($body);
return $this->getResponse()
->withCache('-1 minute', '+30 days')
->withType('application/javascript')
->withStringBody($body);
}

/**
* Get the assets images
*
* @param string $filename Name of file.
* @return \Cake\Http\Response
*/
public function images($filename) {
$fullFilePath = DATA_TABLES_WWW_ROOT . 'images' . DS . $filename;
if (!file_exists($fullFilePath)) {
throw new NotFoundException(sprintf('Image "%s" not found!', $filename));
}

return $this->getResponse()
->withCache('-1 minute', '+30 days')
->withType(mime_content_type($fullFilePath))
->withStringBody(file_get_contents($fullFilePath));
}

}
6 changes: 5 additions & 1 deletion src/Controller/Component/AssetsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ protected function getBodies(array $files, string $type, string $version): strin
$body = '';
$basePath = DATA_TABLES_WWW_ROOT . $type . DS . $version . DS;
foreach ($files as $file) {
$body .= file_get_contents($basePath . $file) . "\n";
$body .= file_get_contents($basePath . $file);
if ($type === 'js') {
$body .= ';';
}
$body .= "\n";
}

return $body;
Expand Down
8 changes: 4 additions & 4 deletions src/StorageEngine/CacheStorageEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,21 @@ public function __construct(?string $cacheConfigName = null) {
* @inheritDoc
*/
public function save(string $key, ConfigBundle $configBundle): bool {
return Cache::write($key, $configBundle, '_data_tables_config_bundles_');
return Cache::write($key, $configBundle, $this->_cacheConfigName);
}

/**
* @inheritDoc
*/
public function exists(string $key): bool {
return Cache::read($key, '_data_tables_config_bundles_') instanceof ConfigBundle;
return Cache::read($key, $this->_cacheConfigName) instanceof ConfigBundle;
}

/**
* @inheritDoc
*/
public function read(string $key): ?ConfigBundle {
$configBundle = Cache::read($key, '_data_tables_config_bundles_');
$configBundle = Cache::read($key, $this->_cacheConfigName);

return ($configBundle instanceof ConfigBundle) ? $configBundle : null;
}
Expand All @@ -63,7 +63,7 @@ public function read(string $key): ?ConfigBundle {
* @inheritDoc
*/
public function delete(string $key): bool {
return Cache::delete($key, '_data_tables_config_bundles_');
return Cache::delete($key, $this->_cacheConfigName);
}

}
29 changes: 29 additions & 0 deletions src/Table/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,35 @@ public static function getInstance(bool $reset = false) {
return static::$_instance;
}

/**
* Apply a assets instance to main instance.
*
* @param \DataTables\Table\Assets $assets
* @return void
*/
public function applyConfig(Assets $assets): void {
$this->setEnabled($assets->isEnabled());
$this->setAutoload($assets->isAutoload());
$this->setCssBlock($assets->getCssBlock());
$this->setScriptBlock($assets->getScriptBlock());
$this->setDataTablesVersion($assets->getDataTablesVersion());
$this->setTheme($assets->getTheme());
$this->setLoadThemeLibrary($assets->isLoadThemeLibrary());
$this->setJquery($assets->getJquery());
$this->setLoadPluginAutoFill($assets->isLoadPluginAutoFill());
$this->setLoadPluginButtons($assets->isLoadPluginButtons());
$this->setLoadPluginColReorder($assets->isLoadPluginColReorder());
$this->setLoadPluginFixedColumns($assets->isLoadPluginFixedColumns());
$this->setLoadPluginFixedHeader($assets->isLoadPluginFixedHeader());
$this->setLoadPluginKeyTable($assets->isLoadPluginKeyTable());
$this->setLoadPluginResponsive($assets->isLoadPluginResponsive());
$this->setLoadPluginRowGroup($assets->isLoadPluginRowGroup());
$this->setLoadPluginRowReorder($assets->isLoadPluginRowReorder());
$this->setLoadPluginScroller($assets->isLoadPluginScroller());
$this->setLoadPluginSearchPanes($assets->isLoadPluginSearchPanes());
$this->setLoadPluginSelect($assets->isLoadPluginSelect());
}

/**
* Return the assets to default state.
*
Expand Down
1 change: 1 addition & 0 deletions src/Table/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public function getConfigBundle(string $dataTables, bool $cache = true): ConfigB
if ($cache === true && $storageEngine->exists($cacheKey)) {
/** @var \DataTables\Table\ConfigBundle $configBundle */
$configBundle = $storageEngine->read($cacheKey);
Assets::getInstance()->applyConfig($configBundle->Assets);
}
if (empty($configBundle) || $configBundle->getCheckMd5() !== $md5) {
$configBundle = new ConfigBundle($md5, $dataTablesFQN);
Expand Down
10 changes: 9 additions & 1 deletion src/Table/ConfigBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace DataTables\Table;

use Cake\Core\Configure;
use Cake\Error\FatalErrorException;
use Cake\Routing\Router;
use Cake\Utility\Inflector;
Expand Down Expand Up @@ -80,8 +81,15 @@ public function __construct(
$this->Options = new MainOption($this, $this->getUrl());
$this->Query = new QueryBaseState();
$this->Assets = Assets::getInstance();
$this->_dataTables->config($this);
$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);
}
}
}

/**
Expand Down
7 changes: 6 additions & 1 deletion src/Tools/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace DataTables\Tools;

use Cake\Core\Configure;
use Cake\Routing\Router;
use Cake\Utility\Inflector;
use DataTables\Table\ConfigBundle;
Expand Down Expand Up @@ -77,10 +78,14 @@ public function arrayKeyLast(array $array) {
* @return string
*/
public function getClassAndVersionMd5(string $classWithNameSpace): string {
$appClassMd5 = '';
if (file_exists(APP . 'DataTables' . DS . 'AppDataTables.php')) {
$appClassMd5 = $this->getClassMd5(Configure::read('App.namespace') . '\\DataTables\\AppDataTables');
}
$classMd5 = $this->getClassMd5($classWithNameSpace);
$pluginCurrentHash = $this->getPluginCurrentCommit();

return md5($classMd5 . $pluginCurrentHash);
return md5($appClassMd5 . $classMd5 . $pluginCurrentHash);
}

/**
Expand Down
24 changes: 24 additions & 0 deletions templates/bake/app_config.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
declare(strict_types=1);
namespace {{ namespace }}\DataTables;
use DataTables\Table\Option\MainOption;
use DataTables\Table\Assets;
/**
* Class {{ name }}DataTables
*/
class {{ name }}DataTables {
/**
* 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.
*/
public function mainConfig(MainOption $options, Assets $assets): void
{
// TODO: Configure here the rules that will be applied to all tables.
}
}
Binary file added webroot/images/sort_asc.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added webroot/images/sort_asc_disabled.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added webroot/images/sort_both.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added webroot/images/sort_desc.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added webroot/images/sort_desc_disabled.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d194a34

Please sign in to comment.