Created
November 19, 2020 06:58
-
-
Save Lathanao/c1dfd296fa3386e91116ae9acf893a54 to your computer and use it in GitHub Desktop.
M2 Bootstrap Sample File
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* | |
* M2 Bootstrap Sample File | |
* | |
*/ | |
ini_set('display_errors', 1); | |
error_reporting(E_ALL); | |
require __DIR__ . '/../app/bootstrap.php'; | |
class TestApp extends \Magento\Framework\App\Http implements \Magento\Framework\AppInterface | |
{ | |
/** | |
* Callback function | |
* | |
* @var \Closure | |
*/ | |
static $callback; | |
/** | |
* @var string | |
*/ | |
protected $areaCode = 'frontend'; | |
/** | |
* Run M2 with $callback | |
* | |
* @param \Closure $callback | |
* @return void | |
*/ | |
static public function run(\Closure $callback) | |
{ | |
self::$callback = $callback; | |
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER); | |
$bootstrap->run($bootstrap->createApplication(__CLASS__)); | |
} | |
/** | |
* Launch | |
* | |
* @return | |
*/ | |
public function launch() | |
{ | |
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); | |
$objectManager->get('\Magento\Framework\App\State')->setAreaCode($this->areaCode); | |
$objectManager->configure( | |
$objectManager->get('\Magento\Framework\ObjectManager\ConfigLoaderInterface') | |
->load($this->areaCode) | |
); | |
call_user_func(self::$callback, $objectManager); | |
return $this->_response; | |
} | |
/** | |
* Catch an exception | |
* | |
* @return | |
*/ | |
public function catchException( | |
\Magento\Framework\App\Bootstrap $bootstrap, | |
\Exception $exception | |
): bool | |
{ | |
echo sprintf( | |
'<h1>%s</h1><' . 'pre>%s<' . '/pre>', $exception->getMessage(), $exception->getTraceAsString() | |
); | |
exit; | |
} | |
} | |
TestApp::run(function ($objectManager) { | |
try { | |
/** | |
* * | |
* Custom code here | |
* | |
*/ | |
} catch (\Exception $e) { | |
echo $e->getMessage() . PHP_EOL . $e->getTraceAsString() . PHP_EOL . PHP_EOL; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment