Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions Block/Adminhtml/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
*/

declare(strict_types=1);

namespace Magefan\Cli\Block\Adminhtml;

use Magento\Framework\View\Element\Template;
use Magento\Framework\Console\CommandListInterface;
use Magento\Framework\View\Helper\SecureHtmlRenderer;
use Magento\Framework\Data\Form\FormKey;
use Magefan\Cli\Model\Config;
use Magento\Framework\AuthorizationInterface;

Expand All @@ -28,27 +32,94 @@ class Form extends \Magento\Framework\View\Element\Template
*/
private $authorization;

/**
* @var \Magento\Framework\App\ResourceConnection
*/
private $resource;

/**
* @var SecureHtmlRenderer
*/
private $secureHtmlRenderer;

/**
* @var FormKey
*/
private $formKey;

/**
* Form constructor.
* @param Template\Context $context
* @param CommandListInterface $commandList
* @param Config $config
* @param AuthorizationInterface $authorization
* @param \Magento\Framework\App\ResourceConnection $resource
* @param SecureHtmlRenderer|null $secureHtmlRenderer
* @param FormKey|null $formKey
* @param array $data
*/
public function __construct(
Template\Context $context,
CommandListInterface $commandList,
Config $config,
AuthorizationInterface $authorization,
\Magento\Framework\App\ResourceConnection $resource,
?SecureHtmlRenderer $secureHtmlRenderer = null,
?FormKey $formKey = null,
array $data = []
) {
$this->commandList = $commandList;
$this->config = $config;
$this->authorization = $authorization;
$this->resource = $resource;
$this->secureHtmlRenderer = $secureHtmlRenderer ?? \Magento\Framework\App\ObjectManager::getInstance()->get(SecureHtmlRenderer::class);
$this->formKey = $formKey ?? \Magento\Framework\App\ObjectManager::getInstance()->get(FormKey::class);
parent::__construct($context, $data);
}

/**
* Get form key for AJAX POST (required by Magento admin).
* @return string
*/
public function getFormKeyValue()
{
return $this->formKey->getFormKey();
}

/**
* Render script tag securely (works without Magefan_Community).
* @param string $script
* @return string
*/
public function renderScript($script)
{
return $this->secureHtmlRenderer->renderTag('script', [], $script, false);
}

/**
* Return recent commands history (most recent first)
* @param int $limit
* @return array
*/
public function getCommandHistory($limit = 100)
{
try {
$connection = $this->resource->getConnection();
$table = $this->resource->getTableName('magefan_cli_log');
$select = $connection->select()->from($table, ['command'])->order('executed_at DESC')->limit((int)$limit);
$rows = $connection->fetchAll($select);
$commands = [];
foreach ($rows as $r) {
if (!empty($r['command'])) {
$commands[] = $r['command'];
}
}
return $commands;
} catch (\Exception $e) {
return [];
}
}

/**
* Preparing global layout
*
Expand Down
38 changes: 38 additions & 0 deletions Block/Adminhtml/History.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* Copyright © Magefan (support@magefan.com). All rights reserved.
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
*/

declare(strict_types=1);

namespace Magefan\Cli\Block\Adminhtml;

use Magento\Backend\Block\Template\Context;
use Magento\Framework\App\ResourceConnection;
use Magento\Backend\Block\Template;

class History extends Template
{
/** @var ResourceConnection */
private $resource;

public function __construct(Context $context, ResourceConnection $resource, array $data = [])
{
$this->resource = $resource;
parent::__construct($context, $data);
}

/**
* Get recent logs
* @param int $limit
* @return array
*/
public function getLogs($limit = 100)
{
$connection = $this->resource->getConnection();
$table = $this->resource->getTableName('magefan_cli_log');
$select = $connection->select()->from($table)->order('executed_at DESC')->limit((int)$limit);
return $connection->fetchAll($select);
}
}
25 changes: 11 additions & 14 deletions Block/Adminhtml/System/Config/Form/Info.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,21 @@

namespace Magefan\Cli\Block\Adminhtml\System\Config\Form;

