/**
  * Class init.
  *
  * @param  string   $element The component option name.
  * @param  \JUri    $uri     The uri object.
  * @param  Registry $option  The option of this server.
  *
  * @throws \InvalidArgumentException
  */
 public function __construct($element, \JUri $uri, Registry $option = null)
 {
     $extracted = ExtensionHelper::extractElement($element);
     $this->option = $option ?: new Registry();
     $this->uri = $uri;
     if ($extracted['type'] !== 'component') {
         throw new \InvalidArgumentException('Please give me a component name like `com_flower`.');
     }
     $this->component = $extracted['name'];
 }
 /**
  * Get the path of extension.
  *
  * @param   string   $element   The extension element name, example: com_content or plg_group_name
  * @param   string   $client    Site or administrator.
  * @param   boolean  $absolute  True to return whole path.
  *
  * @return  string  The found path.
  */
 public static function get($element, $client = null, $absolute = true)
 {
     $element = strtolower($element);
     $extracted = ExtensionHelper::extractElement($element);
     $extension = $extracted['type'];
     $name = $extracted['name'];
     $group = $extracted['group'];
     // Assign name path.
     switch ($extension) {
         case 'component':
         case 'module':
             $folder = $element;
             break;
         case 'plugin':
             $folder = $group . '/' . $name;
             $client = 'site';
             break;
         case 'library':
             $client = 'site';
         default:
             $folder = $name;
             break;
     }
     // Build path
     $extension = StringInflector::getInstance()->toPlural($extension);
     $path = $extension . '/' . $folder;
     if (!$absolute) {
         return $path;
     }
     // Add absolute path.
     switch ($client) {
         case 'site':
             $path = JPATH_SITE . '/' . $path;
             break;
         case 'admin':
         case 'administrator':
             $path = JPATH_ADMINISTRATOR . '/' . $path;
             break;
         default:
             $path = JPATH_BASE . '/' . $path;
             break;
     }
     return $path;
 }
 /**
  * prepareExecute
  *
  * @return  void
  */
 protected function prepareExecute()
 {
     $element = $this->getArgument(1);
     $client = strtolower($this->getOption('c'));
     $class = $this->getOption('class');
     if ($element) {
         list($type, $name, $group) = array_values(ExtensionHelper::extractElement($element));
         if ($client == 'admin') {
             $client = 'administrator';
         }
         if ($type == 'plugin') {
             $client = 'site';
         } elseif ($type == 'component') {
             $client = 'administrator';
         }
     }
     $path = JPATH_ROOT . '/resources/seeders';
     $classPath = $path . '/' . $class . '.php';
     if (!file_exists($classPath) && $element) {
         $path = PathHelper::get($element, $client);
         $classPath = $path . '/src/' . ucfirst($name) . '/Seed/' . $class . '.php';
     }
     if (file_exists($classPath)) {
         include_once $classPath;
     }
     $className = $class;
     if (!class_exists($className)) {
         $className = sprintf('%s\\Seed\\%s', ucfirst($name), ucfirst($class));
     }
     if (!class_exists($className)) {
         throw new \UnexpectedValueException('Class: ' . $class . ' not found.');
     }
     // Auto include classes
     $path = dirname(ReflectionHelper::getPath($className));
     $files = \JFolder::files($path, '.', false, true);
     /** @var \SplFileInfo $file */
     foreach ($files as $file) {
         $file = new \SplFileInfo($file);
         \JLoader::register($file->getBasename('.php'), $file->getPathname());
     }
     $this->app->set('seed.class', $className);
 }
 /**
  * Execute Component.
  *
  * @param string $option Component option name.
  * @param string $client `admin` or `site`.
  * @param array  $input  Input object.
  *
  * @return  mixed
  */
 public static function executeComponent($option, $client = 'site', $input = array())
 {
     $element = ExtensionHelper::extractElement($option);
     $input = new \JInput($input);
     // Prevent class conflict
     class_alias('JString', 'Joomla\\String\\String');
     if (!defined('JPATH_COMPONENT_ADMINISTRATOR')) {
         define('JPATH_COMPONENT_ADMINISTRATOR', PathHelper::get($option, 'admin'));
         define('JPATH_COMPONENT_SITE', PathHelper::get($option, 'site'));
         define('JPATH_COMPONENT', PathHelper::get($option, $client));
     }
     $_SERVER['HTTP_HOST'] = 'windwalker';
     if ($client == 'admin') {
         $client = 'administrator';
     }
     $appClass = 'JApplication' . ucfirst($client);
     $console = \JFactory::$application;
     \JFactory::$application = $appClass::getInstance('site', $input);
     $class = ucfirst($element['name']) . 'Component';
     $component = new $class(ucfirst($element['name']), $input, \JFactory::$application);
     $result = $component->execute();
     \JFactory::$application = $console;
     return $result;
 }
 /**
  * Get config class name by extension name.
  *
  * Example:
  * - Component - Flower\Config\Config
  * - Module    - ModFlower\Config\Config
  * - Plugin    - PlgSystemFlower\Config\Config
  * - Template  - TplFlower\Config\Config
  *
  * @param string $element  The extension name.
  *
  * @throws \LogicException
  * @throws \DomainException
  * @return  string The config class name.
  */
 public static function getClass($element)
 {
     $extracted = ExtensionHelper::extractElement($element);
     switch ($extracted['type']) {
         case 'module':
             $class = 'Mod' . ucfirst($extracted['name']) . '\\Config\\Config';
             break;
         case 'plugin':
             if (!$extracted['group']) {
                 throw new \LogicException(sprintf('Please give me group name when get plugin config.'));
             }
             $class = 'Plg' . ucfirst($extracted['group']) . ucfirst($extracted['name']) . '\\Config\\Config';
             break;
         case 'component':
             $class = ucfirst($extracted['name']) . '\\Config\\Config';
             break;
         case 'template':
             $class = 'Tpl' . ucfirst($extracted['name']) . '\\Config\\Config';
             break;
         default:
             throw new \DomainException(sprintf('Don\'t get config from this extension: %s', $element));
     }
     return $class;
 }