/**
  * Returns the list of objects in the specified theme.
  * This method is used internally by the system.
  * @param \Cms\Classes\Theme $theme Specifies a parent theme.
  * @param boolean $skipCache Indicates if objects should be reloaded from the disk bypassing the cache.
  * @return array Returns an array of CMS objects.
  */
 public static function listInTheme($theme, $skipCache = false)
 {
     if (!$theme) {
         throw new ApplicationException(Lang::get('cms::lang.theme.active.not_set'));
     }
     $dirPath = $theme->getPath() . '/' . static::getObjectTypeDirName();
     $result = [];
     if (!File::isDirectory($dirPath)) {
         return $result;
     }
     $it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dirPath));
     $it->setMaxDepth(1);
     // Support only a single level of subdirectories
     $it->rewind();
     while ($it->valid()) {
         if ($it->isFile() && in_array($it->getExtension(), static::$allowedExtensions)) {
             $filePath = $it->getBasename();
             if ($it->getDepth() > 0) {
                 $filePath = basename($it->getPath()) . '/' . $filePath;
             }
             $page = $skipCache ? static::load($theme, $filePath) : static::loadCached($theme, $filePath);
             $result[] = $page;
         }
         $it->next();
     }
     return $result;
 }
 public function testRoot()
 {
     /*
      * Test the / route and the fallback layout
      */
     $theme = new Theme();
     $theme->load('test');
     $controller = new Controller($theme);
     $response = $controller->run('/');
     $this->assertInternalType('string', $response);
     $content = file_get_contents($theme->getPath() . '/pages/index.htm');
     $this->assertEquals('<h1>My Webpage</h1>', trim($response));
 }
Exemple #3
0
 /**
  * Builds and caches a menu item tree.
  * This method is used internally for menu items and breadcrumbs.
  * @param \Cms\Classes\Theme $theme Specifies the current theme.
  * @return array Returns an array containing the page information
  */
 public static function buildMenuTree($theme)
 {
     if (self::$menuTreeCache !== null) {
         return self::$menuTreeCache;
     }
     $key = crc32($theme->getPath()) . 'static-page-menu-tree';
     $cached = Cache::get($key, false);
     $unserialized = $cached ? @unserialize($cached) : false;
     if ($unserialized !== false) {
         return self::$menuTreeCache = $unserialized;
     }
     $menuTree = ['--root-pages--' => []];
     $iterator = function ($items, $parent, $level) use(&$menuTree, &$iterator) {
         $result = [];
         foreach ($items as $item) {
             $viewBag = $item->page->getViewBag();
             $pageCode = $item->page->getBaseFileName();
             $itemData = ['url' => Str::lower(RouterHelper::normalizeUrl($viewBag->property('url'))), 'title' => $viewBag->property('title'), 'mtime' => $item->page->mtime, 'items' => $iterator($item->subpages, $pageCode, $level + 1), 'parent' => $parent, 'navigation_hidden' => $viewBag->property('navigation_hidden')];
             if ($level == 0) {
                 $menuTree['--root-pages--'][] = $pageCode;
             }
             $result[] = $pageCode;
             $menuTree[$pageCode] = $itemData;
         }
         return $result;
     };
     $pageList = new PageList($theme);
     $iterator($pageList->getPageTree(), null, 0);
     self::$menuTreeCache = $menuTree;
     Cache::put($key, serialize($menuTree), Config::get('cms.parsedPageCacheTTL', 10));
     return self::$menuTreeCache;
 }
Exemple #4
0
 public function testGetPath()
 {
     $theme = new Theme();
     $theme->load('test');
     $this->assertEquals(base_path() . '/tests/fixtures/cms/themes/test', $theme->getPath());
 }
Exemple #5
0
 /**
  * Returns the absolute file path.
  * @param \Cms\Classes\Theme $theme Specifies a theme the file belongs to.
  * @param string$fileName Specifies the file name to return the path to.
  * @return string
  */
 protected static function getFilePath($theme, $fileName)
 {
     return $theme->getPath() . '/' . static::getObjectTypeDirName() . '/' . $fileName;
 }
 /**
  * Returns a list of partial-based snippets and corresponding partial names.
  * @param \Cms\Classes\Theme $theme Specifies a parent theme.
  * @return Returns an associative array with the snippet code in keys and partial file names in values.
  */
 public function getPartialSnippetMap($theme)
 {
     $result = [];
     $key = crc32($theme->getPath()) . self::CACHE_KEY_PARTIAL_MAP;
     $cached = Cache::get($key, false);
     if ($cached !== false && ($cached = @unserialize($cached)) !== false) {
         return $cached;
     }
     $partials = Partial::listInTheme($theme);
     foreach ($partials as $partial) {
         $viewBag = $partial->getViewBag();
         $snippetCode = $viewBag->property('snippetCode');
         if (!strlen($snippetCode)) {
             continue;
         }
         $result[$snippetCode] = $partial->getFileName();
     }
     Cache::put($key, serialize($result), Config::get('cms.parsedPageCacheTTL', 10));
     return $result;
 }
 public function testSaveFull()
 {
     $theme = new Theme();
     $theme->load('apitest');
     $destFilePath = $theme->getPath() . '/testobjects/compound.htm';
     if (file_exists($destFilePath)) {
         unlink($destFilePath);
     }
     $this->assertFileNotExists($destFilePath);
     $obj = new TestCmsCompoundObject($theme);
     $obj->fill(['fileName' => 'compound', 'settings' => ['var' => 'value'], 'code' => 'function a() {return true;}', 'markup' => '<p>Hello, world!</p>']);
     $obj->save();
     $referenceFilePath = base_path() . '/tests/fixtures/cms/reference/compound-full.htm';
     $this->assertFileExists($referenceFilePath);
     $this->assertFileExists($destFilePath);
     $this->assertFileEquals($referenceFilePath, $destFilePath);
 }
Exemple #8
0
 public function testSaveNewDir()
 {
     $theme = new Theme();
     $theme->load('apitest');
     $destFilePath = $theme->getPath() . '/testobjects/testsubdir/mytestobj.htm';
     if (file_exists($destFilePath)) {
         unlink($destFilePath);
     }
     $destDirPath = dirname($destFilePath);
     if (file_exists($destDirPath) && is_dir($destDirPath)) {
         rmdir($destDirPath);
     }
     $this->assertFileNotExists($destFilePath);
     $this->assertFileNotExists($destDirPath);
     $testContents = 'mytestcontent';
     $obj = new TestCmsObject($theme);
     $obj->fill(['fileName' => 'testsubdir/mytestobj.htm', 'content' => $testContents]);
     $obj->save();
     $this->assertFileExists($destFilePath);
     $this->assertEquals($testContents, file_get_contents($destFilePath));
 }
 /**
  * Clears the object cache.
  * @param \Cms\Classes\Theme $theme Specifies a parent theme.
  * @return void
  */
 public static function clearCache($theme)
 {
     $key = crc32($theme->getPath()) . 'component-properties';
     Cache::forget($key);
 }