4

Is it possible to create a new Magento command for example to delete the generation folder?

André Ferraz
  • 3,483
  • 6
  • 20
  • 40

2 Answers2

17

I've created several useful commands, which are useful for us who develop Magento 2 sites on a daily basis. Magento 2 console commands are based on symphony, you can create commands for your personal/team use something like bin/magento cache:clean. This way you can execute the command directly from the terminal.

Here is a simple hello world command. Before we get started clear you generation folder and here is what you need to do.

Create a new module for illustration purposes I'll call it Tools under app/code/Andre/, include the registration.php and module.xml.

app/code/Andre/Tools/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Andre_Tools',
    __DIR__
);

app/code/Andre/Tools/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Andre_Tools" setup_version="0.1.0"/>
</config>

Create a new model class, this is where it will contains the options, description and the logic of your command.

app/code/Andre/Tools/Model/Generation.php

<?php
namespace Andre\Tools\Model;

use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface;

class Generation extends Command { protected function configure() { $this->setName('generation:clean') ->setDescription('The description of you command here!');

    parent::configure();
}

protected function execute(InputInterface $input, OutputInterface $output)
{
    $output-&gt;writeln('Hello World!');
}

}

app/code/Andre/Tools/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Framework\Console\CommandList">
        <arguments>
            <argument name="commands" xsi:type="array">
                <item name="clean" xsi:type="object">Andre\Tools\Model\Generation</item>
            </argument>
        </arguments>
    </type>
</config>

Lastly do a bin/magento setup:upgrade, check that the module is active bin/magento module:status if not then run bin/magento module:enable Andre_Tools.

Now to run the command you just create simply run:

 bin/magento generation:clean

Now just add your own logic under the execute() method, to delete the generation folder.

Msquare
  • 9,063
  • 7
  • 25
  • 63
André Ferraz
  • 3,483
  • 6
  • 20
  • 40
0

Here the Vendor is Store and module name is Tools

Create these files:

app/code/Store/Tools/etc/di.xml
app/code/Store/Tools/etc/module.xml
app/code/Store/Tools/registration.php
app/code/Store/Tools/Console/Command/Generation.php

di.xml:

<?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
        <type name="Magento\Framework\Console\CommandList">
            <arguments>
                <argument name="commands" xsi:type="array">
                    <item name="delete_generation" xsi:type="object">Store\Tools\Console\Command\Generation</item>
                </argument>
            </arguments>
        </type>
    </config>

module.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Store_Tools" setup_version="1.0.0">
    </module>
</config>

registration.php

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Store_Tools',
    __DIR__
);

Generation.php :

namespace Store\Tools\Console\Command;

use Symfony\Component\Console\Command\Command; // for parent class
use Symfony\Component\Console\Input\InputInterface; // for InputInterface used in execute method
use Symfony\Component\Console\Output\OutputInterface; // for OutputInterface used in execute method
use Symfony\Component\Filesystem\Filesystem;

class Generation extends Command
{
    protected function configure()
    {
        $this->setName('clear:generation')
             ->setDescription('Clear var/generation folder!');

        parent::configure();
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $fs = new Filesystem();
        try {
            if($fs->exists('var/generation')){
                $fs->remove(array('var/generation'));
                $output->writeln('Cleared generation!');
            }
            else {
             $output->writeln('Can\'t find directory');
            }
        } catch (IOExceptionInterface $e) {
            echo "An error occurred while deleting your directory at ".$e->getPath();
        }
    }
}

Run php bin/magento setup:upgrade

And you are ready to use the command like this

php bin/magento clear:generation
Sejal Shah
  • 2,141
  • 1
  • 16
  • 46