/**
  * Creates an instance of the object and associates it with a CMS theme.
  * @param \Cms\Classes\Theme $theme Specifies the theme the object belongs to.
  */
 public function __construct(Theme $theme)
 {
     parent::__construct($theme);
     self::$allowedExtensions = self::getEditableExtensions();
 }
示例#2
0
 public function testCache()
 {
     $theme = Theme::load('test');
     $themePath = $theme->getPath();
     $filePath = $themePath .= '/temporary/test.htm';
     if (file_exists($filePath)) {
         @unlink($filePath);
     }
     $this->assertFileNotExists($filePath);
     file_put_contents($filePath, '<p>Test content</p>');
     /*
      * First try - the object should be loaded from the file
      */
     $obj = TestTemporaryCmsObject::loadCached($theme, 'test.htm');
     $this->assertFalse($obj->isLoadedFromCache());
     $this->assertEquals('<p>Test content</p>', $obj->getContent());
     $this->assertEquals('test.htm', $obj->getFileName());
     $this->assertEquals(filemtime($filePath), $obj->mtime);
     /*
      * Second try - the object should be loaded from the cache
      */
     CmsObject::clearInternalCache();
     $obj = TestTemporaryCmsObject::loadCached($theme, 'test.htm');
     $this->assertTrue($obj->isLoadedFromCache());
     $this->assertEquals('<p>Test content</p>', $obj->getContent());
     $this->assertEquals('test.htm', $obj->getFileName());
     $this->assertEquals(filemtime($filePath), $obj->mtime);
     /*
      * Modify the file. The object should be loaded from the disk and re-cached.
      */
     sleep(1);
     // Sleep a second in order to have the update file modification time
     file_put_contents($filePath, '<p>Updated test content</p>');
     clearstatcache();
     // The filemtime() function caches its value within a request, so we should clear its cache.
     CmsObject::clearInternalCache();
     $obj = TestTemporaryCmsObject::loadCached($theme, 'test.htm');
     $this->assertFalse($obj->isLoadedFromCache());
     $this->assertEquals('<p>Updated test content</p>', $obj->getContent());
     $this->assertEquals(filemtime($filePath), $obj->mtime);
     CmsObject::clearInternalCache();
     $obj = TestTemporaryCmsObject::loadCached($theme, 'test.htm');
     $this->assertTrue($obj->isLoadedFromCache());
     $this->assertEquals('<p>Updated test content</p>', $obj->getContent());
     $this->assertEquals(filemtime($filePath), $obj->mtime);
     /*
      * Delete the file. The loadCached() should return null
      */
     @unlink($filePath);
     $this->assertFileNotExists($filePath);
     CmsObject::clearInternalCache();
     $obj = TestTemporaryCmsObject::loadCached($theme, 'test.htm');
     $this->assertNull($obj);
 }
示例#3
0
 /**
  * Saves the object to the disk.
  */
 public function save()
 {
     $this->code = trim($this->code);
     $this->markup = trim($this->markup);
     $trim = function (&$values) use(&$trim) {
         foreach ($values as &$value) {
             if (!is_array($value)) {
                 $value = trim($value);
             } else {
                 $trim($value);
             }
         }
     };
     $trim($this->settings);
     if (array_key_exists('components', $this->settings) && count($this->settings['components']) == 0) {
         unset($this->settings['components']);
     }
     $this->validate();
     $content = [];
     if ($this->settings) {
         $content[] = FileHelper::formatIniString($this->settings);
     }
     if ($this->code) {
         if ($this->wrapCodeToPhpTags() && array_get($this->originalData, 'code') != $this->code) {
             $code = preg_replace('/^\\<\\?php/', '', $this->code);
             $code = preg_replace('/^\\<\\?/', '', $code);
             $code = preg_replace('/\\?>$/', '', $code);
             $content[] = '<?php' . PHP_EOL . $this->code . PHP_EOL . '?>';
         } else {
             $content[] = $this->code;
         }
     }
     $content[] = $this->markup;
     $this->content = trim(implode(PHP_EOL . '==' . PHP_EOL, $content));
     parent::save();
 }
