Example #1
0
 /**
  * Run task if it exists.
  */
 public function runTask(array $argv)
 {
     $options = getopt($this->options);
     if (!isset($options['t'])) {
         $this->logger->error('No task defined, please define the desired task as first argument. Ej; ./cli.php -t WordFrequency');
     } else {
         $tasksNamespace = 'App\\CLI\\';
         $taskClass = $tasksNamespace . $options['t'] . self::TASK_CLASS_SUFFIX;
         if (class_exists($taskClass)) {
             $taskInstance = new $taskClass($this->logger);
             $taskInstance->main(getopt($this->options, $taskInstance->longOptions));
         } else {
             $this->logger->error("Unknown task {$taskClass}.");
         }
     }
 }
Example #2
0
 public function getTable()
 {
     $this->logger->info("Will print table...");
     while ($content = $this->inputStrategy->read()) {
         $content = $this->clean($content);
         $words = preg_split("/[\\s,]+/", $content);
         $words = array_filter($words);
         $counts = array_count_values($words);
         foreach ($counts as $word => $freq) {
             if (!isset($this->frequencyTable[$word])) {
                 $this->frequencyTable[$word] = $freq;
             } else {
                 $this->frequencyTable[$word] += $freq;
             }
         }
     }
     ksort($this->frequencyTable);
     return $this->frequencyTable;
 }
Example #3
0
 public function close()
 {
     $this->logger->info("Closing resource.");
     fclose($this->resource);
     $this->resource = null;
 }