Beispiel #1
0
 public function process()
 {
     $noteDir = getenv('NOTE_HOME');
     // Establish the directory where notes will be stored
     if ($noteDir === false) {
         $homeDir = getenv('HOME');
         if ($homeDir !== false) {
             $noteDir = $homeDir . '/notes';
         } else {
             $this->log->error('Configuration Error, No $HOME env var');
             throw new Exception('There is no $HOME environment variable defined.');
         }
     }
     // Capture arguments
     $args = array_slice($_SERVER['argv'], 1);
     $t = array_search('-t', $args);
     $t = $t === false ? array_search('--title', $args) : $t;
     if ($t !== false) {
         try {
             $title = trim($args[$t + 1]) ?: null;
         } catch (ErrorException $e) {
             if (preg_match('/Undefined offset/i', $e->getMessage())) {
                 $this->log->warning('Missing Title Value');
                 throw new Exception('Argument `' . $args[$t] . '` must be followed by a value.');
             }
         }
         if (substr($title, 0, 1) === '-' || substr($title, 0, 2) === '--') {
             $this->log->warning('Invalid Note Title', [$title]);
             throw new Exception('Title cannot start with - or --');
         }
     } else {
         $title = null;
     }
     // Create the directory if it doesn't exist
     if (!is_dir($noteDir)) {
         mkdir($noteDir);
     }
     // Define the filename
     $currentDate = date('Y-m-d h-i-s');
     $fileName = str_replace(' ', '-', $currentDate);
     if ($title) {
         $fileName .= '_' . StringUtil::simplify($title);
         $heading = "# {$title}\n{$currentDate}\n\n";
     } else {
         $heading = "# Note {$currentDate}\n\n";
         $this->log->info('Note Title Not Specified, Using Default', [$heading]);
     }
     $content = $heading;
     $filePath = $noteDir . '/' . $fileName . '.md';
     // Create it if it doesn't exist
     if (!file_exists($filePath)) {
         $this->log->info('Note Created', [$filePath]);
         file_put_contents($filePath, $content);
     } else {
         $this->log->warning('Note Already Exists', [$filePath]);
         throw new Exception('There is already a note with this title (' . $filePath . ').');
     }
     // Output the path
     echo $filePath;
 }
 /**
  * @dataProvider simplifyData
  */
 public function testSimplify($input, $expectedResult)
 {
     $result = StringUtil::simplify($input);
     $this->assertEquals($expectedResult, $result);
 }