<commit_before><?php

namespace App\Lib\SlimVC;

use \App\Lib\SlimVC\PageTemplate as PageTemplate;


class ConfigManager{

	protected $configDir = '../../Config';
	protected $extension = '.php';
	protected $PageTemplate;
	protected $fileNames = array(
		'application',
		'images',
		'menus',
		'sidebars',
		'templates',
		'postTypes',
		'taxonomies'
	);

	protected $defaultImageSize = array(0,0,false);

	/**
	 * construcor. takes a SLimVC object instnace
	 * @param [SlimVC] $parent
	 */
	public function __construct( $parent ){
		$this->parent = $parent;
		$this->PageTemplate = new PageTemplate();
		$this->readConfigDir();
		$this->applyConfiguration();
		$this->initWpHooks();
	}

	/**
	 * reads the configuration directory
	 * and sets the internals per configuration
	 * @return [void]
	 */
	protected function readConfigDir(){
		foreach( $this->fileNames as $file ){
			$path = trailingslashit(dirname(__FILE__)) . trailingslashit($this->configDir) . $file . $this->extension;
			if( file_exists( $path ) ){
				$this->$file = include $path;
			}else{
				$this->$file = array();
			}
		}
	}

	/**<commit_msg>ConfigManager now being cached if APC is enabled (saves about 9-25ms)<commit_after><?php

namespace App\Lib\SlimVC;

use \App\Lib\SlimVC\PageTemplate as PageTemplate;


class ConfigManager{

	/**
	 * default configuration directory
	 * @var string
	 */
	protected $configDir = '../../Config';

	/**
	 * extension of config files
	 * @var string
	 */
	protected $extension = '.php';

	/**
	 * holds a PageTemplate instance
	 * @var [\App\Lib\SlimVC\PageTemplate]
	 */
	protected $PageTemplate;

	/**
	 * filenames which will be included
	 * @var array
	 */
	protected $fileNames = array(
		'application',
		'images',
		'menus',
		'sidebars',
		'templates',
		'postTypes',
		'taxonomies'
	);

	/**
	 * APC specific 
	 * @var boolean
	 */
	protected $APC_ENABLED = false;

	/**
	 * APC namespace-prefix
	 * @var string
	 */
	protected $APC_PREFIX = 'CONFIG_MANAGER_';

	/**
	 * default image size definition array
	 * @var array
	 */
	protected $defaultImageSize = array(0,0,false);

	/**
	 * construcor. takes a SLimVC object instnace
	 * @param [SlimVC] $parent
	 */
	public function __construct( $parent ){
		$this->parent = $parent;

		$this->APC_ENABLED = extension_loaded('apc') && ini_get('apc.enabled');

		$this->PageTemplate = new PageTemplate();
		$this->readConfigDir();
		$this->applyConfiguration();
		$this->initWpHooks();
	}

	/**
	 * reads the configuration directory
	 * and sets the internals per configuration
	 * @return [void]
	 */
	protected function readConfigDir(){

		// remove application conf from filenames
		$strippedFilenames = array_diff($this->fileNames, array('application') );

		// read app conf directly
		$this->application = array_merge( $this->getApplicationDefaults(), $this->read('application') );

		// the rest might be cached if available
		foreach( $strippedFilenames as $file ){

			// apc enabled ?
			if( $this->APC_ENABLED ){
				// fetch from cache if exists
				if( (!apc_exists( $this->APC_PREFIX . 'FILE_' . $file )) && (true !== $this->application['debug']) ){
					$content = $this->read($file);
					apc_add($this->APC_PREFIX . 'FILE_' . $file, $content);
					$this->$file = $content;
				}else{
					$this->$file = apc_fetch($this->APC_PREFIX . 'FILE_' . $file);
				}
			}else{
				$this->$file = $this->read($file);
			}
		}
	}

	/**
	 * reads a file and returns its content
	 * @param  [string] $file
	 * @return [mixed]
	 */
	protected function read($file){
		$path = trailingslashit(dirname(__FILE__)) . trailingslashit($this->configDir) . $file . $this->extension;
		if( file_exists( $path ) ){
			return include $path;
		}else{
			return array();
		}
	}

	/**
	 * application-wide default configuration
	 * @var array
	 */
	protected function getApplicationDefaults(){
		return array(
			'debug' => true,
			'namespace.controller' => '\\App\\Conrollers\\',
			'slim' => array(
				// env vars
				'mode' => 'development',
				'log.enabled' => true,
				'log.writer' => new \App\Lib\SlimVC\Logger(),
				'log.level' => \Slim\Log::DEBUG,

				// view & templating
				'view' => new \Slim\Views\Twig(),
				'templates.path' => dirname(__FILE__) . '/../Views',
			)
		);
	}

	/**
{'fraction_non_alphanumeric': 0.16701902748414377, 'fraction_numerical': 0.001644350481559784, 'mean_word_length': 3.331637843336724, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 44, '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': '4603664', 'n_tokens_mistral': 1594, 'n_tokens_neox': 1395, 'n_words': 331}