<commit_before><?php

namespace Zend\Expressive\Phptal;

use ArrayObject;
use DirectoryIterator;
use Interop\Container\ContainerInterface;
use PHPTAL;
use PHPTAL_PreFilter_Compress;
use PHPTAL_PreFilter_StripComments;

use PHPTAL_TalesRegistry;
use Zend\Expressive\Phptal\Tales\Helper as TalesHelper;
use Zend\Expressive\Phptal\HelperManager;
use Zend\Expressive\Phptal\Helper;

/**
 * Create and return a PHPTAL template instance.
 *
 * Optionally uses the service 'config', which should return an array. This
 * factory consumes the following structure:
 *
 * <code>
 * 'debug' => boolean,
 * 'templates' => [
 *     'extension' => 'file extension used by templates; defaults to html',
 *     'paths' => [
 *         // Paths may be strings or arrays of string paths.
 *     ],
 * ],
 * 'phptal' => [
 *     'cache_dir' => 'path to cached templates',
 *     // if enabled, delete all template cache files before processing
 *     'cache_purge_mode' => boolean,
 *     // set how long compiled templates and phptal:cache files are kept; in days 
 *     'cache_lifetime' => 30,
 *     'encoding' => 'set input and ouput encoding; defaults to UTF-8',
 *     // set whitespace compression mode
 *     'compress_whitespace' => boolean,
 *     // strip all html comments
 *     'strip_comments' => boolean,
 * ],
 * </code>
 *
 * Note: the various keys in the `phptal` configuration key can occur in either
 * that location, or under `templates`;
 * the two arrays are merged by the factory.
 */