class Info extends \Magefan\Community\Block\Adminhtml\System\Config\Form\Info
{
/**
* Return extension url
* @return string
*/
protected function getModuleUrl():string
{
return 'https://mage' .
'fan.com/magento2-extensions?utm_source=m2admin_cli_config&utm_medium=link&utm_campaign=regular';
}
use Magento\Config\Block\System\Config\Form\Field;
use Magento\Framework\Data\Form\Element\AbstractElement;

class Info extends Field
{
/**
* Return extension title
* @param AbstractElement $element
* @return string
*/
protected function getModuleTitle():string
protected function _getElementHtml(AbstractElement $element)
{
return 'Command Line Interface';
return '<div style="padding: 10px; background: #f5f5f5; border: 1px solid #ddd; border-radius: 3px;">' .
'<strong>Magefan Command Line Interface</strong><br/>' .
'Version: 1.0.0<br/>' .
'<a href="https://magefan.com/magento2-extensions" target="_blank">Visit Magefan.com →</a>' .
'</div>';
}
}
35 changes: 35 additions & 0 deletions Controller/Adminhtml/History/Index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* Copyright © Magefan (support@magefan.com). All rights reserved.
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
*/

declare(strict_types=1);

namespace Magefan\Cli\Controller\Adminhtml\History;

use Magento\Backend\App\Action\Context;
use Magento\Backend\App\Action;
use Magento\Framework\View\Result\PageFactory;

class Index extends Action
{
const ADMIN_RESOURCE = 'Magefan_Cli::elements';

/** @var PageFactory */
private $resultPageFactory;

public function __construct(Context $context, PageFactory $resultPageFactory)
{
$this->resultPageFactory = $resultPageFactory;
parent::__construct($context);
}

public function execute()
{
$resultPage = $this->resultPageFactory->create();
$resultPage->setActiveMenu('Magefan_Cli::history');
$resultPage->getConfig()->getTitle()->prepend(__('Command Line History'));
return $resultPage;
}
}
28 changes: 28 additions & 0 deletions Controller/Adminhtml/Index/Cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
*/

declare(strict_types=1);

namespace Magefan\Cli\Controller\Adminhtml\Index;

use Magefan\Cli\Model\Config;
use Magento\Framework\Data\Form\FormKey;
use Magento\Framework\App\ResourceConnection;

class Cli extends \Magento\Backend\App\Action
{
Expand Down Expand Up @@ -45,6 +48,11 @@ class Cli extends \Magento\Backend\App\Action
*/
private $formKey;

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

/**
* Constructor
*
Expand All @@ -70,6 +78,7 @@ public function __construct(
$this->authSession = $authSession;
$this->config = $config;
$this->formKey = $formKey;
$this->resource = $context->getObjectManager()->get(ResourceConnection::class);
parent::__construct($context);
}

Expand Down Expand Up @@ -132,6 +141,25 @@ public function execute()
if (!$message) {
$message = __('Command not found or error occurred.') . PHP_EOL;
}
// persist log to DB
try {
$connection = $this->resource->getConnection();
$tableName = $this->resource->getTableName('magefan_cli_log');
$userId = null;
try {
$user = $this->authSession->getUser();
$userId = $user ? (int)$user->getId() : null;
} catch (\Exception $e) {
$userId = null;
}

$connection->insertMultiple($tableName, [
['command' => $command, 'result' => $message, 'user_id' => $userId, 'executed_at' => (new \DateTime())->format('Y-m-d H:i:s')]
]);
} catch (\Exception $e) {
// swallow DB errors to not break execution
}

unlink($logFile);
} catch (\Exception $e) {
$message = $e->getMessage() . PHP_EOL;
Expand Down
2 changes: 2 additions & 0 deletions Controller/Adminhtml/Index/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
*/

declare(strict_types=1);

namespace Magefan\Cli\Controller\Adminhtml\Index;

use Magefan\Cli\Model\Config;
Expand Down
36 changes: 36 additions & 0 deletions Controller/Adminhtml/MfcliHistory/Index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* Copyright © Magefan (support@magefan.com). All rights reserved.
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
* Command Line History page via default admin route (admin/admin/mfcli_history/index).
*/

declare(strict_types=1);

namespace Magefan\Cli\Controller\Adminhtml\MfcliHistory;

use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;

class Index extends Action
{
const ADMIN_RESOURCE = 'Magefan_Cli::elements';

/** @var PageFactory */
private $resultPageFactory;

public function __construct(Context $context, PageFactory $resultPageFactory)
{
$this->resultPageFactory = $resultPageFactory;
parent::__construct($context);
}

public function execute()
{
$resultPage = $this->resultPageFactory->create();
$resultPage->setActiveMenu('Magefan_Cli::history');
$resultPage->getConfig()->getTitle()->prepend(__('Command Line History'));
return $resultPage;
}
}
Loading