Skip to content

Commit

Permalink
Adicionado modulo MM_Ignition
Browse files Browse the repository at this point in the history
  • Loading branch information
eneiasramos committed Nov 11, 2024
1 parent 51f0d01 commit bf8dc2a
Show file tree
Hide file tree
Showing 9 changed files with 670 additions and 0 deletions.
79 changes: 79 additions & 0 deletions app/code/community/MM/Ignition/Controller/Router.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

class MM_Ignition_Controller_Router extends Mage_Core_Controller_Varien_Router_Standard {

const IGNITION_CONFIG_PATH = '_ignition/update-config';

/**
* Initialize Controller Router
*
* @param Varien_Event_Observer $observer
*/
public function initControllerRouters(Varien_Event_Observer $observer)
{
$front = $observer->getEvent()->getFront();
$front->addRouter('_ignition', $this);
}

/**
* Match the request in the form "_ignition/update-config", skip store code prefix
*
* @param Mage_Core_Controller_Request_Http $request
* @inheritDoc
*/
public function match(Zend_Controller_Request_Http $request)
{
/** @var MM_Ignition_Helper_Data $_helper */
$_helper = Mage::helper('mm_ignition');
if (!$_helper->shouldPrintIgnition()) {
return false;
}

$requestPathInfo = trim($request->getPathInfo(), '/');
if ($requestPathInfo == self::IGNITION_CONFIG_PATH && $request->isPost()) {
$module = 'mm_ignition';
$controller = 'config';
$action = 'update';
$realModule = 'MM_Ignition';

$request->setModuleName($module);
$request->setControllerName($controller);
$request->setActionName($action);
$request->setControllerModule($realModule);

// set params from JSON body
$rawBody = $request->getRawBody();
$jsonData = json_decode($rawBody, true);
if ($jsonData == null) {
return false;
}
$request->setParams($jsonData);

$controllerClassName = $this->_validateControllerClassName($realModule, $controller);
if (!$controllerClassName) {
return false;
}

// instantiate controller class
$controllerInstance = Mage::getControllerInstance($controllerClassName, $request, $this->getFront()->getResponse());

if (!$this->_validateControllerInstance($controllerInstance)) {
return false;
}

if (!$controllerInstance->hasAction($action)) {
return false;
}

// dispatch action
$request->setDispatched(true);
$controllerInstance->dispatch($action);

return true;
}

return false;
}


}
187 changes: 187 additions & 0 deletions app/code/community/MM/Ignition/Helper/Data.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
<?php
class MM_Ignition_Helper_Data extends Mage_Core_Helper_Abstract
{
const XML_PATH_ENABLED = 'dev/mm_ignition/enabled';
const XML_PATH_THEME = 'dev/mm_ignition/theme';
const XML_PATH_EDITOR = 'dev/mm_ignition/editor';
const XML_PATH_OVERRIDE_CONFIG = 'dev/mm_ignition/override_config';
const XML_PATH_OPENAI_ENABLED = 'dev/mm_ignition/enable_openai';
const XML_PATH_OPENAI_KEY = 'dev/mm_ignition/openai_api_key';
const XML_PATH_FLARE_ENABLED = 'dev/mm_ignition/enable_flare';
const XML_PATH_FLARE_API_KEY = 'dev/mm_ignition/flare_api_key';
const XML_PATH_FLARE_ANONYMIZE_IP = 'dev/mm_ignition/flare_anonymize_ip';

/**
* Allowed keys for settings
* @var array
*/
const SETTINGS_ALLOWED_KEYS = ['theme', 'editor', 'hide_solutions'];

/**
* Check if Ignition should be printed
* only if Ignition is enabled and developer mode is on
* @return bool
*/
public function shouldPrintIgnition()
{
if (!$this->isEnabled()) {
return false;
}
if (!Mage::getIsDeveloperMode()) {
return false;
}

return true;
}

/**
* Check if Ignition is enabled
* @return bool
*/
public function isEnabled()
{
return Mage::getStoreConfigFlag(self::XML_PATH_ENABLED);
}

/**
* Get theme preference
* @return string
*/
public function getTheme()
{
return $this->getSessionConfig('theme') ?: Mage::getStoreConfig(self::XML_PATH_THEME);
}

/**
* Set theme preference
* @return string
*/
public function setTheme($theme)
{
if (!in_array($theme, MM_Ignition_Model_System_Config_Source_Theme::OPTIONS)) {
return false;
}
$this->setSessionConfig('theme', $theme);
if ($this->shouldUseSessionConfig()) {
return true;
}
Mage::getConfig()->saveConfig(self::XML_PATH_THEME, $theme);
}

/**
* Get editor preference
* @return string
*/
public function getEditor()
{
return $this->getSessionConfig('editor') ?: Mage::getStoreConfig(self::XML_PATH_EDITOR);
}

/**
* Set editor preference
* @return string
*/
public function setEditor($editor)
{
$editorOptions = array_keys((MM_Ignition_Model_System_Config_Source_Editor::getOptions()));
if (!in_array($editor, $editorOptions)) {
return false;
}
$this->setSessionConfig('editor', $editor);
if ($this->shouldUseSessionConfig()) {
return true;
}
Mage::getConfig()->saveConfig(self::XML_PATH_EDITOR, $editor);
}

/**
* Check if config should be read from session
* @return bool
*/
public function shouldUseSessionConfig()
{
return Mage::getStoreConfigFlag(self::XML_PATH_OVERRIDE_CONFIG);
}

/**
* Read config from session
* @param $key string config key like (theme|editor)
* @return mixed
*/
public function getSessionConfig($key)
{
if (!$this->shouldUseSessionConfig()) {
return false;
}
$ignitionConfig = Mage::getSingleton('core/session')->getIgnitionConfig();
if (is_array($ignitionConfig) && array_key_exists($key, $ignitionConfig)) {
return $ignitionConfig[$key];
}
return false;
}

/**
* Write config from session
* @param $key string config key like (theme|editor)
* @param $value string config value
* @return void
*/
public function setSessionConfig($key, $value)
{
if (!$this->shouldUseSessionConfig()) {
return false;
}
if (!in_array($key, self::SETTINGS_ALLOWED_KEYS)) {
return false;
}
$ignitionConfig = Mage::getSingleton('core/session')->getIgnitionConfig() ?: [];
$ignitionConfig[$key] = $value;
Mage::getSingleton('core/session')->setIgnitionConfig($ignitionConfig);
}

/**
* Check if OpenAI is enabled
* @return bool
*/
public function isOpenAiEnabled()
{
return Mage::getStoreConfigFlag(self::XML_PATH_OPENAI_ENABLED);
}

/**
* Get OpenAI key
* @return string
*/
public function getOpenAiKey()
{
return Mage::getStoreConfig(self::XML_PATH_OPENAI_KEY);
}

/**
* Check if Flare is enabled
* @return bool
*/
public function isFlareEnabled()
{
return Mage::getStoreConfigFlag(self::XML_PATH_FLARE_ENABLED);
}

/**
* Check if Flare should anonymize IP
* @return bool
*/
public function shouldAnonymizeIp()
{
return Mage::getStoreConfigFlag(self::XML_PATH_FLARE_ANONYMIZE_IP);
}

/**
* Get Flare API key
* @return string
*/
public function getFlareApiKey()
{
return Mage::getStoreConfig(self::XML_PATH_FLARE_API_KEY);
}

}
113 changes: 113 additions & 0 deletions app/code/community/MM/Ignition/Model/Observer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

