Last active
January 19, 2023 08:42
-
-
Save magevision/c50ebd6045f17b7ecc5c139e00fc1733 to your computer and use it in GitHub Desktop.
SetNameToACustomCLICommand
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace MageVision\Blog64\Console\Command; | |
use Magento\Directory\Model\ResourceModel\Country\CollectionFactory; | |
use Magento\Framework\Console\Cli; | |
use Magento\Framework\Profiler\Driver\Standard\Stat; | |
use Magento\Framework\Profiler\Driver\Standard\StatFactory; | |
use Symfony\Component\Console\Command\Command; | |
use Symfony\Component\Console\Input\InputInterface; | |
use Symfony\Component\Console\Output\OutputInterface; | |
class ProfilerCommand extends Command | |
{ | |
/** | |
* @var StatFactory | |
*/ | |
private $statFactory; | |
/** | |
* @var CollectionFactory | |
*/ | |
private $countryCollectionFactory; | |
/** | |
* @param StatFactory $statFactory | |
* @param CollectionFactory $countryCollectionFactory | |
* @param string $name | |
*/ | |
public function __construct( | |
StatFactory $statFactory, | |
CollectionFactory $countryCollectionFactory | |
) { | |
$this->statFactory = $statFactory; | |
$this->countryCollectionFactory = $countryCollectionFactory; | |
//2nd way | |
parent::__construct('magevision:blog64:profiler'); | |
} | |
/** | |
* @inheritDoc | |
*/ | |
protected function configure() | |
{ | |
//3rd way | |
$this->setName('magevision:blog64:profiler'); | |
$this->setDescription('MageVision console command with profiler.'); | |
parent::configure(); | |
} | |
/** | |
* Execute the command | |
* | |
* @param InputInterface $input | |
* @param OutputInterface $output | |
* | |
* @return int | |
*/ | |
protected function execute(InputInterface $input, OutputInterface $output) | |
{ | |
$output->writeln('<info>Process starts.</info>'); | |
$stat = $this->statFactory->create(); | |
$stat->start( | |
'magevision_blog64_profiler', | |
microtime(true), | |
memory_get_usage(true), | |
memory_get_usage() | |
); | |
$countries = $this->countryCollectionFactory->create()->getItems(); | |
foreach ($countries as $country) { | |
// Add here your functionality | |
//... | |
} | |
$stat->stop( | |
'magevision_blog64_profiler', | |
microtime(true), | |
memory_get_usage(true), | |
memory_get_usage() | |
); | |
$stat = $stat->get('magevision_blog64_profiler'); | |
$stat['time'] = $stat[Stat::TIME]; | |
$output->writeln([ | |
'<info>Process successfully finished.</info>', | |
sprintf('<info>Executing time in microseconds.: %s</info>', $stat[Stat::TIME]), | |
sprintf('<info>Real size of memory allocated from system: %s</info>', $stat[Stat::REALMEM]), | |
sprintf('<info>Memory used by emalloc(): %s</info>', $stat[Stat::EMALLOC]), | |
]); | |
return Cli::RETURN_SUCCESS; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment