Skip to content

Commit e35c8f4

Browse files
committed
Display module composer version in system config and sort admin menu
1 parent 4f5faad commit e35c8f4

File tree

10 files changed

+193
-14
lines changed

10 files changed

+193
-14
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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\Block\Adminhtml\System\Config\Composer;
12+
13+
use Magento\Backend\Block\Template\Context;
14+
use Magento\Config\Block\System\Config\Form\Field;
15+
use Magento\Framework\Data\Form\Element\AbstractElement;
16+
use ManiyaTech\Core\Model\Module;
17+
18+
class Version extends Field
19+
{
20+
/**
21+
* @var Module
22+
*/
23+
private Module $module;
24+
25+
/**
26+
* Constructor.
27+
*
28+
* @param Context $context
29+
* @param Module $module
30+
* @param array $data
31+
*/
32+
public function __construct(
33+
Context $context,
34+
Module $module,
35+
array $data = []
36+
) {
37+
parent::__construct($context, $data);
38+
$this->module = $module;
39+
}
40+
41+
/**
42+
* Remove scope label and render the field as HTML.
43+
*
44+
* @param AbstractElement $element
45+
* @return string
46+
*/
47+
public function render(AbstractElement $element): string
48+
{
49+
// Hide scope labels for clarity
50+
$element->unsScope()
51+
->unsCanUseWebsiteValue()
52+
->unsCanUseDefaultValue();
53+
54+
return parent::render($element);
55+
}
56+
57+
/**
58+
* Return the installed version as value in the config field.
59+
*
60+
* @param AbstractElement $element
61+
* @return string
62+
*/
63+
protected function _getElementHtml(AbstractElement $element): string
64+
{
65+
// Try to extract module name from the field ID if prefixed
66+
$elementId = (string)$element->getOriginalData('id');
67+
$moduleName = str_starts_with($elementId, 'ManiyaTech_')
68+
? $elementId
69+
: $this->getModuleName();
70+
71+
return '<strong>'.'v' . $this->module->getInstalledVersion($moduleName).'</strong>';
72+
}
73+
}

Controller/Adminhtml/Image/Uploader.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
/**
33
* ManiyaTech
44
*
5-
* @author Milan Maniya
6-
* @package ManiyaTech_Core
5+
* @author Milan Maniya
6+
* @package ManiyaTech_Core
77
*/
88

99
namespace ManiyaTech\Core\Controller\Adminhtml\Image;

Helper/Data.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
/**
33
* ManiyaTech
44
*
5-
* @author Milan Maniya
6-
* @package ManiyaTech_Core
5+
* @author Milan Maniya
6+
* @package ManiyaTech_Core
77
*/
88

99
namespace ManiyaTech\Core\Helper;

Model/ImageUploader.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
/**
33
* ManiyaTech
44
*
5-
* @author Milan Maniya
6-
* @package ManiyaTech_Core
5+
* @author Milan Maniya
6+
* @package ManiyaTech_Core
77
*/
88

99
namespace ManiyaTech\Core\Model;

Model/Module.php

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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+
}

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "Magento 2 Core Extension by ManiyaTech",
44
"type": "magento2-module",
55
"license": "proprietary",
6-
"version": "1.0.2",
6+
"version": "1.0.3",
77
"authors": [
88
{
99
"name": "ManiyaTech",

etc/acl.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<resource id="Magento_Backend::stores">
2020
<resource id="Magento_Backend::stores_settings">
2121
<resource id="Magento_Config::config">
22-
<resource id="ManiyaTech_Core::config" title="ManiyaTech Extension Settings" translate="title" sortOrder="100"/>
22+
<resource id="ManiyaTech_Core::config" title="ManiyaTech Extension Settings" translate="title" sortOrder="10"/>
2323
</resource>
2424
</resource>
2525
</resource>

etc/module.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
*/
99
-->
1010

11-
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
12-
<module name="ManiyaTech_Core" setup_version="1.0.0" schema_version="1.0.0"/>
11+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
12+
<module name="ManiyaTech_Core" setup_version="1.0.0" schema_version="1.0.0" />
1313
</config>

registration.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
/**
33
* ManiyaTech
44
*
5-
* @author Milan Maniya
6-
* @package ManiyaTech_Core
5+
* @author Milan Maniya
6+
* @package ManiyaTech_Core
77
*/
88

99
\Magento\Framework\Component\ComponentRegistrar::register(

view/adminhtml/web/js/grid/columns/status.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/**
22
* ManiyaTech
33
*
4-
* @author Milan Maniya
5-
* @package ManiyaTech_Core
4+
* @author Milan Maniya
5+
* @package ManiyaTech_Core
66
*/
77

88
define(

0 commit comments

Comments
 (0)