示例#4
0
 /**
  * Saves the object to the disk.
  */
 public function save()
 {
     if ($this->itemData !== false) {
         $this->items = MenuItem::initFromArray($this->itemData);
     }
     $contentData = ['name' => $this->name, 'items' => $this->itemData ? $this->itemData : []];
     $dumper = new YamlDumper();
     $this->content = $dumper->dump($contentData, 20, 0, false, true);
     return parent::save();
 }
 public function testCache()
 {
     $theme = Theme::load('test');
     $themePath = $theme->getPath();
     /*
      * Prepare the test file
      */
     $srcPath = $themePath . '/testobjects/compound.htm';
     $this->assertFileExists($srcPath);
     $testContent = file_get_contents($srcPath);
     $this->assertNotEmpty($testContent);
     $filePath = $themePath .= '/temporary/testcompound.htm';
     if (file_exists($filePath)) {
         @unlink($filePath);
     }
     $this->assertFileNotExists($filePath);
     file_put_contents($filePath, $testContent);
     /*
      * Load the test object to initialize the cache
      */
     $obj = TestTemporaryCmsCompoundObject::loadCached($theme, 'testcompound.htm');
     $this->assertFalse($obj->isLoadedFromCache());
     $this->assertEquals($testContent, $obj->getContent());
     $this->assertEquals('testcompound.htm', $obj->getFileName());
     $this->assertEquals('<p>This is a paragraph</p>', $obj->markup);
     $this->assertInternalType('array', $obj->settings);
     $this->assertArrayHasKey('var', $obj->settings);
     $this->assertEquals('value', $obj->settings['var']);
     $this->assertArrayHasKey('components', $obj->settings);
     $this->assertInternalType('array', $obj->settings['components']['section']);
     $this->assertArrayHasKey('version', $obj->settings['components']['section']);
     $this->assertEquals(10, $obj->settings['components']['section']['version']);
     $this->assertEquals('value', $obj->var);
     $this->assertInternalType('array', $obj->settings['components']['section']);
     $this->assertArrayHasKey('version', $obj->settings['components']['section']);
     $this->assertEquals(10, $obj->settings['components']['section']['version']);
     /*
      * Load the test object again, it should be loaded from the cache this time
      */
     CmsObject::clearInternalCache();
     $obj = TestTemporaryCmsCompoundObject::loadCached($theme, 'testcompound.htm');
     $this->assertTrue($obj->isLoadedFromCache());
     $this->assertEquals($testContent, $obj->getContent());
     $this->assertEquals('testcompound.htm', $obj->getFileName());
     $this->assertEquals('<p>This is a paragraph</p>', $obj->markup);
     $this->assertInternalType('array', $obj->settings);
     $this->assertArrayHasKey('var', $obj->settings);
     $this->assertEquals('value', $obj->settings['var']);
     $this->assertArrayHasKey('components', $obj->settings);
     $this->assertInternalType('array', $obj->settings['components']['section']);
     $this->assertArrayHasKey('version', $obj->settings['components']['section']);
     $this->assertEquals(10, $obj->settings['components']['section']['version']);
     $this->assertEquals('value', $obj->var);
     $this->assertInternalType('array', $obj->settings['components']['section']);
     $this->assertArrayHasKey('version', $obj->settings['components']['section']);
     $this->assertEquals(10, $obj->settings['components']['section']['version']);
 }
 /**
  * Dynamically handle calls into the query instance.
  *
  * @param  string  $method
  * @param  array   $parameters
  * @return mixed
  */
 public function __call($method, $parameters)
 {
     if (in_array($method, $this->passthru)) {
         $collection = $this->get();
         return call_user_func_array([$collection, $method], $parameters);
     }
     return parent::__call($method, $parameters);
 }
 /**
  * Saves the object to the disk.
  */
 public function save()
 {
     $this->code = trim($this->code);
     $this->markup = trim($this->markup);
     $trim = function (&$values) use(&$trim) {
         foreach ($values as &$value) {
             if (!is_array($value)) {
                 $value = trim($value);
             } else {
                 $trim($value);
             }
         }
     };
     $trim($this->settings);
     $this->validate();
     $content = [];
     if ($this->settings) {
         $content[] = FileHelper::formatIniString($this->settings);
     }
     if ($this->code) {
         $code = preg_replace('/^\\<\\?php/', '', $this->code);
         $code = preg_replace('/^\\<\\?/', '', $code);
         $code = preg_replace('/\\?>$/', '', $code);
         $content[] = '<?php' . PHP_EOL . $this->code . PHP_EOL . '?>';
     }
     $content[] = $this->markup;
     $this->content = trim(implode(PHP_EOL . '==' . PHP_EOL, $content));
     parent::save();
 }