class PhptalRendererFactory
{
    /**
     * @param ContainerInterface $container
     * @return PhptalRenderer
     */
    public function __invoke(ContainerInterface $container)
    {
        $config = $container->has('config') ? $container->get('config') : [];
        
        if (! is_array($config) && ! $config instanceof ArrayObject) {
            throw new Exception\InvalidConfigException(sprintf(
                '"config" service must be an array or ArrayObject for the %s to be able to consume it; received %s',
                __CLASS__,
                (is_object($config) ? get_class($config) : gettype($config))
            ));
        }
        
        // Set debug mode
        $debug = array_key_exists('debug', $config) ? (bool) $config['debug'] : false;
        $config = $this->mergeConfig($config);
        
        // Create the engine instance:
        $engine = new PHPTAL();
        
        // Set file extension
        $extension = isset($config['extension']) ? $config['extension'] : 'html';
        
        // Change the compiled code destination if set in the config
        if (isset($config['cache_dir'])) {
            $engine->setPhpCodeDestination($config['cache_dir']);
        }
        
        // Configure the encoding
        if (isset($config['encoding'])) {
            $engine->setEncoding($config['encoding']);
        }
        
        // Set template repositories
        if (isset($config['paths'])) {
            $engine->setTemplateRepository($config['paths']);
        }
        
        // Configure cache lifetime
        if (isset($config['cache_lifetime'])) {
            $engine->setCacheLifetime($config['cache_lifetime']);
        }
        
        // If purging of the tal template cache is enabled
		// find all template cache files and delete them
        $cachePurgeMode = isset($config['cache_purge_mode']) ? (bool) $config['cache_purge_mode'] : false;
        
        if ($cachePurgeMode) {
			$cacheFolder = $engine->getPhpCodeDestination();
			if (is_dir($cacheFolder)) {
				foreach (new DirectoryIterator($cacheFolder) as $cacheItem) {
					if (strncmp($cacheItem->getFilename(), 'tpl_', 4) != 0 || $cacheItem->isdir()) {
						continue;
					}
					@unlink($cacheItem->getPathname());
				}
			}
		}
        
        // Configure the whitespace compression mode
        $compressWhitespace = isset($config['compress_whitespace']) 
            ? (bool) $config['compress_whitespace'] 
            : false;
        
        if ($compressWhitespace) {
            $engine->addPreFilter(new PHPTAL_PreFilter_Compress());
        }
        
        // Strip html comments and compress un-needed whitespace
        $stripComments = isset($config['strip_comments']) 
            ? (bool) $config['strip_comments'] 
            : false;<commit_msg>Added new configuration option 'output_mode'<commit_after><?php

namespace Zend\Expressive\Phptal;

use ArrayObject;
use DirectoryIterator;
use Interop\Container\ContainerInterface;
use PHPTAL;
use PHPTAL_PreFilter_Compress;
use PHPTAL_PreFilter_StripComments;

use PHPTAL_TalesRegistry;
use Zend\Expressive\Phptal\Tales\Helper as TalesHelper;
use Zend\Expressive\Phptal\HelperManager;
use Zend\Expressive\Phptal\Helper;

/**
 * Create and return a PHPTAL template instance.
 *
 * Optionally uses the service 'config', which should return an array. This
 * factory consumes the following structure:
 *
 * <code>
 * 'debug' => boolean,
 * 'templates' => [
 *     'extension' => 'file extension used by templates; defaults to html',
 *     'paths' => [
 *         // Paths may be strings or arrays of string paths.
 *     ],
 * ],
 * 'phptal' => [
 *     'cache_dir' => 'path to cached templates',
 *     // if enabled, delete all template cache files before processing
 *     'cache_purge_mode' => boolean,
 *     // set how long compiled templates and phptal:cache files are kept; in days 
 *     'cache_lifetime' => 30,
 *     'encoding' => 'set input and ouput encoding; defaults to UTF-8',
 *     // one of the predefined constant: PHPTAL::HTML5,  PHPTAL::XML, PHPTAL::XHTML
 *     'output_mode' => PHPTAL::HTML5,
 *     // set whitespace compression mode
 *     'compress_whitespace' => boolean,
 *     // strip all html comments
 *     'strip_comments' => boolean,
 * ],
 * </code>
 *
 * Note: the various keys in the `phptal` configuration key can occur in either
 * that location, or under `templates`;
 * the two arrays are merged by the factory.
 */
class PhptalRendererFactory
{
    /**
     * @param ContainerInterface $container
     * @return PhptalRenderer
     */
    public function __invoke(ContainerInterface $container)
    {
        $config = $container->has('config') ? $container->get('config') : [];
        
        if (! is_array($config) && ! $config instanceof ArrayObject) {
            throw new Exception\InvalidConfigException(sprintf(
                '"config" service must be an array or ArrayObject for the %s to be able to consume it; received %s',
                __CLASS__,
                (is_object($config) ? get_class($config) : gettype($config))
            ));
        }
        
        // Set debug mode
        $debug = array_key_exists('debug', $config) ? (bool) $config['debug'] : false;
        $config = $this->mergeConfig($config);
        
        // Create the engine instance:
        $engine = new PHPTAL();
        
        // Set file extension
        $extension = isset($config['extension']) ? $config['extension'] : 'html';
        
        // Change the compiled code destination if set in the config
        if (isset($config['cache_dir'])) {
            $engine->setPhpCodeDestination($config['cache_dir']);
        }
        
        // Configure the encoding
        if (isset($config['encoding'])) {
            $engine->setEncoding($config['encoding']);
        }
        
        // Configure the output mode
        $outputMode = isset($config['output_mode']) ? $config['output_mode'] : PHPTAL::HTML5;
        $engine->setOutputMode($outputMode);
        
        // Set template repositories
        if (isset($config['paths'])) {
            $engine->setTemplateRepository($config['paths']);
        }
        
        // Configure cache lifetime
        if (isset($config['cache_lifetime'])) {
            $engine->setCacheLifetime($config['cache_lifetime']);
        }
        
        // If purging of the tal template cache is enabled
		// find all template cache files and delete them
        $cachePurgeMode = isset($config['cache_purge_mode']) ? (bool) $config['cache_purge_mode'] : false;
        
        if ($cachePurgeMode) {
            $cacheFolder = $engine->getPhpCodeDestination();
            if (is_dir($cacheFolder)) {
                foreach (new DirectoryIterator($cacheFolder) as $cacheItem) {
                    if (strncmp($cacheItem->getFilename(), 'tpl_', 4) != 0 || $cacheItem->isdir()) {
                        continue;
                    }
                    @unlink($cacheItem->getPathname());
                }
            }
        }

        // Configure the whitespace compression mode
        $compressWhitespace = isset($config['compress_whitespace']) 
            ? (bool) $config['compress_whitespace'] 
            : false;
        
        if ($compressWhitespace) {
            $engine->addPreFilter(new PHPTAL_PreFilter_Compress());
        }
        
        // Strip html comments and compress un-needed whitespace
        $stripComments = isset($config['strip_comments']) 
            ? (bool) $config['strip_comments'] 
            : false;
{'fraction_non_alphanumeric': 0.12433862433862433, 'fraction_numerical': 0.0014329805996472662, 'mean_word_length': 2.440652256351915, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1337103', 'n_tokens_mistral': 2555, 'n_tokens_neox': 2478, 'n_words': 658}