n98-magerun2 – Project Update 2015-01

Published by Christian Münch on

n98-magerun2 Updated

After some busy days and a intermediate update of n98-magerun V1 i ha time to give the code base a little bit love.
In the meantime i got help from EliasZ who ported a lot of n98-magerun commands to V2.

Newly ported commands

This command were ported in the last 30 days:

  • cache:clean
  • cache:enable
  • cache:disable
  • cache:flush
  • cache:list
  • config:get
  • config:set
  • config:delete
  • dev:symlinks
  • dev:template-hints-blocks
  • dev:template-hints
  • sys:maintenance
  • sys:setup:compare-versions
  • sys:setup:run
  • dev:module:list
  • dev:module:observer:list
  • script
  • shell
  • self-update

Feel free to test the command in your Magento 2 installation and send us pull requests/issues if
you find a problem.

Automatic dependecy injection

As we started one of the first things was to fetch the Magento 2 ObjectManager which is needed
to create instances of Magento 2 objects.
In n98-magerun we can use the object manager (inside a command) as service locator.

Example:

$storeManager = $this->getObjectManager->get(
    'Magento\Store\Model\StoreManagerInterface'
);

Now we improved the system a little bit and implemeted a new system for automatic dependecy injection.
It’s very simple and simiar to constructor injection in Magento 2 modules.
We introduced a new magic method inject. If this method is defined in a command class we automatic
parse the arguments and try to inject all objects.
Another side effect is that Magento is initialized and no manual “initMagento” call is needed anymore.

Example:

class AcmeCommand extends AbstractMagentoCommand
{
    /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $storeManager; // <-- define it

    protected function configure() { ... }

    /**
     * @param \Magento\Store\Model\StoreManager $storeManager
     */
    public function inject(
        \Magento\Store\Model\StoreManagerInterface $storeManager
    ) {
        $this->storeManager = $storeManager; // <-- bind it
    }

    /**
     * @param \Symfony\Component\Console\Input\InputInterface $input
     * @param \Symfony\Component\Console\Output\OutputInterface $output
     * @return int|void
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        foreach ($this->storeManager->getStores() as $store) { // <-- use it
           ...
        }
    }

}

Use the inject method if you need some Magento objects in your command. If the method is not available
the command behavior is like before.

We don’t removed the getObjcetManager method.

Statistics

As written in previous post we first try to port all commands from Version 2. The milestone progress has reached 30%.

Categories: Magerun

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.