Example #1
0
 /**
  * Initializes the command just after the input has been validated.
  *
  * This is mainly useful when a lot of commands extends one main command
  * where some things need to be initialized based on the input arguments and options.
  *
  * @param InputInterface  $input  An InputInterface instance
  * @param OutputInterface $output An OutputInterface instance
  */
 protected function initialize(InputInterface $input, OutputInterface $output)
 {
     parent::initialize($input, $output);
     $dsn = null;
     $user = null;
     $password = null;
     if ($this->input->hasOption('host') && $this->input->getOption('host')) {
         $host = $this->input->getOption('host');
         $port = 3306;
         if ($this->input->getOption('port')) {
             $port = $this->input->getOption('port');
         }
         $dsn = 'mysql:host=' . urlencode($host) . ';port=' . (int) $port;
     }
     if ($this->input->hasOption('user') && $this->input->getOption('user')) {
         $user = $this->input->getOption('user');
     }
     if ($this->input->hasOption('password') && $this->input->getOption('password')) {
         $password = $this->input->getOption('password');
     }
     if ($user !== null || $password !== null) {
         DatabaseConnection::setDsn($dsn, $user, $password);
     }
 }
Example #2
0
 /**
  * Initialize configuration
  */
 protected function initializeConfiguration()
 {
     $isRunningAsRoot = $this->isRunningAsRoot();
     //#########################
     // Database connection
     //#########################
     if (!empty($this->config['db'])) {
         $dsn = null;
         $username = null;
         $password = null;
         if (!empty($this->config['db']['dsn'])) {
             $dsn = $this->config['db']['dsn'];
         }
         if (!empty($this->config['db']['username'])) {
             $username = $this->config['db']['username'];
         }
         if (!empty($this->config['db']['password'])) {
             $password = $this->config['db']['password'];
         }
         DatabaseConnection::setDsn($dsn, $username, $password);
     }
     //#########################
     // Commands
     //#########################
     if (!empty($this->config['commands']['class'])) {
         // Load list
         foreach ($this->config['commands']['class'] as $class) {
             if ($this->checkCommandClass($class)) {
                 // check OnlyRoot filter
                 if (!$isRunningAsRoot && is_subclass_of($class, '\\CliTools\\Console\\Filter\\OnlyRootFilterInterface')) {
                     // class only useable for root
                     continue;
                 }
                 $this->add(new $class());
             }
         }
     }
 }