|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * ManiyaTech |
| 4 | + * |
| 5 | + * @author Milan Maniya |
| 6 | + * @package ManiyaTech_Core |
| 7 | + */ |
| 8 | + |
| 9 | +declare(strict_types=1); |
| 10 | + |
| 11 | +namespace ManiyaTech\Core\Model; |
| 12 | + |
| 13 | +use InvalidArgumentException; |
| 14 | +use Magento\Framework\Component\ComponentRegistrar; |
| 15 | +use Magento\Framework\Component\ComponentRegistrarInterface; |
| 16 | +use Magento\Framework\Filesystem\Directory\ReadFactory; |
| 17 | +use Magento\Framework\Filesystem\Directory\ReadInterface; |
| 18 | + |
| 19 | +class Module |
| 20 | +{ |
| 21 | + /** |
| 22 | + * Cached composer.json data for modules. |
| 23 | + * |
| 24 | + * @var array<string, array|object> |
| 25 | + */ |
| 26 | + private array $composerJsonData = []; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var ComponentRegistrarInterface |
| 30 | + */ |
| 31 | + private ComponentRegistrarInterface $componentRegistrar; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var ReadFactory |
| 35 | + */ |
| 36 | + private ReadFactory $readFactory; |
| 37 | + |
| 38 | + /** |
| 39 | + * Module constructor. |
| 40 | + * |
| 41 | + * @param ComponentRegistrarInterface $componentRegistrar |
| 42 | + * @param ReadFactory $readFactory |
| 43 | + */ |
| 44 | + public function __construct( |
| 45 | + ComponentRegistrarInterface $componentRegistrar, |
| 46 | + ReadFactory $readFactory |
| 47 | + ) { |
| 48 | + $this->componentRegistrar = $componentRegistrar; |
| 49 | + $this->readFactory = $readFactory; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Get decoded composer.json data for the given module. |
| 54 | + * |
| 55 | + * @param string $moduleName |
| 56 | + * @param bool $assoc Return associative array if true, otherwise stdClass |
| 57 | + * @return array|object |
| 58 | + */ |
| 59 | + public function getLocalComposerData(string $moduleName, bool $assoc = false): array|object |
| 60 | + { |
| 61 | + if (!isset($this->composerJsonData[$moduleName])) { |
| 62 | + try { |
| 63 | + $path = $this->componentRegistrar->getPath(ComponentRegistrar::MODULE, $moduleName); |
| 64 | + /** @var ReadInterface $directoryRead */ |
| 65 | + $directoryRead = $this->readFactory->create($path); |
| 66 | + $composerJson = $directoryRead->readFile('composer.json'); |
| 67 | + $this->composerJsonData[$moduleName] = $this->decodeJson($composerJson, $assoc); |
| 68 | + } catch (\Exception $e) { |
| 69 | + // In case of error (e.g., file not found), store an empty array to prevent repeated file reads |
| 70 | + $this->composerJsonData[$moduleName] = $assoc ? [] : (object)[]; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + return $this->composerJsonData[$moduleName]; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Get the installed version from composer.json for the specified module. |
| 79 | + * |
| 80 | + * @param string $moduleName |
| 81 | + * @return string |
| 82 | + */ |
| 83 | + public function getInstalledVersion(string $moduleName): string |
| 84 | + { |
| 85 | + $data = $this->getLocalComposerData($moduleName, true); |
| 86 | + return $data['version'] ?? '0.0.0'; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Decode JSON safely with error handling. |
| 91 | + * |
| 92 | + * @param string $json |
| 93 | + * @param bool $assoc |
| 94 | + * @return array|object |
| 95 | + * @throws InvalidArgumentException If JSON decoding fails |
| 96 | + */ |
| 97 | + public function decodeJson(string $json, bool $assoc = false): array|object |
| 98 | + { |
| 99 | + $result = json_decode($json, $assoc); |
| 100 | + if (json_last_error() !== JSON_ERROR_NONE) { |
| 101 | + throw new InvalidArgumentException('Unable to decode JSON: ' . json_last_error_msg()); |
| 102 | + } |
| 103 | + |
| 104 | + return $result; |
| 105 | + } |
| 106 | +} |
0 commit comments