I guess most of the users of Symfony2/Zend2 would have used various list of commands for your bundle/module with your app, it may be for installing web assets or generating entity classes for your propel/Doctrine ORM etc..
In my application, we had to start with a new module which doesnt have any pre-defined data in place, and due to some issues we had decided to add the screens at a later stage. And so to proceed further we need to have few dummy data for testing. So instead of creating a html form, i have planned to create my own console command to load dummy data. Apart from that i have to do some calculations over milliseconds as well. I am adding the code for that console command in this tip, hope it will help for those who create a new reusable bundle or generic module that can be integrated with any other web app.
<?php namespace EF\Bundle\DevBundle\Command; use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; class CurrentMillissecondCommand extends ContainerAwareCommand { class CurrentMillissecondCommand extends ContainerAwareCommand { /** * @see Command * configuring the command */ protected function configure() { $this ->setName('get:current:millis') ->setDescription('Get Current milli seconds') ; } /** * @see Command */ protected function execute(InputInterface $input, OutputInterface $output) { //generates a millisecond. default operation in php is [microtime() * 1000] $billingexpiration = (\MH\Util\Clock\Millis::now()); $output->writeln(sprintf(' Result is <comment>%s</comment>', $billingexpiration)); } }
When you need to enter an input and get a dynamic o/p, then please find my code below that takes input while executing command and returns the number of days added to current millisecond.
<?php namespace EF\Bundle\DevBundle\Command; use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; class AddMillissecondCommand extends ContainerAwareCommand { /** * @see Command */ protected function configure() { $this ->setName('add:days:millis') ->setDescription('Add Number of days to milliseconds.') ->setDefinition(array( new InputArgument('days', InputArgument::REQUIRED, 'days'))) ; } /** * @see Command */ protected function execute(InputInterface $input, OutputInterface $output) { $days = $input->getArgument('days'); $billingexpiration = (\MH\Util\Clock\Millis::now() + (60 * 60 * 24 * $days * 1000)); $output->writeln(sprintf(' Result is <comment>%s</comment>', $billingexpiration)); } /** * @see Command */ protected function interact(InputInterface $input, OutputInterface $output) { if (!$input->getArgument('days')) { $days = $this->getHelper('dialog')->askAndValidate( $output, 'Enter the number of days:', function($days) { if (empty($days)) { throw new \Exception('days can not be empty'); } return $days; } ); $input->setArgument('days', $days); } } }
Result:
Please find the resulting o/p on console command as follows.
satheeskumar@sathees-u11:~/Projects/myproject$ php app/console get:current:millis Result is 1355288119966 satheeskumar@sathees-u11:~/Projects/myproject$ php app/console add:days:millis Enter the number of days:5 Result is 1355720145595