-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInspector.php
More file actions
90 lines (77 loc) · 2.06 KB
/
Copy pathInspector.php
File metadata and controls
90 lines (77 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
declare(strict_types=1);
namespace OpenForgeProject\MageForge\Block;
use Magento\Developer\Helper\Data as DevHelper;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\State;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
/**
* Block for MageForge Inspector
*
* Only renders inspector assets when in developer mode, enabled in config, and from allowed IP
*/
class Inspector extends Template
{
private const XML_PATH_INSPECTOR_ENABLED = 'dev/mageforge_inspector/enabled';
public function __construct(
Context $context,
private readonly State $state,
private readonly ScopeConfigInterface $scopeConfig,
private readonly DevHelper $devHelper,
array $data = []
) {
parent::__construct($context, $data);
}
/**
* Check if inspector should be rendered
*
* @return bool
*/
public function shouldRender(): bool
{
// Check developer mode
if ($this->state->getMode() !== State::MODE_DEVELOPER) {
return false;
}
// Check if inspector is enabled in configuration
if (!$this->scopeConfig->isSetFlag(self::XML_PATH_INSPECTOR_ENABLED)) {
return false;
}
// Check if current IP is allowed
if (!$this->devHelper->isDevAllowed()) {
return false;
}
return true;
}
/**
* Get CSS file URL
*
* @return string
*/
public function getCssUrl(): string
{
return $this->getViewFileUrl('OpenForgeProject_MageForge::css/inspector.css');
}
/**
* Get JS file URL
*
* @return string
*/
public function getJsUrl(): string
{
return $this->getViewFileUrl('OpenForgeProject_MageForge::js/inspector.js');
}
/**
* Render block HTML
*
* @return string
*/
protected function _toHtml(): string
{
if (!$this->shouldRender()) {
return '';
}
return parent::_toHtml();
}
}