|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © MagePal LLC. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + * http://www.magepal.com | [email protected] |
| 6 | +*/ |
| 7 | + |
| 8 | +namespace MagePal\NewsletterSignupEmail\Block\Adminhtml\System\Config\Form\Composer; |
| 9 | + |
| 10 | +class Version extends \Magento\Config\Block\System\Config\Form\Field |
| 11 | +{ |
| 12 | + |
| 13 | + /** |
| 14 | + * @var \Magento\Framework\App\DeploymentConfig |
| 15 | + */ |
| 16 | + protected $deploymentConfig; |
| 17 | + |
| 18 | + /** |
| 19 | + * @var \Magento\Framework\Component\ComponentRegistrarInterface |
| 20 | + */ |
| 21 | + protected $componentRegistrar; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var \Magento\Framework\Filesystem\Directory\ReadFactory |
| 25 | + */ |
| 26 | + protected $readFactory; |
| 27 | + |
| 28 | + /** |
| 29 | + * @param \Magento\Framework\App\DeploymentConfig $deploymentConfig |
| 30 | + * @param \Magento\Framework\Component\ComponentRegistrarInterface $componentRegistrar |
| 31 | + * @param \Magento\Framework\Filesystem\Directory\ReadFactory $readFactory |
| 32 | + */ |
| 33 | + public function __construct( |
| 34 | + \Magento\Backend\Block\Template\Context $context, |
| 35 | + \Magento\Framework\App\DeploymentConfig $deploymentConfig, |
| 36 | + \Magento\Framework\Component\ComponentRegistrarInterface $componentRegistrar, |
| 37 | + \Magento\Framework\Filesystem\Directory\ReadFactory $readFactory, |
| 38 | + array $data = [] |
| 39 | + ) { |
| 40 | + $this->deploymentConfig = $deploymentConfig; |
| 41 | + $this->componentRegistrar = $componentRegistrar; |
| 42 | + $this->readFactory = $readFactory; |
| 43 | + parent::__construct($context, $data); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Render button |
| 48 | + * |
| 49 | + * @param \Magento\Framework\Data\Form\Element\AbstractElement $element |
| 50 | + * @return string |
| 51 | + * @throws \Magento\Framework\Exception\LocalizedException |
| 52 | + */ |
| 53 | + public function render(\Magento\Framework\Data\Form\Element\AbstractElement $element) |
| 54 | + { |
| 55 | + // Remove scope label |
| 56 | + $element->unsScope()->unsCanUseWebsiteValue()->unsCanUseDefaultValue(); |
| 57 | + return parent::render($element); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Return element html |
| 62 | + * |
| 63 | + * @param \Magento\Framework\Data\Form\Element\AbstractElement $element |
| 64 | + * @return string |
| 65 | + * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
| 66 | + */ |
| 67 | + protected function _getElementHtml(\Magento\Framework\Data\Form\Element\AbstractElement $element) |
| 68 | + { |
| 69 | + return 'v' . $this->getVersion(); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Get Module version number |
| 74 | + * |
| 75 | + * @return string |
| 76 | + */ |
| 77 | + public function getVersion() |
| 78 | + { |
| 79 | + return $this->getComposerVersion($this->getModuleName()); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @return string |
| 84 | + */ |
| 85 | + public function getModuleName() |
| 86 | + { |
| 87 | + $classArray = explode('\\', get_class($this)); |
| 88 | + |
| 89 | + return count($classArray) > 2 ? "{$classArray[0]}_{$classArray[1]}" : ''; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Get module composer version |
| 94 | + * |
| 95 | + * @param $moduleName |
| 96 | + * @return \Magento\Framework\Phrase|string|void |
| 97 | + */ |
| 98 | + public function getComposerVersion($moduleName) |
| 99 | + { |
| 100 | + $path = $this->componentRegistrar->getPath( |
| 101 | + \Magento\Framework\Component\ComponentRegistrar::MODULE, |
| 102 | + $moduleName |
| 103 | + ); |
| 104 | + |
| 105 | + try { |
| 106 | + $directoryRead = $this->readFactory->create($path); |
| 107 | + $composerJsonData = $directoryRead->readFile('composer.json'); |
| 108 | + |
| 109 | + if ($composerJsonData) { |
| 110 | + $data = json_decode($composerJsonData); |
| 111 | + return !empty($data->version) ? $data->version : __('Unknown'); |
| 112 | + } |
| 113 | + } catch (\Exception $e) { |
| 114 | + // |
| 115 | + } |
| 116 | + |
| 117 | + return 'Unknown'; |
| 118 | + } |
| 119 | +} |
0 commit comments