ConfigTrait.php 997 B

123456789101112131415161718192021222324252627282930313233343536
  1. <?php
  2. declare(strict_types=1);
  3. namespace SixShop\System\Trait;
  4. use SixShop\System\ExtensionManager;
  5. trait ConfigTrait
  6. {
  7. public function __construct(private readonly ExtensionManager $extensionManager, private array $options = [])
  8. {
  9. }
  10. public function getConfig(string $key = null): mixed
  11. {
  12. if (empty($this->options)) {
  13. if (!method_exists($this, 'getExtensionID')) {
  14. return [];
  15. }
  16. $this->options = $this->extensionManager->getExtensionConfig($this->getExtensionID());
  17. if (method_exists($this, 'getOptions')) {
  18. $this->options = array_merge($this->options, $this->getOptions());
  19. }
  20. }
  21. return $key ? ($this->options[$key] ?? null) : $this->options;
  22. }
  23. public function __get(string $name): mixed
  24. {
  25. return $this->getConfig($name);
  26. }
  27. public function __isset(string $name): bool
  28. {
  29. return $this->getConfig($name) !== null;
  30. }
  31. }