<commit_before><?php

/**
 * @copyright  <NAME>
 * @license    http://www.makoframework.com/license
 */

namespace mako\view\compilers;

use mako\file\FileSystem;

/**
 * Template compiler.
 *
 * @author  <NAME>
 */
class Template
{
	/**
	 * File system instance.
	 *
	 * @var \mako\file\FileSystem
	 */
	protected $fileSystem;

	/**
	 * Path to compiled template.
	 *
	 * @var string
	 */
	protected $cachePath;

	/**
	 * Path to raw template.
	 *
	 * @var string
	 */
	protected $template;

	/**
	 * Compilation order.
	 *
	 * @var array
	 */
	protected $compileOrder =
	[
		'comments',
		'extensions',
		'views',
		'blocks',
		'controlStructures',
		'echos',
	];

	/**
	 * Constructor.
	 *
	 * @access  public
	 * @param   \mako\file\FileSystem  $fileSystem  File system instance
	 * @param   string                 $cachePath   Cache path
	 * @param   string                 $template    Path to template
	 */
	public function __construct(FileSystem $fileSystem, $cachePath, $template)
	{
		$this->fileSystem = $fileSystem;

		$this->cachePath = $cachePath;

		$this->template = $template;
	}

	/**
	 * Compiles comments.
	 *
	 * @access  protected
	 * @param   string     $template  Template
	 * @return  string
	 */
	protected function comments($template)
	{
		// Strip comments from templates

		return preg_replace('/{#(.*?)#}/s', '', $template);
	}

	/**
	 * Compiles template extensions.
	 *
	 * @access  protected
	 * @param   string     $template  Template
	 * @return  string
	 */
	protected function extensions($template)
	{
		// Replace first occurance of extends tag with an empty string
		// and append the template with a view tag

		if(preg_match('/^{%\s*extends:(.*?)\s*%}/i', $template, $matches) > 0)
		{
			$replacement = '<?php $__view__ = $__viewfactory__->create(' . $matches[1] . '); $__renderer__ = $__view__->getRenderer(); ?>';

			$template = preg_replace('/^{%\s*extends:(.*?)\s*%}/i', $replacement, $template, 1);

			$template .= '<?php echo $__view__->render(); ?>';
		}

		return $template;
	}

	/**
	 * Compiles view includes.
	 *
	 * @access  protected
	 * @param   string     $template  Template
	 * @return  string
	 */
	protected function views($template)
	{
		// Compile view includes with parameters

		$template = preg_replace('/{{\s*view:([^,]*?)\s*,\s*\[(.*?)\]\s*}}/i', '<?php echo $__viewfactory__->create($1, array_merge(get_defined_vars(), [$2]))->render(); ?>', $template);

		// Compile view includes without parameters

		return preg_replace('/{{\s*view:(.*?)\s*}}/i', '<?php echo $__viewfactory__->create($1, get_defined_vars())->render(); ?>', $template);
	}

	/**
	 * Compiles blocks.
	 *
	 * @access  protected
	 * @param   string     $template  Template
	 * @return  string
	 */
	protected function blocks($template)
	{
		// Compile blocks<commit_msg>Improved regex to also support function and method calls before variables<commit_after><?php

/**
 * @copyright  <NAME>
 * @license    http://www.makoframework.com/license
 */

namespace mako\view\compilers;

use mako\file\FileSystem;

/**
 * Template compiler.
 *
 * @author  <NAME>
 */
class Template
{
	/**
	 * File system instance.
	 *
	 * @var \mako\file\FileSystem
	 */
	protected $fileSystem;

	/**
	 * Path to compiled template.
	 *
	 * @var string
	 */
	protected $cachePath;

	/**
	 * Path to raw template.
	 *
	 * @var string
	 */
	protected $template;

	/**
	 * Compilation order.
	 *
	 * @var array
	 */
	protected $compileOrder =
	[
		'comments',
		'extensions',
		'views',
		'blocks',
		'controlStructures',
		'echos',
	];

	/**
	 * Constructor.
	 *
	 * @access  public
	 * @param   \mako\file\FileSystem  $fileSystem  File system instance
	 * @param   string                 $cachePath   Cache path
	 * @param   string                 $template    Path to template
	 */
	public function __construct(FileSystem $fileSystem, $cachePath, $template)
	{
		$this->fileSystem = $fileSystem;

		$this->cachePath = $cachePath;

		$this->template = $template;
	}

	/**
	 * Compiles comments.
	 *
	 * @access  protected
	 * @param   string     $template  Template
	 * @return  string
	 */
	protected function comments($template)
	{
		// Strip comments from templates

		return preg_replace('/{#(.*?)#}/s', '', $template);
	}

	/**
	 * Compiles template extensions.
	 *
	 * @access  protected
	 * @param   string     $template  Template
	 * @return  string
	 */
	protected function extensions($template)
	{
		// Replace first occurance of extends tag with an empty string
		// and append the template with a view tag

		if(preg_match('/^{%\s*extends:(.*?)\s*%}/i', $template, $matches) > 0)
		{
			$replacement = '<?php $__view__ = $__viewfactory__->create(' . $matches[1] . '); $__renderer__ = $__view__->getRenderer(); ?>';

			$template = preg_replace('/^{%\s*extends:(.*?)\s*%}/i', $replacement, $template, 1);

			$template .= '<?php echo $__view__->render(); ?>';
		}

		return $template;
	}

	/**
	 * Compiles view includes.
	 *
	 * @access  protected
	 * @param   string     $template  Template
	 * @return  string
	 */
	protected function views($template)
	{
		// Compile view includes with parameters

		$template = preg_replace('/{{view:((((?!,(?![^()]*\))[^,]+).)+?),\s*\[(.*?)\]}})/i', '<?php echo $__viewfactory__->create($2, [$4] + get_defined_vars())->render(); ?>', $template);

		// Compile view includes without parameters

		return preg_replace('/{{\s*view:(.*?)\s*}}/i', '<?php echo $__viewfactory__->create($1, get_defined_vars())->render(); ?>', $template);
	}

	/**
	 * Compiles blocks.
	 *
	 * @access  protected
	 * @param   string     $template  Template
	 * @return  string
	 */
	protected function blocks($template)
	{
		// Compile blocks
{'fraction_non_alphanumeric': 0.1772887323943662, 'fraction_numerical': 0.002112676056338028, 'mean_word_length': 3.003523608174771, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2121278', 'n_tokens_mistral': 2014, 'n_tokens_neox': 1806, 'n_words': 429}