/**
     */
    public function testGetBlocks()
    {
        $strTemplate = <<<EOT
<!-- BEGIN one -->
 <!-- BEGIN three -->
  <!-- BEGIN four -->
   <!-- BEGIN six -->
    some text
   <!-- END six -->
  <!-- END four -->
 <!-- END three -->
 <!-- BEGIN five -->
 <!-- END five -->
<!-- END one -->
<!-- BEGIN two -->
<!-- END two -->
EOT;
        $arBlocks = HTML_Template_PHPLIB_Generator::getBlocks(HTML_Template_PHPLIB_Helper::splitLines($strTemplate));
        $this->assertEquals(2, count($arBlocks));
        $this->assertArrayHasKey('one', $arBlocks);
        $this->assertArrayHasKey('two', $arBlocks);
        $this->assertEquals(2, count($arBlocks['one']['sub']));
        $this->assertArrayHasKey('three', $arBlocks['one']['sub']);
        $this->assertArrayHasKey('five', $arBlocks['one']['sub']);
        $this->assertArrayHasKey('four', $arBlocks['one']['sub']['three']['sub']);
    }
    /**
     */
    public function testSplitLines()
    {
        $strContent = <<<EOT
1
22
333
44
5
EOT;
        $this->assertEquals(array('1', '22', '333', '44', '5'), HTML_Template_PHPLIB_Helper::splitLines($strContent));
    }
 /**
  * Validates a template file.
  * You can pass either a file name, or the file content. One of the parameters
  *  needs to be !== null.
  *
  * @param string $strFile    Template file name to check
  * @param string $strContent Template content to check
  *
  * @return mixed Boolean true if no errors have been found, array of
  *                errors otherwise. An error is an array with keys
  *                - 'short' (short error code, string)
  *                - 'message' (readable message)
  *                - 'line'    (line number)
  *                - 'code'    (code that caused the error)
  *                false if no file and content is given
  */
 public static function validate($strFile = null, $strContent = null)
 {
     $arLines = HTML_Template_PHPLIB_Helper::getLines($strFile, $strContent);
     if ($arLines === false) {
         return false;
     }
     $arErrors = HTML_Template_PHPLIB_Validator::checkBlockDefinitions($arLines);
     $arErrors = array_merge($arErrors, HTML_Template_PHPLIB_Validator::checkVariables($arLines));
     HTML_Template_PHPLIB_Validator::sortByLine($arErrors);
     return count($arErrors) == 0 ? true : $arErrors;
 }
Exemple #4
0
 /**
  * Returns an array with all lines of the text.
  * Extracts it from the file or the text
  *
  * @param string $strFile    File name
  * @param string $strContent Template code
  *
  * @return array Array with text lines, without trailing newlines,
  *                false when both are null
  */
 public static function getLines($strFile = null, $strContent = null)
 {
     if ($strContent !== null) {
         $arLines = HTML_Template_PHPLIB_Helper::splitLines($strContent);
     } else {
         if ($strFile !== null) {
             $arLines = file($strFile, FILE_IGNORE_NEW_LINES);
         } else {
             //all null?
             return false;
         }
     }
     return $arLines;
 }
 /**
  * Creates the code to use a given template file
  *
  * @param string $strFile    Template file
  * @param string $strTplName Template reference name
  * @param string $strPrefix  Prefix to prepend before the code
  *
  * @return string PHP code
  */
 function getCodeBlockDefinition($strFile, $strTplName = null, $strPrefix = '$tpl')
 {
     $arBlocks = HTML_Template_PHPLIB_Generator::getBlocks(HTML_Template_PHPLIB_Helper::getLines($strFile));
     if ($strTplName === null) {
         $strTplName = HTML_Template_PHPLIB_Generator::getTemplateNameFromFilename($strFile);
     }
     $nl = "\r\n";
     $code = '';
     $code .= $strPrefix . ' = new HTML_Template_PHPLIB();' . $nl;
     $code .= HTML_Template_PHPLIB_Generator::getCodeBlock($arBlocks, $strTplName, $strPrefix);
     $code .= $nl;
     $code .= '//TODO: do something with the code' . $nl;
     $code .= $nl;
     $code .= $strPrefix . '->finish(' . trim($strPrefix) . "->parse('TMP', '" . $strTplName . "'));" . $nl;
     return $code;
 }
    /**
     * Test if wrongly defined variables are detected
     */
    public function testCheckVariables()
    {
        $arErrors = HTML_Template_PHPLIB_Validator::checkVariables(array());
        $this->assertEquals(array(), $arErrors);
        $cont = <<<EOT
{FULL}
{PARTIAL1
PARTIAL2}
<h1>{FULL2}{FULL3}</h2>
<p><strong>{PARTIAL3</strong>}</p>
<p>{<strong>PARTIAL3}</strong></p>
EOT;
        $arErrors = HTML_Template_PHPLIB_Validator::checkVariables(HTML_Template_PHPLIB_Helper::getLines(null, $cont));
        $arErrors = self::stripMessages($arErrors);
        $this->assertEquals(array(array('short' => 'CLOSING_BRACE_MISSING', 'line' => 2, 'code' => '{PARTIAL1'), array('short' => 'OPENING_BRACE_MISSING', 'line' => 3, 'code' => 'PARTIAL2}'), array('short' => 'CLOSING_BRACE_MISSING', 'line' => 5, 'code' => '{PARTIAL3<'), array('short' => 'OPENING_BRACE_MISSING', 'line' => 6, 'code' => '>PARTIAL3}')), $arErrors);
    }