Example #1
0
 public function testParse()
 {
     // Test a single section
     $result = SectionParser::parse("this is a twig content");
     $this->assertInternalType('array', $result);
     $this->assertCount(3, $result);
     $this->assertArrayHasKey("settings", $result);
     $this->assertArrayHasKey("code", $result);
     $this->assertArrayHasKey("markup", $result);
     $this->assertEmpty($result["settings"]);
     $this->assertNull($result["code"]);
     $this->assertNotNull($result["markup"]);
     $this->assertEquals("this is a twig content", $result["markup"]);
     // Test two sections
     $result = SectionParser::parse("url = \"/blog/post/\" \n==\n this is a twig content");
     $this->assertInternalType('array', $result);
     $this->assertCount(3, $result);
     $this->assertArrayHasKey("settings", $result);
     $this->assertArrayHasKey("code", $result);
     $this->assertArrayHasKey("markup", $result);
     $this->assertNotNull($result["markup"]);
     $this->assertNotNull($result["settings"]);
     $this->assertNull($result["code"]);
     $this->assertEquals("this is a twig content", $result["markup"]);
     $this->assertInternalType("array", $result["settings"]);
     $this->assertArrayHasKey("url", $result["settings"]);
     $this->assertEquals("/blog/post/", $result["settings"]["url"]);
     // Test three sections
     $result = SectionParser::parse("url = \"/blog/post/\"\n[section]\nindex = value \n===\n \$var = 23; \n phpinfo(); \n===\n this is a twig content");
     $this->assertInternalType('array', $result);
     $this->assertCount(3, $result);
     $this->assertArrayHasKey("settings", $result);
     $this->assertArrayHasKey("code", $result);
     $this->assertArrayHasKey("markup", $result);
     $this->assertNotNull($result["settings"]);
     $this->assertNotNull($result["markup"]);
     $this->assertNotNull($result["code"]);
     $this->assertEquals("this is a twig content", $result["markup"]);
     $this->assertInternalType("array", $result["settings"]);
     $this->assertArrayHasKey("url", $result["settings"]);
     $this->assertEquals("/blog/post/", $result["settings"]["url"]);
     $this->assertContains("\$var = 23;", $result["code"]);
     $this->assertContains("phpinfo();", $result["code"]);
     $this->assertArrayHasKey("section", $result["settings"]);
     $this->assertInternalType("array", $result["settings"]["section"]);
     $this->assertArrayHasKey("index", $result["settings"]["section"]);
     $this->assertEquals("value", $result["settings"]["section"]["index"]);
     // Test zero sections
     $result = SectionParser::parse("");
     $this->assertCount(3, $result);
     $this->assertArrayHasKey("settings", $result);
     $this->assertArrayHasKey("code", $result);
     $this->assertArrayHasKey("markup", $result);
     $this->assertEmpty($result["settings"]);
     $this->assertNotNull($result["markup"]);
     $this->assertNull($result["code"]);
     $this->assertEquals("", $result["markup"]);
 }
Example #2
0
 /**
  * Loads the object from a file.
  * @param \Cms\Classes\Theme $theme Specifies the theme the object belongs to.
  * @param string $fileName Specifies the file name, with the extension.
  * The file name can contain only alphanumeric symbols, dashes and dots.
  * @return boolean Returns true if the object was successfully loaded. Otherwise returns false.
  */
 public static function load($theme, $fileName)
 {
     if (($obj = parent::load($theme, $fileName)) === null) {
         return null;
     }
     CmsException::mask($obj, 200);
     $parsedData = SectionParser::parse($obj->content);
     CmsException::unmask();
     $obj->settings = $parsedData['settings'];
     $obj->code = $parsedData['code'];
     $obj->markup = $parsedData['markup'];
     $obj->parseComponentSettings();
     $obj->parseSettings();
     return $obj;
 }
 /**
  * Gives out an excerpt of the search term in the file.
  *
  * @param null $length - not working yet: Lenght of excerpt, with the term in the middle
  *
  * @return string - the excerpt
  * @throws Exception
  */
 public function getExcerpt($length = 100)
 {
     if (is_null($this->excerpt)) {
         $contentsParsed = SectionParser::parse($this->getContents());
         $contents = $contentsParsed['markup'];
         $this->excerpt = strip_tags($contents);
     }
     $preExcerpt = "";
     $startPos = stripos($this->excerpt, $this->term) - $length / 2;
     if ($startPos <= 0) {
         $startPos = 0;
     } else {
         $preExcerpt = "...";
     }
     if (strlen($this->excerpt) > $length) {
         $excerpt = substr($this->excerpt, $startPos, $length - 3);
         $lastSpace = strrpos($excerpt, ' ');
         $excerpt = substr($excerpt, 0, $lastSpace);
         $excerpt .= '...';
         $excerpt = $preExcerpt . $excerpt;
     } else {
         $excerpt = $this->excerpt;
     }
     $excerpt = trim(str_ireplace($this->term, "<strong>" . $this->term . "</strong>", $excerpt));
     if (!strlen($excerpt)) {
         $excerpt = "...<strong>" . $this->term . "</strong>...";
     }
     return $excerpt;
 }
 /**
  * Function that inspects a file with a potential search result.
  * Checks if the term is inside the markup section (and not part of metadata.
  *
  * @param $contents
  * @param $term - search term optional
  *
  * @return bool - true if term was found in markup-part
  */
 protected function hasTermInContent($contents, $term)
 {
     $contentsParsed = SectionParser::parse($contents);
     $contents = $contentsParsed['markup'];
     $r = preg_match($term, $contents, $matches);
     return $r == 1;
 }