Example #1
0
 /**
  * Chuyển tất cả các assets về dạng node để resolve dependency
  *
  * @return $this
  * @throws \Exception
  */
 protected function processModuleAssetsToNode()
 {
     //init note
     foreach ($this->izTheme->getAssetsTree() as $assetName => $assetInfo) {
         $this->nodes[$assetName] = new Node($assetName);
     }
     //init dependency
     foreach ($this->izTheme->getAssetsTree() as $assetName => $assetInfo) {
         if (isset($assetInfo['dependency']) && is_array($assetInfo['dependency']) && count($assetInfo['dependency']) > 0) {
             foreach ($assetInfo['dependency'] as $dep) {
                 if (isset($this->nodes[$dep])) {
                     $this->nodes[$assetName]->addEdge($this->nodes[$dep]);
                 } else {
                     throw new \Exception("Not found dependency: " . $dep);
                 }
             }
         }
     }
     return $this;
 }
Example #2
0
 /**
  * Call when render layout.
  * Merger additions assets, 'all' assets, current assets
  * Resolve dependency assets
  *
  * @param \Teepluss\Theme\Theme $theme
  *
  * @param null                  $currentPath
  *
  * @return $this
  * @throws \Exception
  */
 public function initAssets(Theme $theme = null, $currentPath = null)
 {
     if (is_null($theme)) {
         $theme = $this->getTheme();
     }
     if (is_null($currentPath)) {
         if ($this->currentPath) {
             $currentPath = $this->currentPath;
         } else {
             throw new \Exception("Not found current path");
         }
     }
     // Lấy thêm assets từ các modules khác
     $this->initAdditionAssets($currentPath);
     $assets = isset($this->assets[$currentPath]) ? $this->assets[$currentPath] : [];
     // Merge global asset
     $assets = $this->mergeGlobalAssets($assets);
     //Get All assets in modules, not just current assets in path because they are interdependent
     $moduleAssets = $this->izTheme->getAssetsTree();
     /*
      * TODO: Trả về thứ tự cài đặt của assets theo dependency
      */
     $assets = $this->izAssetDependency->processDependency($assets);
     /*
      * Add bower_components asset to theme
      */
     foreach ($assets as $asset) {
         if (isset($moduleAssets[$asset])) {
             if (isset($moduleAssets[$asset]['sources']['scripts'])) {
                 /*add script*/
                 foreach ($moduleAssets[$asset]['sources']['scripts'] as $k => $script) {
                     $theme->asset()->container('footer')->usePath($moduleAssets[$asset]['theme_name'])->add('script-' . $asset . '-' . $k, $script, []);
                 }
             }
             if (isset($moduleAssets[$asset]['sources']['styles'])) {
                 /*add style*/
                 foreach ($moduleAssets[$asset]['sources']['styles'] as $k => $style) {
                     $theme->asset()->usePath($moduleAssets[$asset]['theme_name'])->add('style-' . $asset . '-' . $k, $style, []);
                 }
             }
         } else {
             throw new \Exception('Not found asset: ' . $asset);
         }
     }
     // * --------------------------- Add custom asset to theme ---------------------------
     // From Provider
     if (isset($this->customAssets[$currentPath])) {
         foreach ($this->customAssets[$currentPath] as $customAssetName => $customAsset) {
             $theme->asset()->container('custom-assets')->usePath(isset($customAsset['theme_name']) ? $customAsset['theme_name'] : $this->theme->getThemeName())->add($customAssetName, $customAsset['source'], isset($customAsset['dependency']) ? $customAsset['dependency'] : []);
         }
     }
     // From XML
     $xml = $this->izXml->getXmlByPath($this->theme->getLayoutName());
     if (isset($xml['custom_assets'])) {
         foreach ($xml['custom_assets'] as $customAsset) {
             $theme->asset()->container('custom-assets')->usePath(isset($customAsset['theme_name']) ? $customAsset['theme_name'] : $this->theme->getThemeName())->add($customAsset['name'], $customAsset['source'], isset($customAsset['dependency']) ? explode(',', $customAsset['dependency']) : []);
         }
     }
     $xml = $this->izXml->getXmlByPath('all');
     if (isset($xml['custom_assets'])) {
         foreach ($xml['custom_assets'] as $customAsset) {
             $theme->asset()->container('custom-assets')->usePath(isset($customAsset['theme_name']) ? $customAsset['theme_name'] : $this->theme->getThemeName())->add($customAsset['name'], $customAsset['source'], isset($customAsset['dependency']) ? explode(',', $customAsset['dependency']) : []);
         }
     }
     return $this;
 }