BBCodeList.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /**
  3. * This file is part of the ForkBB <https://github.com/forkbb>.
  4. *
  5. * @copyright (c) Visman <mio.visman@yandex.ru, https://github.com/MioVisman>
  6. * @license The MIT License (MIT)
  7. */
  8. declare(strict_types=1);
  9. namespace ForkBB\Models\BBCodeList;
  10. use ForkBB\Core\Container;
  11. use ForkBB\Models\Model;
  12. use RuntimeException;
  13. class BBCodeList extends Model
  14. {
  15. /**
  16. * Ключ модели для контейнера
  17. * @var string
  18. */
  19. protected $cKey = 'BBCodeList';
  20. public function __construct(string $file, Container $container)
  21. {
  22. parent::__construct($container);
  23. $this->fileDefault = "{$container->DIR_CONFIG}/{$file}";
  24. $this->fileCache = "{$container->DIR_CACHE}/generated_bbcode.php";
  25. }
  26. /**
  27. * Загружает массив сгенерированных bbcode
  28. */
  29. public function init(): BBCodeList
  30. {
  31. if (! \is_file($this->fileCache)) {
  32. $this->generate();
  33. }
  34. $this->list = include $this->fileCache;
  35. return $this;
  36. }
  37. /**
  38. * Очищает кеш сгенерированных bbcode
  39. */
  40. public function reset(): BBCodeList
  41. {
  42. if (\is_file($this->fileCache)) {
  43. if (\unlink($this->fileCache)) {
  44. return $this->invalidate();
  45. } else {
  46. throw new RuntimeException('The generated bbcode file cannot be deleted');
  47. }
  48. } else {
  49. return $this;
  50. }
  51. }
  52. /**
  53. * Очищает opcache/apc от закэшированного файла
  54. */
  55. public function invalidate(): BBCodeList
  56. {
  57. if (\function_exists('\\opcache_invalidate')) {
  58. \opcache_invalidate($this->fileCache, true);
  59. } elseif (\function_exists('\\apc_delete_file')) {
  60. \apc_delete_file($this->fileCache);
  61. }
  62. return $this;
  63. }
  64. }