示例#1
0
 public function syncRef($ref, $type)
 {
     $content = new GitSyncContent($this->setting('owner'), $this->setting('repository'), $this->github);
     $hasDocs = $content->exists($this->setting('sync.paths.docs'), $ref);
     if (!$hasDocs) {
         return;
     }
     $destinationDir = Path::join($this->project->getPath(), $ref);
     $menu = $content->show($this->setting('sync.paths.menu'), $ref);
     $menuContent = base64_decode($menu['content']);
     $menuArray = Yaml::parse($menuContent);
     $unfilteredPages = [];
     $this->extractDocumentsFromMenu($menuArray['menu'], $unfilteredPages);
     $filteredPages = [];
     # filter out pages that link to external sites
     foreach ($unfilteredPages as $page) {
         if (Str::startsWith($page, 'http', true) || Str::startsWith($page, '//', true) || Str::startsWith($page, 'git', true)) {
             continue;
         }
         if (!in_array($page, $filteredPages, true)) {
             $filteredPages[] = $page;
         }
     }
     # get all pages their content and save to local
     foreach ($filteredPages as $pagePath) {
         $path = Path::join($this->setting('sync.paths.docs'), $pagePath . '.md');
         # check if page exists on remote
         $exists = $content->exists($path, $ref);
         if (!$exists) {
             continue;
         }
         # the raw github page content response
         $pageRaw = $content->show('/' . $path, $ref);
         # transform remote directory path to local directory path
         $dir = Str::remove($pageRaw['path'], $this->setting('sync.paths.docs'));
         $dir = Str::remove($dir, $pageRaw['name']);
         $dir = Path::canonicalize(Path::join($destinationDir, $dir));
         if (!$this->files->exists($dir)) {
             $this->files->makeDirectory($dir);
         }
         # raw github page to utf8 and save it to local
         $this->files->put(Path::join($dir, $pageRaw['name']), base64_decode($pageRaw['content']));
     }
     # save the menu to local
     $this->files->put(Path::join($destinationDir, 'menu.yml'), $menuContent);
     # if enabled, Get phpdoc structure and save it
     if ($this->setting('phpdoc')) {
         $hasStructure = $content->exists($this->setting('paths.phpdoc'), $ref);
         if ($hasStructure) {
             $structure = $content->show($this->setting('paths.phpdoc'), $ref);
             $structureXml = base64_decode($structure['content']);
             $destination = Path::join($destinationDir, 'structure.xml');
             $destinationDir = Path::getDirectory($destination);
             if (!$this->files->exists($destinationDir)) {
                 $this->files->makeDirectory($destinationDir);
             }
             $this->files->put($destination, $structureXml);
         }
     }
     # set cache sha for branches, not for tags (obviously)
     if ($type === 'branch') {
         $branchData = $this->github->repositories()->branches($this->setting('owner'), $this->setting('repository'), $ref);
         $this->cache->forever(md5($this->project->getName() . $branchData['name']), $branchData['commit']['sha']);
     }
 }