use Spatie\Ignition\Config\IgnitionConfig;
use Spatie\Ignition\Ignition;
use Spatie\Ignition\Solutions\OpenAi\OpenAiSolutionProvider;
use Spatie\FlareClient\Flare;

class MM_Ignition_Model_Observer extends Mage_Core_Model_Observer
{
/**
* Register Ignition error handler.
*
* @param Varien_Event_Observer $observer
* @return void
*/
public function handleIgnitionRegister(Varien_Event_Observer $observer)
{
if (!$this->getHelper()->shouldPrintIgnition() && !$this->getHelper()->isFlareEnabled()) {
return;
}

Mage::app()->setErrorHandler(NULL);
$this->getIgnitionInstance()->register(error_reporting());
}

/**
* Handle Exception with Ignition.
*
* @param Varien_Event_Observer $observer
* @return void
*/
public function handleIgnitionException(Varien_Event_Observer $observer)
{
if (!$this->getHelper()->shouldPrintIgnition() && !$this->getHelper()->isFlareEnabled()) {
return;
}

$e = $observer->getEvent()->getException();
$this->getIgnitionInstance()->handleException($e);

if ($this->getHelper()->shouldPrintIgnition()) {
die();
}
}

/**
* Get the Ignition instance.
*
* @return \Spatie\Ignition\Ignition
*/
protected function getIgnitionInstance()
{
$_ignition = Ignition::make()
->runningInProductionEnvironment(!Mage::getIsDeveloperMode())
->setConfig($this->getIgnitionConfig())
->applicationPath(Mage::getBaseDir());

if ($this->getHelper()->isOpenAiEnabled() && !empty($this->getHelper()->getOpenAiKey())) {
$openAiKey = $this->getHelper()->getOpenAiKey();
$aiSolutionProvider = new OpenAiSolutionProvider($openAiKey);
$aiSolutionProvider->applicationType('OpenMage a fork of Magetno 1.9 (Generic Developer Documentation: https://devdocs-openmage.org/ NO mention Magento 1.x)');

$_ignition->addSolutionProviders([
$aiSolutionProvider,
]);
}

if ($this->getHelper()->isFlareEnabled() && !empty($this->getHelper()->getFlareApiKey())) {
$_ignition->sendToFlare($this->getHelper()->getFlareApiKey());
if ($this->getHelper()->shouldAnonymizeIp()) {
$_ignition->configureFlare(function(Flare $flare) {
$flare->anonymizeIp();
});
}
}

return $_ignition;
}

/**
* Get the Ignition config.
*
* @return \Spatie\Ignition\Config\IgnitionConfig
*/
protected function getIgnitionConfig()
{
return (new IgnitionConfig())
->merge($this->getSystemConfig());
}

/**
* Get settings from system config.
*
* @return array
*/
protected function getSystemConfig()
{
return [
'editor' => $this->getHelper()->getEditor() ?? 'vscode',
'theme' => $this->getHelper()->getTheme() ?? 'auto',
];
}

/**
* Get helper data.
*
* @return MM_Ignition_Helper_Data
*/
protected function getHelper()
{
return Mage::helper('mm_ignition');
}
}
Loading

0 comments on commit bf8dc2a

Please # to comment.