6

My Client wants me to write code to :

  1. programmatically upload a customized module and then
  2. enable the module(php bin/magento module:enable VENDORNAME_MODULENAME) and then
  3. upgrade the system(php bin/magento setup:upgrade).

I successfully did the first 2 steps, but unable to do the 3rd one. Can anyone share sample code to programmatically run setup upgrade in Magento 2?

 <?php use Magento\Framework\App\Bootstrap; include('../app/bootstrap.php'); $bootstrap = Bootstrap::create(BP, $_SERVER); $objectManager = $bootstrap->getObjectManager(); $modules = array('VENDORNAME_MODULENAME'); try{ // Code to enable the module $objectManager->create('Magento\Framework\Module')->setIsEnabled(true,$modules); // Code to setup upgrade(php bin/magento setup:upgrade) ....... }catch(Exception $e){ $msg = 'Error : '.$e->getMessage();die(); } 

2 Answers 2

3

To do so you need to use the Magento/Setup/Model/InstallerFactory class.

For example this is the code that runs when you call the CLI command:

 $keepGenerated = $input->getOption(self::INPUT_KEY_KEEP_GENERATED); $installer = $this->installerFactory->create(new ConsoleLogger($output)); $installer->updateModulesSequence($keepGenerated); $installer->installSchema(); $installer->installDataFixtures(); if (!$keepGenerated) { $output->writeln('<info>Please re-run Magento compile command</info>'); } 
1
  • Thanks Raphael for your response. It helped me to me diffing deep into the core found the final solution, which worked fine for me. Commented Aug 19, 2016 at 12:24
2
<?php use Magento\Framework\App\Bootstrap; include('../app/bootstrap.php'); $bootstrap = Bootstrap::create(BP, $_SERVER); $objectManager = $bootstrap->getObjectManager(); $modules = array('VENDORNAME_MODULENAME'); try{ /* Code to enable a module [ php bin/magento module:enable VENDORNAME_MODULENAME ] */ $moduleStatus = $objectManager->create('Magento\Framework\Module\Status')->setIsEnabled(true,$modules); /* Code to run setup upgrade [ php bin/magento setup:upgrade ] */ $installerFactory = $objectManager->create('Magento\Setup\Test\Unit\Console\Command\UpgradeCommandTest')->testExecute(); /* Code to clean cache [ php bin/magento:cache:clean ] */ try{ $_cacheTypeList = $objectManager->create('Magento\Framework\App\Cache\TypeListInterface'); $_cacheFrontendPool = $objectManager->create('Magento\Framework\App\Cache\Frontend\Pool'); $types = array('config','layout','block_html','collections','reflection','db_ddl','eav','config_integration','config_integration_api','full_page','translate','config_webservice'); foreach ($types as $type) { $_cacheTypeList->cleanType($type); } foreach ($_cacheFrontendPool as $cacheFrontend) { $cacheFrontend->getBackend()->clean(); } }catch(Exception $e){ echo $msg = 'Error during cache clean: '.$e->getMessage();die(); } }catch(Exception $e){ echo $msg = 'Error during module enabling : '.$e->getMessage();die(); } 
1
  • Thank You @IpsitaRout! This is what i am looking for.. +1. Happy Coding Commented Sep 8, 2017 at 4:22

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.