Example #1
0
 /**
  * Create ClI task class and save in file.
  * 
  *     CLI_Task_Scaffold::create('db/migrate', 'template');
  * 
  * @param  string $name     task name
  * @param  string $template class template, uses [CLI_Task_Scaffold::$templates]
  * @return void
  * @throws CLI_Exception
  */
 public static function create($name, $template)
 {
     // Generate task filename
     $filename = APPPATH . CLI_Tasker::DIR_ROOT . Text::ucfirst($name, '/') . EXT;
     $filename = str_replace(array('_', '/'), DIRECTORY_SEPARATOR, $filename);
     // Create task directory if it's not exist
     $dirname = dirname($filename);
     if (!is_dir($dirname) and !mkdir($dirname, 0755, TRUE)) {
         throw new CLI_Exception('Method :method: can not create directory `:dirname`', array(':method' => __METHOD__, ':dirname' => $dirname));
     }
     // Create class content
     $content = View::factory(CLI_Task_Scaffold::$templates[$template], array('kohana_cli_class' => CLI_Tasker::name2class($name)));
     // Save task content in file
     if (file_put_contents($filename, $content) === FALSE) {
         throw new CLI_Exception('Method :method: can not create file `:filename`', array(':method' => __METHOD__, ':filename' => $filename));
     }
 }
Example #2
0
 /**
  * Gets description for instance of [CLI_Task] class.
  * 
  *     $info = CLI_Task_Info::get_info($name);
  * 
  * @param  string|object $name task name or instance of [CLI_Task] class
  * @return array
  */
 public static function get_info($name)
 {
     if (is_object($name)) {
         // Convert object to task name
         $name = CLI_Tasker::class2name($name);
     }
     if (Kohana::$caching) {
         // Create cache key\tag for find task information
         $cache_key = __METHOD__ . '(' . $name . ')';
         // Try load information from cache
         if ($info = Kohana::cache($cache_key)) {
             return $info;
         }
     }
     // Convert task name to class name
     $class = CLI_Tasker::name2class($name);
     // Create task inspector
     $inspector = new ReflectionClass($class);
     // Get class description and convert to display in CLI
     $description = $inspector->getDocComment();
     // Normalize all new lines to `\n`
     $description = str_replace(array("\r\n", "\n"), "\n", $description);
     // Remove the phpdoc open\close tags and split
     $description = array_slice(explode("\n", $description), 1, -1);
     foreach ($description as $i => $line) {
         // Remove all leading whitespace
         $description[$i] = preg_replace('/^\\s*\\* ?/m', '', $line);
     }
     $description = implode(PHP_EOL, $description);
     // Get default options
     $options = (array) $inspector->getProperty('options')->getValue();
     // Combine information in array
     $info = compact('name', 'class', 'description', 'options');
     if (Kohana::$caching) {
         // Cache task information
         Kohana::cache($cache_key, $info, 3600);
     }
     return $info;
 }
Example #3
0
 /**
  * Create CLI task.
  * 
  *     CLI_Tasker::factory(CLI::option('task'), CLI::option())->execute();
  * 
  * @param  string $name    task name
  * @param  array  $options values of options 
  * @return instance of [CLI_Task]
  * @throws CLI_Exception
  */
 public static function factory($name = '', array $options = array())
 {
     if (empty($name)) {
         // Use default name if task not set
         $name = CLI_Tasker::$default;
     }
     $class = CLI_Tasker::name2class($name);
     if (!class_exists($class)) {
         throw new CLI_Exception('Method :method: class `:class` not exists', array(':method' => __METHOD__, ':class' => $class));
     } elseif (!is_subclass_of($class, CLI_Tasker::PARENT_CLASS)) {
         throw new CLI_Exception('Method :method: class `:class` not extended `:parent`', array(':method' => __METHOD__, ':class' => $class, ':parent' => CLI_Tasker::PARENT_CLASS));
     }
     $class = new $class($options);
     return $class;
 }