PHP code example of j-stam / magento2-shell

1. Go to this page and download the library: Download j-stam/magento2-shell library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

j-stam / magento2-shell example snippets


protected function di(
    \Magento\Catalog\Model\Product $product
) {
    $this->product = $product;
}
 
$this->io->writeJson($data, 'filename.json')
 
$this->io->readJson('filename.json')

$sql = 'SELECT * FROM your_table LIMIT 1';
$result = $this->connection->fetchAll($sql);
// or
$query = $this->connection->query('SELECT * FROM your_table LIMIT 1');
$result = $query->fetchAll();

$this->logger->error('Your error message');
$this->logger->info('Your info message');
...

$this->writeln('Your line');
$this->write('Your message')
$this->write(['Your message', 'Your other message'])



 Example extends \Stam\Shell\ShellAbstract
{
    protected $product;

    /**
     * Optional function, can be used for dependency injection
     */
    protected function di(
        \Magento\Catalog\Model\Product $product
    ) {
        $this->product = $product;
    }

    public function run()
    {
        $this->writeln('Hello world!');
        $this->logger->debug('Hello world!');
    }
}
$shell = new Example();
$shell->run();