diff --git a/app/code/community/MM/Ignition/Controller/Router.php b/app/code/community/MM/Ignition/Controller/Router.php new file mode 100644 index 00000000000..c411f92bb41 --- /dev/null +++ b/app/code/community/MM/Ignition/Controller/Router.php @@ -0,0 +1,79 @@ +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; + } + + +} \ No newline at end of file diff --git a/app/code/community/MM/Ignition/Helper/Data.php b/app/code/community/MM/Ignition/Helper/Data.php new file mode 100644 index 00000000000..e28f51181dd --- /dev/null +++ b/app/code/community/MM/Ignition/Helper/Data.php @@ -0,0 +1,187 @@ +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); + } + +} \ No newline at end of file diff --git a/app/code/community/MM/Ignition/Model/Observer.php b/app/code/community/MM/Ignition/Model/Observer.php new file mode 100644 index 00000000000..edf0902d1d7 --- /dev/null +++ b/app/code/community/MM/Ignition/Model/Observer.php @@ -0,0 +1,113 @@ +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'); + } +} \ No newline at end of file diff --git a/app/code/community/MM/Ignition/Model/System/Config/Source/Editor.php b/app/code/community/MM/Ignition/Model/System/Config/Source/Editor.php new file mode 100644 index 00000000000..6fa828e0135 --- /dev/null +++ b/app/code/community/MM/Ignition/Model/System/Config/Source/Editor.php @@ -0,0 +1,32 @@ +toArray()['editorOptions']; + return $editorOptions; + } + + /** + * Get available editor options + * + * @return array + */ + public function toOptionArray() + { + foreach ($this->getOptions() as $key => $value) { + $options[] = [ + 'value' => $key, + 'label' => $value['label'] + ]; + } + return $options; + } +} \ No newline at end of file diff --git a/app/code/community/MM/Ignition/Model/System/Config/Source/Theme.php b/app/code/community/MM/Ignition/Model/System/Config/Source/Theme.php new file mode 100644 index 00000000000..e346c7ba2f1 --- /dev/null +++ b/app/code/community/MM/Ignition/Model/System/Config/Source/Theme.php @@ -0,0 +1,29 @@ + $option, + 'label' => ucfirst($option) + ]; + } + return $options; + } +} \ No newline at end of file diff --git a/app/code/community/MM/Ignition/controllers/ConfigController.php b/app/code/community/MM/Ignition/controllers/ConfigController.php new file mode 100644 index 00000000000..9a54cd6381d --- /dev/null +++ b/app/code/community/MM/Ignition/controllers/ConfigController.php @@ -0,0 +1,29 @@ +updateConfig($this->getRequest()->getParams()); + if (!$result) { + $this->getResponse()->setHttpResponseCode(400); + } + + $this->getResponse()->setHeader('Content-type', 'application/json'); + $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result)); + } + + private function updateConfig($config) + { + /** @var MM_Ignition_Helper_Data $_helper */ + $_helper = Mage::helper('mm_ignition'); + if (isset($config['theme'])) { + $_helper->setTheme($config['theme']); + } + if (isset($config['editor'])) { + $_helper->setEditor($config['editor']); + } + return true; + } +} \ No newline at end of file diff --git a/app/code/community/MM/Ignition/etc/config.xml b/app/code/community/MM/Ignition/etc/config.xml new file mode 100644 index 00000000000..989a8d55aef --- /dev/null +++ b/app/code/community/MM/Ignition/etc/config.xml @@ -0,0 +1,76 @@ + + + + + 1.4.1 + + + + + + MM_Ignition_Helper + + + + + MM_Ignition_Model + + + + + + + MM_Ignition_Model_Observer + handleIgnitionRegister + + + + + + + MM_Ignition_Model_Observer + handleIgnitionException + + + + + + + MM_Ignition_Model_Observer + handleIgnitionException + + + + + + + MM_Ignition_Controller_Router + initControllerRouters + + + + + + + + + + + ignition_router + + MM_Ignition + _ignition + + + + + + + + 1 + clipboard + auto + + + + \ No newline at end of file diff --git a/app/code/community/MM/Ignition/etc/system.xml b/app/code/community/MM/Ignition/etc/system.xml new file mode 100644 index 00000000000..5babfd5aff3 --- /dev/null +++ b/app/code/community/MM/Ignition/etc/system.xml @@ -0,0 +1,113 @@ + + + + + + + 20 + 1 + 1 + 1 + + + + select + adminhtml/system_config_source_yesno + 10 + 1 + 1 + 1 + + + + Default editor for code editing + select + mm_ignition/system_config_source_editor + 20 + 1 + 1 + 1 + + + + Default theme for code editing + select + mm_ignition/system_config_source_theme + 30 + 1 + 1 + 1 + + + + Enable to allow custom settings stored in session otherwise default settings will be updated + select + adminhtml/system_config_source_yesno + 40 + 1 + 1 + 1 + + + + Enable AI Generated Solution by OpenAI + select + adminhtml/system_config_source_yesno + 50 + 1 + 1 + 1 + + + + API key for OpenAI + password + 60 + 1 + 1 + 1 + + 1 + + + + + Enable Flare Error Tracking + select + adminhtml/system_config_source_yesno + 50 + 1 + 1 + 1 + + + + API key for Flare provided when you created a new project + password + 70 + 1 + 1 + 1 + + 1 + + + + + Enable to anonymize IP addresses sent to Flare + select + adminhtml/system_config_source_yesno + 80 + 1 + 1 + 1 + + 1 + + + + + + + + diff --git a/app/etc/modules/MM_Ignition.xml b/app/etc/modules/MM_Ignition.xml new file mode 100644 index 00000000000..ab7635de103 --- /dev/null +++ b/app/etc/modules/MM_Ignition.xml @@ -0,0 +1,12 @@ + + + + + true + community + + + + + + \ No newline at end of file