Ejemplo n.º 1
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;
 }
Ejemplo n.º 2
0
    public function testParseOffset()
    {
        // Test three sections
        $content = <<<ESC
setting = "test"
==
function onStart() { // Line 3

}
==
<p>Line 7</p>
ESC;
        $result = SectionParser::parseOffset($content);
        $this->assertArrayHasKey("settings", $result);
        $this->assertArrayHasKey("code", $result);
        $this->assertArrayHasKey("markup", $result);
        $this->assertNotNull($result["settings"]);
        $this->assertNotNull($result["code"]);
        $this->assertNotNull($result["markup"]);
        $this->assertEquals(1, $result["settings"]);
        $this->assertEquals(3, $result["code"]);
        $this->assertEquals(7, $result["markup"]);
        // Test two sections
        $content = <<<ESC
setting = "test"
another = "setting"
foo = "bar"
==
<p>Line 5</p>
ESC;
        $result = SectionParser::parseOffset($content);
        $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(1, $result["settings"]);
        $this->assertEquals(5, $result["markup"]);
        // Test two sections with white space
        $content = <<<ESC


line = "Line 3"
another = "setting"
foo = "bar"
==





<p>Line 12</p>
ESC;
        $result = SectionParser::parseOffset($content);
        $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(3, $result["settings"]);
        $this->assertEquals(12, $result["markup"]);
        // Test one section
        $content = <<<ESC
<p>Line 1</p>
<p>Line 2</p>
<p>Line 3</p>
ESC;
        $result = SectionParser::parseOffset($content);
        $this->assertArrayHasKey("settings", $result);
        $this->assertArrayHasKey("code", $result);
        $this->assertArrayHasKey("markup", $result);
        $this->assertNotNull($result["markup"]);
        $this->assertNull($result["settings"]);
        $this->assertNull($result["code"]);
        $this->assertEquals(1, $result["markup"]);
        // Test empty PHP
        $content = <<<ESC
setting = "test"
==
==
<p>Line 4</p>
ESC;
        $result = SectionParser::parseOffset($content);
        $this->assertArrayHasKey("settings", $result);
        $this->assertArrayHasKey("code", $result);
        $this->assertArrayHasKey("markup", $result);
        $this->assertNotNull($result["settings"]);
        $this->assertNotNull($result["code"]);
        $this->assertNotNull($result["markup"]);
        $this->assertEquals(1, $result["settings"]);
        $this->assertEquals(3, $result["code"]);
        $this->assertEquals(4, $result["markup"]);
        // Test with PHP tags
        $content = <<<ESC
setting = "test"
another = "setting"
==
<?
function onStart() {

}
?>
==
<p>Line 10</p>
ESC;
        $result = SectionParser::parseOffset($content);
        $this->assertArrayHasKey("settings", $result);
        $this->assertArrayHasKey("code", $result);
        $this->assertArrayHasKey("markup", $result);
        $this->assertNotNull($result["settings"]);
        $this->assertNotNull($result["code"]);
        $this->assertNotNull($result["markup"]);
        $this->assertEquals(1, $result["settings"]);
        $this->assertEquals(5, $result["code"]);
        $this->assertEquals(10, $result["markup"]);
        // Test with PHP tags and whitespace
        $content = <<<ESC
setting = "test"
another = "setting"
foo = "bar"
==







<?php
function onStart() { // Line 13

}
?>
==
<p>Line 18</p>
ESC;
        $result = SectionParser::parseOffset($content);
        $this->assertArrayHasKey("settings", $result);
        $this->assertArrayHasKey("code", $result);
        $this->assertArrayHasKey("markup", $result);
        $this->assertNotNull($result["settings"]);
        $this->assertNotNull($result["code"]);
        $this->assertNotNull($result["markup"]);
        $this->assertEquals(1, $result["settings"]);
        $this->assertEquals(13, $result["code"]);
        $this->assertEquals(18, $result["markup"]);
        // Test with PHP tags and whitespace both sides
        $content = <<<ESC
setting = "test"
another = "setting"
foo = "bar"
==







<?php







function onStart() { // Line 20

}
?>
==
<p>Line 25</p>
ESC;
        $result = SectionParser::parseOffset($content);
        $this->assertArrayHasKey("settings", $result);
        $this->assertArrayHasKey("code", $result);
        $this->assertArrayHasKey("markup", $result);
        $this->assertNotNull($result["settings"]);
        $this->assertNotNull($result["code"]);
        $this->assertNotNull($result["markup"]);
        $this->assertEquals(1, $result["settings"]);
        $this->assertEquals(20, $result["code"]);
        $this->assertEquals(25, $result["markup"]);
        // Test with whitespace on PHP and Twig
        $content = <<<ESC
setting = "test"
another = "setting"
foo = "bar"
==



function onStart() { // Line 8

}
==







<p>Line 19</p>
ESC;
        $result = SectionParser::parseOffset($content);
        $this->assertArrayHasKey("settings", $result);
        $this->assertArrayHasKey("code", $result);
        $this->assertArrayHasKey("markup", $result);
        $this->assertNotNull($result["settings"]);
        $this->assertNotNull($result["code"]);
        $this->assertNotNull($result["markup"]);
        $this->assertEquals(1, $result["settings"]);
        $this->assertEquals(8, $result["code"]);
        $this->assertEquals(19, $result["markup"]);
        // Test namespaces
        $content = <<<ESC

line = "Line 2"
setting = "test"
==

use October\\Rain\\Support\\Str; // This will be removed (-1 line)
use October\\Rain\\Support\\FlashBag; // This will be removed (-1 line)

function onStart() { // Line 7

    use October\\Rain\\Support\\Str; // And placed here
    use October\\Rain\\Support\\FlashBag; // And placed here

}
==
<p>Line 16</p>
ESC;
        $result = SectionParser::parseOffset($content);
        $this->assertArrayHasKey("settings", $result);
        $this->assertArrayHasKey("code", $result);
        $this->assertArrayHasKey("markup", $result);
        $this->assertNotNull($result["settings"]);
        $this->assertNotNull($result["code"]);
        $this->assertNotNull($result["markup"]);
        $this->assertEquals(2, $result["settings"]);
        $this->assertEquals(7, $result["code"]);
        $this->assertEquals(16, $result["markup"]);
    }
 /**
  * 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;
 }
Ejemplo n.º 4
0
 /**
  * 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;
 }
Ejemplo n.º 5
0
 /**
  * Override properties of an exception specific to the Twig section
  * of a CMS object.
  * @param \Exception $exception The exception to modify.
  * @return bool
  */
 protected function processTwig(Exception $exception)
 {
     // Must be a Twig related exception
     if (!$exception instanceof Twig_Error) {
         return false;
     }
     $this->message = $exception->getRawMessage();
     $this->line = $exception->getTemplateLine();
     // Find where the twig markup section begins
     $offsetArray = SectionParser::parseOffset($this->compoundObject->getContent());
     $this->line += $offsetArray['markup'];
     // Account for line 0
     $this->line--;
     return true;
 }