From 84f24c015609d19c9fb20f703263c18eef974ba7 Mon Sep 17 00:00:00 2001 From: Kevin Papst Date: Mon, 22 Nov 2021 16:08:03 +0100 Subject: [PATCH] use FileHelper to store and load custom rules --- CHANGELOG.md | 6 +++++ Repository/CustomCssRepository.php | 42 +++++++++++++++--------------- Resources/config/services.yaml | 3 --- composer.json | 5 ++-- 4 files changed, 29 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6a4e21c..196a8ce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/Repository/CustomCssRepository.php b/Repository/CustomCssRepository.php index 644a88d..11db97b 100644 --- a/Repository/CustomCssRepository.php +++ b/Repository/CustomCssRepository.php @@ -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; } /** @@ -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()); @@ -53,6 +44,11 @@ public function getPredefinedStyles() return $rules; } + private function getStorageFilename(): string + { + return $this->fileHelper->getDataDirectory() . '/custom-css-bundle.css'; + } + /** * @param CustomCss $entity * @return bool @@ -60,12 +56,14 @@ public function getPredefinedStyles() */ 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; @@ -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; diff --git a/Resources/config/services.yaml b/Resources/config/services.yaml index 05b4031..59b129d 100644 --- a/Resources/config/services.yaml +++ b/Resources/config/services.yaml @@ -3,9 +3,6 @@ services: autowire: true autoconfigure: true public: false - bind: - $pluginDirectory: "%kimai.plugin_dir%" - $dataDirectory: "%kimai.data_dir%" KimaiPlugin\CustomCSSBundle\: resource: '../../*' diff --git a/composer.json b/composer.json index 326480d..a682d66 100644 --- a/composer.json +++ b/composer.json @@ -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" @@ -18,8 +18,7 @@ ], "extra": { "kimai": { - "require": "1.4", - "version": "1.4", + "require": "1.9", "name": "CustomCSSBundle" } }