示例#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']);
     }
 }
示例#2
0
文件: Project.php 项目: docit/core
 /**
  * Resolves and creates the documents menu from the parsed menu.yml
  *
  * @param array       $items The array converted from yaml
  * @param string $parentId
  * @return \Docit\Core\Menus\Menu
  */
 protected function resolveDocumentsMenu($items, $parentId = 'root')
 {
     /**
      * @var Menus\Menu $menu
      */
     $menu = $this->factory->getMenus()->add('project_sidebar_menu');
     foreach ($items as $item) {
         $link = '#';
         if (array_key_exists('document', $item)) {
             // remove .md extension if present
             $path = Str::endsWith($item['document'], '.md', false) ? Str::remove($item['document'], '.md') : $item['document'];
             $link = $this->factory->url($this, $this->ref, $path);
         } elseif (array_key_exists('href', $item)) {
             $link = $item['href'];
         }
         $id = md5($item['name'] . $link);
         $node = $menu->add($id, $item['name'], $parentId);
         $node->setAttribute('href', $link);
         $node->setAttribute('id', $id);
         if (isset($item['icon'])) {
             $node->setMeta('icon', $item['icon']);
         }
         if (isset($item['children'])) {
             $this->resolveDocumentsMenu($item['children'], $id);
         }
     }
     return $menu;
 }