Skip to content

Commit

Permalink
use FileHelper to store and load custom rules
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinpapst committed Nov 22, 2021
1 parent 399c81d commit 84f24c0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 27 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 1.5

Compatibility: requires minimum Kimai 1.9

- Use FileHelper to store and load custom CSS rules

## 1.4

- Added permission `select_custom_css` to hide pre-made rules
Expand Down
42 changes: 21 additions & 21 deletions Repository/CustomCssRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,18 @@

namespace KimaiPlugin\CustomCSSBundle\Repository;

use App\Utils\FileHelper;
use KimaiPlugin\CustomCSSBundle\Entity\CustomCss;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;

class CustomCssRepository
{
/**
* @var string
*/
protected $cssFile;
/**
* @var string
*/
protected $ruleSetDir;
private $fileHelper;

/**
* @param string $pluginDirectory
*/
public function __construct(string $pluginDirectory, string $dataDirectory)
public function __construct(FileHelper $fileHelper)
{
$this->ruleSetDir = $pluginDirectory . '/CustomCSSBundle/Resources/ruleset';
$this->cssFile = $dataDirectory . '/custom-css-bundle.css';
$this->fileHelper = $fileHelper;
}

/**
Expand All @@ -39,9 +29,10 @@ public function __construct(string $pluginDirectory, string $dataDirectory)
public function getPredefinedStyles()
{
$rules = [];
$searchDir = __DIR__ . '/../Resources/ruleset';

$finder = new Finder();
$finder->ignoreUnreadableDirs()->ignoreVCS(true)->files()->name('*.css')->in($this->ruleSetDir)->depth('< 2');
$finder->ignoreUnreadableDirs()->ignoreVCS(true)->files()->name('*.css')->in($searchDir)->depth('< 2');
/** @var SplFileInfo $bundleDir */
foreach ($finder as $file) {
$name = str_replace('.css', '', $file->getFilename());
Expand All @@ -53,19 +44,26 @@ public function getPredefinedStyles()
return $rules;
}

private function getStorageFilename(): string
{
return $this->fileHelper->getDataDirectory() . '/custom-css-bundle.css';
}

/**
* @param CustomCss $entity
* @return bool
* @throws \Exception
*/
public function saveCustomCss(CustomCss $entity)
{
if (file_exists($this->cssFile) && !is_writable($this->cssFile)) {
throw new \Exception('CSS file is not writable: ' . $this->cssFile);
$file = $this->getStorageFilename();

if (file_exists($file) && !is_writable($file)) {
throw new \Exception('CSS file is not writable: ' . $file);
}

if (false === file_put_contents($this->cssFile, $entity->getCustomCss())) {
throw new \Exception('Failed saving custom css rules to file: ' . $this->cssFile);
if (false === file_put_contents($file, $entity->getCustomCss())) {
throw new \Exception('Failed saving custom css rules to file: ' . $file);
}

return true;
Expand All @@ -76,10 +74,12 @@ public function saveCustomCss(CustomCss $entity)
*/
public function getCustomCss()
{
$file = $this->getStorageFilename();

$entity = new CustomCss();

if (file_exists($this->cssFile) && is_readable($this->cssFile)) {
$entity->setCustomCss(file_get_contents($this->cssFile));
if (file_exists($file) && is_readable($file)) {
$entity->setCustomCss(file_get_contents($file));
}

return $entity;
Expand Down
3 changes: 0 additions & 3 deletions Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ services:
autowire: true
autoconfigure: true
public: false
bind:
$pluginDirectory: "%kimai.plugin_dir%"
$dataDirectory: "%kimai.data_dir%"

KimaiPlugin\CustomCSSBundle\:
resource: '../../*'
Expand Down
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "A Kimai 2 plugin, which allows to edit custom CSS rules through an administration screen.",
"homepage": "https://www.kimai.org/store/keleo-css-custom-bundle.html",
"type": "kimai-plugin",
"version": "1.4",
"version": "1.5",
"keywords": [
"kimai",
"kimai-plugin"
Expand All @@ -18,8 +18,7 @@
],
"extra": {
"kimai": {
"require": "1.4",
"version": "1.4",
"require": "1.9",
"name": "CustomCSSBundle"
}
}
Expand Down

0 comments on commit 84f24c0

Please sign in to comment.