How can I bootstrap Magento 2 in a test.php script?
Based on @Flyingmana's answer I did a little digging and come up with a solution. It seams to work for me.
First my solution, then some explanations.
I've created a file called test.php
in the root of my magento instance.
<?php
require __DIR__ . '/app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
/** @var \Magento\Framework\App\Http $app */
$app = $bootstrap->createApplication('TestApp');
$bootstrap->run($app);
Then I created a file called TestApp.php
in the same place with this content.
<?php
class TestApp
extends \Magento\Framework\App\Http
implements \Magento\Framework\AppInterface {
public function launch()
{
//dirty code goes here.
//the example below just prints a class name
echo get_class($this->_objectManager->create('\Magento\Catalog\Model\Category'));
//the method must end with this line
return $this->_response;
}
public function catchException(\Magento\Framework\App\Bootstrap $bootstrap, \Exception $exception)
{
return false;
}
}
Now I can just call test.php
in the browser and everything that is placed in TestApp::launch() will be executed.
Now, why this works:
The method createApplication
from the bootstrap class is the most important part. It creates an instance of an application class. The method createApplication
expects an implementation of the \Magento\Framework\AppInterface
that contains 2 methods.
So I created my own class in TestApp
that implements that interface. I made the method catchException
return false
always because I don't want my app to handle exceptions. In case something is wrong, just print it on the screen.
Then I implemented the method launch
. this one is called by \Magento\Framework\App\Bootstrap::run
. This run
method does almost the same thing no matter what the application passed as a parameter is.
The only thing that depends on the application is this line:
$response = $application->launch();
This means that calling \Magento\Framework\App\Bootstrap::run
will init the Magento env (maybe do some other crazy stuff...I haven't checked everything yet) then calls the launch
method from the application.
That's why you need to put all your dirty code inside that method.
Then the \Magento\Framework\App\Bootstrap::run
calls $response->sendResponse();
where $response
is what the launch
method returns.
That's why return $this->_response;
is needed. It just returns an empty response.
I made my app class extend \Magento\Framework\App\Http
so I will already have request and response parameters (and others), but you can make your class extend nothing. Then you need to copy the constructor from the \Magento\Framework\App\Http
class. Maybe add more parameters in the constructor if you need it.
For quick/short/dirty tests, I used something like this:
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$state = $obj->get(Magento\Framework\App\State::class);
$state->setAreaCode('frontend');
$quote = $obj->get(Magento\Checkout\Model\Session::class)->getQuote()->load(1);
print_r($quote->getOrigData());
Based on @Marius's answer I came up with this.
It works via both the command line as well as the browser, which I find useful.
Here's a sample script to programmatically delete category.
scripts/abstract.php
<?php
use \Magento\Framework\AppInterface as AppInterface;
use \Magento\Framework\App\Http as Http;
use Magento\Framework\ObjectManager\ConfigLoaderInterface;
use Magento\Framework\App\Request\Http as RequestHttp;
use Magento\Framework\App\Response\Http as ResponseHttp;
use Magento\Framework\Event;
use Magento\Framework\Filesystem;
use Magento\Framework\App\AreaList as AreaList;
use Magento\Framework\App\State as State;
abstract class AbstractApp implements AppInterface
{
public function __construct(
\Magento\Framework\ObjectManagerInterface $objectManager,
Event\Manager $eventManager,
AreaList $areaList,
RequestHttp $request,
ResponseHttp $response,
ConfigLoaderInterface $configLoader,
State $state,
Filesystem $filesystem,
\Magento\Framework\Registry $registry
) {
$this->_objectManager = $objectManager;
$this->_eventManager = $eventManager;
$this->_areaList = $areaList;
$this->_request = $request;
$this->_response = $response;
$this->_configLoader = $configLoader;
$this->_state = $state;
$this->_filesystem = $filesystem;
$this->registry = $registry;
}
public function launch()
{
$this->run();
return $this->_response;
}
abstract public function run();
public function catchException(\Magento\Framework\App\Bootstrap $bootstrap, \Exception $exception)
{
return false;
}
}
scripts/delete-category.php
<?php
require dirname(__FILE__) . '/../app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
require dirname(__FILE__) . '/abstract.php';
class CreateCategoriesApp extends AbstractApp
{
public function run()
{
$this->_objectManager->get('Magento\Framework\Registry')
->register('isSecureArea', true);
$category = $this->_objectManager->create('\Magento\Catalog\Model\Category');
$category = $category->load(343);
$category->delete();
}
}
/** @var \Magento\Framework\App\Http $app */
$app = $bootstrap->createApplication('CreateCategoriesApp');
$bootstrap->run($app);
Then I just run it like php scripts/delete-category.php