Skip to content

Commit

Permalink
V1.5
Browse files Browse the repository at this point in the history
  • Loading branch information
mauricio-tonny committed Aug 10, 2022
1 parent 21e6008 commit 8d3d107
Show file tree
Hide file tree
Showing 13 changed files with 474 additions and 50 deletions.
12 changes: 6 additions & 6 deletions Block/Adminhtml/System/Config/Documentation.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ public function render(\Magento\Framework\Data\Form\Element\AbstractElement $ele

return '<p style="font-size: 18px; font-weight: 600; margin-top: 15px;">Sobre o módulo</p>
<br>
<p>Versão: 1.1</p>
<p>Versão: 1.5</p>
<br>
<p>Este módulo tem como objetivo centralizar todos os módulos da BUZZ.</p>
<br>
<div style="padding: 20px; border: solid 1px #000; background: #eee;">
<p style="font-size: 16px;">Esperado para as próximas versões do módulo:</p>
<br>
<p> - Listar módulos instalados da BUZZ,</p>
<p> - Exibir a versão de cada módulo instalado da Buzz,</p>
<p> - Exibir a pasta onde o Magento está instalado,</p>
<p> - Criar menu customizado com logo (BUZZ),</p>
<p> - Exibir o modo que a loja se encontra (desenvolvimento/produção),</p>
<p> - Listar módulos instalados da BUZZ;</p>
<p> - Campo para inserção de CSS customizado (Magento Backend);</p>
<p> - BUG para versão 2.4.3+ Precisa do <strong>laminas/laminas-serializer</strong>;</p>
<br>
</div>
<br>
Expand Down
238 changes: 238 additions & 0 deletions Block/Adminhtml/SystemInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
<?php

/**
* @package Buzz_Base
* @company Buzz
* @author github.com/mauricio-tonny
* @copyright Copyright (c)
*/

namespace Buzz\Base\Block\Adminhtml;

use Buzz\Base\Model\ModuleInfoProvider;
use Magento\Backend\Block\Context;
use Magento\Backend\Model\Auth\Session;
use Magento\Config\Block\System\Config\Form\Field;
use Magento\Config\Block\System\Config\Form\Fieldset;
use Magento\Cron\Model\ResourceModel\Schedule\CollectionFactory;
use Magento\Framework\App\DeploymentConfig\Reader;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\App\ProductMetadataInterface;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\App\State;
use Magento\Framework\Data\Form\Element\AbstractElement;
use Magento\Framework\View\Helper\Js;

class SystemInfo extends Fieldset
{
/**
* @var CollectionFactory
*/
private $cronFactory;

/**
* @var DirectoryList
*/
private $directoryList;

/**
* @var ResourceConnection
*/
private $resourceConnection;

/**
* @var ProductMetadataInterface
*/
private $productMetadata;

/**
* @var Reader
*/
private $reader;

/**
* @var Field|null
*/
protected $fieldRenderer;

/**
* @var ModuleInfoProvider
*/
private $moduleInfoProvider;

public function __construct(
Context $context,
Session $authSession,
Js $jsHelper,
CollectionFactory $cronFactory,
DirectoryList $directoryList,
Reader $reader,
ResourceConnection $resourceConnection,
ProductMetadataInterface $productMetadata,
ModuleInfoProvider $moduleInfoProvider,
array $data = []
) {
parent::__construct($context, $authSession, $jsHelper, $data);
$this->cronFactory = $cronFactory;
$this->directoryList = $directoryList;
$this->resourceConnection = $resourceConnection;
$this->productMetadata = $productMetadata;
$this->reader = $reader;
$this->moduleInfoProvider = $moduleInfoProvider;
}

/**
* Render fieldset html
*
* @param AbstractElement $element
* @return string
*/
public function render(AbstractElement $element)
{
$html = $this->_getHeaderHtml($element);

$html .= $this->getMagentoMode($element);
$html .= $this->getMagentoPathInfo($element);
$html .= $this->getOwnerInfo($element);
$html .= $this->getSystemTime($element);
$html .= $this->getCronInfo($element);

$html .= $this->_getFooterHtml($element);

return $html;
}

/**
* @return \Magento\Framework\View\Element\BlockInterface
*/
private function getFieldRenderer()
{
if (empty($this->fieldRenderer)) {
$this->fieldRenderer = $this->_layout->createBlock(
Field::class
);
}

return $this->fieldRenderer;
}

/**
* @param AbstractElement $fieldset
*
* @return string
*/
private function getMagentoMode($fieldset)
{
$label = __('Magento Mode:');

$env = $this->reader->load();
$mode = isset($env[State::PARAM_MODE]) ? $env[State::PARAM_MODE] : '';

return $this->getFieldHtml($fieldset, 'magento_mode', $label, ucfirst($mode));
}

/**
* @param AbstractElement $fieldset
*
* @return string
*/
private function getMagentoPathInfo($fieldset)
{
$label = __('Magento Folder:');
$path = $this->directoryList->getRoot();

return $this->getFieldHtml($fieldset, 'magento_path', $label, $path);
}

/**
* @param AbstractElement $fieldset
*
* @return string
*/
private function getOwnerInfo($fieldset)
{
$serverUser = __('Unknown');
if (function_exists('get_current_user')) {
$serverUser = get_current_user();
}

return $this->getFieldHtml(
$fieldset,
'magento_user',
__('Magento User:'),
$serverUser
);
}

/**
* @param AbstractElement $fieldset
*
* @return string
*/
private function getSystemTime($fieldset)
{
if (version_compare($this->productMetadata->getVersion(), '2.2', '>=')) {
$time = $this->resourceConnection->getConnection()->fetchOne('select now()');
} else {
$time = $this->_localeDate->date()->format('H:i:s');
}
return $this->getFieldHtml($fieldset, 'mysql_current_date_time', __('Server Time:'), $time);
}

/**
* @param AbstractElement $fieldset
*
* @return string
*/
private function getCronInfo($fieldset)
{
$crontabCollection = $this->cronFactory->create();
$crontabCollection->setOrder('schedule_id')->setPageSize(5);

if ($crontabCollection->count() === 0) {
$value = '<div class="red">';
$value .= __('No cron jobs found') . '</div>';
if (!$this->moduleInfoProvider->isOriginMarketplace()) {
$value .=
'<a target="_blank"
href="https://github.com/Buzz-Dev-Web/Tutoriais/tree/main/Magento_2/82%20-%20O%20que%20%C3%A9%20Cron">' .
__('What is CRON?') .
'</a>';
}
} else {
$value = '<table>';
foreach ($crontabCollection as $crontabRow) {
$value .=
'<tr>' .
'<td>' . $crontabRow['job_code'] . '</td>' .
'<td>' . $crontabRow['status'] . '</td>' .
'<td>' . $crontabRow['created_at'] . '</td>' .
'</tr>';
}
$value .= '</table>';
}

$label = __('Cron (Last 5)');

return $this->getFieldHtml($fieldset, 'cron_configuration', $label, $value);
}

/**
* @param AbstractElement $fieldset
* @param string $fieldName
* @param string $label
* @param string $value
*
* @return string
*/
protected function getFieldHtml($fieldset, $fieldName, $label = '', $value = '')
{
$field = $fieldset->addField($fieldName, 'label', [
'name' => 'dummy',
'label' => $label,
'after_element_html' => $value,
])->setRenderer($this->getFieldRenderer());

return $field->toHtml();
}
}
88 changes: 88 additions & 0 deletions Model/ModuleInfoProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

/**
* @package Buzz_Base
* @company Buzz
* @author github.com/mauricio-tonny
* @copyright Copyright (c)
*/

declare(strict_types=1);

namespace Buzz\Base\Model;

use Magento\Framework\Exception\FileSystemException;
use Magento\Framework\Filesystem\Driver\File;
use Magento\Framework\Module\Dir\Reader;

class ModuleInfoProvider
{
protected $moduleDataStorage = [];

/**
* @var Reader
*/
private $moduleReader;

/**
* @var File
*/
private $filesystem;

/**
* @var Serializer
*/
private $serializer;

public function __construct(
Reader $moduleReader,
File $filesystem,
Serializer $serializer
) {
$this->moduleReader = $moduleReader;
$this->filesystem = $filesystem;
$this->serializer = $serializer;
}

/**
* Read info about extension from composer json file
*
* @param string $moduleCode
*
* @return mixed
*/
public function getModuleInfo(string $moduleCode)
{
if (!isset($this->moduleDataStorage[$moduleCode])) {
$this->moduleDataStorage[$moduleCode] = [];

try {
$dir = $this->moduleReader->getModuleDir('', $moduleCode);
$file = $dir . '/composer.json';

$string = $this->filesystem->fileGetContents($file);
$this->moduleDataStorage[$moduleCode] = $this->serializer->unserialize($string);
} catch (FileSystemException $e) {
$this->moduleDataStorage[$moduleCode] = [];
}
}

return $this->moduleDataStorage[$moduleCode];
}

/**
* Check whether module was installed via Magento Marketplace
*
* @param string $moduleCode
*
* @return bool
*/
public function isOriginMarketplace(string $moduleCode = 'Buzz_Base'): bool
{
$moduleInfo = $this->getModuleInfo($moduleCode);
$origin = isset($moduleInfo['extra']['origin']) ? $moduleInfo['extra']['origin'] : null;

return 'marketplace' === $origin;
}

}
Loading

0 comments on commit 8d3d107

Please sign in to comment.