Exemplo n.º 1
0
 /**
  * @param \Codex\Core\Factory                         $codex
  * @param \Illuminate\Contracts\Filesystem\Filesystem $files
  * @param \Illuminate\Contracts\Container\Container   $container
  * @param string                                      $name
  * @param array                                       $config
  */
 public function __construct(Codex $codex, Filesystem $files, Container $container, $name, $config)
 {
     $this->setContainer($container);
     $this->setCodex($codex);
     $this->setConfig($config);
     $this->setFiles($files);
     $this->name = $name;
     $this->path = $path = Path::join($codex->getRootDir(), $name);
     $this->runHook('project:ready', [$this]);
     # Resolve refs
     $directories = $this->files->directories($this->path);
     $branches = [];
     $this->refs = [];
     $this->versions = array_filter(array_map(function ($dirPath) use($path, $name, &$branches) {
         $version = Str::create(Str::ensureLeft($dirPath, '/'))->removeLeft($path)->removeLeft(DIRECTORY_SEPARATOR);
         $version = (string) $version->removeLeft($name . '/');
         $this->refs[] = $version;
         try {
             return new version($version);
         } catch (\RuntimeException $e) {
             $branches[] = $version;
         }
     }, $directories), 'is_object');
     $this->branches = $branches;
     // check which version/branch to show by default
     $defaultRef = count($this->versions) > 0 ? head($this->versions) : head($branches);
     switch ($this->config['default']) {
         case Project::SHOW_LAST_VERSION:
             usort($this->versions, function (version $v1, version $v2) {
                 return version::gt($v1, $v2) ? -1 : 1;
             });
             $defaultRef = head($this->versions);
             break;
         case Project::SHOW_LAST_VERSION_OTHERWISE_MASTER_BRANCH:
             if (count($this->versions) > 0) {
                 usort($this->versions, function (version $v1, version $v2) {
                     return version::gt($v1, $v2) ? -1 : 1;
                 });
             }
             $defaultRef = count($this->versions) > 0 ? head($this->versions) : head($branches);
             break;
         case Project::SHOW_MASTER_BRANCH:
             $defaultRef = 'master';
             break;
         case Project::SHOW_CUSTOM:
             $defaultRef = $this->config['custom'];
             break;
     }
     $this->ref = $this->defaultRef = (string) $defaultRef;
     # Resolve menu
     $this->runHook('project:done', [$this]);
 }