public static function load($file) { if (File::exists($file)) { $config = (include $file); static::$items = arrayMergeRecursive(static::$items, $config); } else { $file = APPLICATION_PATH . DS . '..' . DS . 'public' . DS . 'content' . DS . SITE_NAME . DS . 'config' . DS . $file . '.php'; if (File::exists($file)) { $config = (include $file); static::$items = arrayMergeRecursive(static::$items, $config); } } }
/** * Load the config.php configuration file an store it into the configurations list * * If a default configuration is set into the loaded configuration file, current configuration is * switched to this configuration. * * @param $file_name string * @return Configuration */ public function load($file_name = 'config.php') { $config = []; /** @noinspection PhpIncludeInspection */ include $file_name; $configurations = []; foreach ($config as $config_name => $config_options) { if (isset($config_options['extends'])) { $extends_array = is_array($config_options['extends']) ? $config_options['extends'] : [$config_options['extends']]; unset($config_options['extends']); foreach ($extends_array as $extends) { $config_options = arrayMergeRecursive($configurations[$extends], $config_options); } } if (!isset($config_options['app'])) { $config_options['app'] = $config_name; } if (!isset($config_options['author'])) { $config_options['author'] = 'SAF'; } $configurations[$config_name] = $config_options; } $this->configurations = []; foreach ($configurations as $config_name => $config_options) { $removed = array_flip($config_options[Priority::REMOVE]); unset($config_options[Priority::REMOVE]); foreach ($config_options as $level => $plugins) { if (is_array($plugins)) { $plugins_configurations = []; foreach ($plugins as $class_name => $plugin_configuration) { if (!isset($removed[$class_name])) { if (is_numeric($class_name)) { $class_name = $plugin_configuration; $plugin_configuration = []; } $plugins_configurations[$class_name] = $plugin_configuration; } } $config_options[$level] = $plugins_configurations; } } $this->configurations[$config_name] = new Configuration($config_name, $config_options); } return end($this->configurations); }
/** * @param $class Reflection_Class * @return boolean */ public function compileClass(Reflection_Class $class) { $this->compiled_classes[$class->name] = true; if (self::DEBUG) { echo '<h2>' . $class->name . '</h2>'; } $methods = []; $properties = []; if ($class->type !== T_INTERFACE) { // implements : _read_property, _write_property if ($class->type !== T_TRAIT) { $this->scanForImplements($properties, $class); } // read/write : __aop, __construct, __get, __isset, __set, __unset $this->scanForGetters($properties, $class); $this->scanForLinks($properties, $class); $this->scanForSetters($properties, $class); $this->scanForReplaces($properties, $class); $this->scanForAbstract($methods, $class); // TODO should be done for all classes before compiling : it creates links in other classes //$this->scanForMethods($methods, $class); } list($methods2, $properties2) = $this->getPointcuts($class->name); $methods = arrayMergeRecursive($methods, $methods2); $properties = arrayMergeRecursive($properties, $properties2); $methods_code = []; if (self::DEBUG && $properties) { echo '<pre>properties = ' . print_r($properties, true) . '</pre>'; } if ($properties) { $properties_compiler = new Compiler\Properties($class); $methods_code = $properties_compiler->compile($properties); } if (self::DEBUG && $methods) { echo '<pre>methods = ' . print_r($methods, true) . '</pre>'; } $method_compiler = new Compiler\Method($class); foreach ($methods as $method_name => $advices) { if ($compiled_method = $method_compiler->compile($method_name, $advices)) { $methods_code[$method_name] = $compiled_method; } } if ($methods_code) { ksort($methods_code); if (self::DEBUG && $methods_code) { echo '<pre>' . print_r($methods_code, true) . '</pre>'; } $class->source->setSource(substr(trim($class->source->getSource()), 0, -1) . TAB . '//' . str_repeat('#', 91) . ' AOP' . LF . join('', $methods_code) . LF . '}' . LF); } return boolval($methods_code); }
function arrayMergeRecursive($firstArray, $secondArray) { if (Arrays::is($firstArray) && Arrays::is($secondArray)) { foreach ($secondArray as $key => $value) { if (isset($firstArray[$key])) { $firstArray[$key] = arrayMergeRecursive($firstArray[$key], $value); } else { if ($key === 0) { $firstArray = array(0 => arrayMergeRecursive($firstArray, $value)); } else { $firstArray[$key] = $value; } } } } else { $firstArray = $secondArray; } return $firstArray; }
<?php namespace Thin; /* GENERAL */ $config = array('database' => array('adapter' => 'mysql', 'host' => 'localhost', 'username' => 'root', 'password' => 'root', 'dbname' => 'ajf', 'port' => 3306, 'driver' => 'pdo_mysql', 'proxy_namespace' => 'ThinDoctrine', 'proxy_dir' => APPLICATION_PATH . DS . 'models' . DS . 'Doctrine' . DS . 'Proxies'), 'application' => array('plugins_dir' => __DIR__ . '/../../app/plugins/', 'helpers_dir' => __DIR__ . '/../../app/plugins/', 'library_dir' => __DIR__ . '/../../app/lib/', 'view' => array('cache' => true, 'cacheTtl' => 7200)), 'mailer' => array('host' => 'smtp.mandrillapp.com', 'login' => '*****@*****.**', 'password' => 'BT99CIkPBCsoWX5otYpo9g', 'port' => 587, 'secure' => null, 'auth' => true, 'debug' => false), 'mongo' => array('hostname' => 'localhost', 'replicaSet' => false, 'db' => SITE_NAME)); /* PRODUCTION ENVIRONMENT */ $production = array(); /* TESTING ENVIRONMENT */ $testing = array(); /* DEVELOPMENT ENVIRONMENT */ $development = array('application' => array('view' => array('cache' => false))); /* LOCAL ENVIRONMENT */ $local = File::exists(__DIR__ . DS . 'local.php') ? include __DIR__ . DS . 'local.php' : array(); $config = arrayMergeRecursive($config, $local); $application = new Container(); $configEnv = Config::get('application.env', APPLICATION_ENV); $configEnv = ${$configEnv}; $config = arrayMergeRecursive($config, $configEnv); return $application->populate($config);
/** * Unshifts a parameter at beginning of the parameters array * * @param $parameter_value mixed */ public function unshift($parameter_value) { if (is_object($parameter_value)) { $class_name = get_class($parameter_value); if (isset($this->parameters[$class_name])) { unset($this->parameters[$class_name]); } $this->parameters = arrayMergeRecursive([get_class($parameter_value) => $parameter_value], $this->parameters); } else { $this->unshiftUnnamed($parameter_value); } }
/** * Gets all dependencies full classes names * - Looks for them using namespace 'use', classes 'extends', 'implements' and 'use' * * @param $instantiates boolean if true, searches for '::class' and 'new' too * @return Dependency[] */ public function getDependencies($instantiates = false) { if (!isset($this->dependencies) || $instantiates && !isset($this->instantiates)) { $filters = []; if ($instantiates && !isset($this->instantiates)) { $filters[] = self::INSTANTIATES; } if (!isset($this->dependencies)) { $filters[] = self::DEPENDENCIES; } if (!isset($this->namespaces)) { $filters[] = self::NAMESPACES; } if (!isset($this->requires)) { $filters[] = self::REQUIRES; } if (!isset($this->use)) { $filters[] = self::USES; } $this->get($filters); } return $instantiates ? arrayMergeRecursive($this->dependencies, $this->instantiates) : $this->dependencies; }
/** * Merges two arrays, with recursion * * Elements of $array1 and $array2 with same index (even if numeric) : $array2 element replaces $array1 element. * If $array2 element is an array : merge $array1 and $array2 array element, recursively. * * @param $array1 array * @param $array2 array * @return array */ function arrayMergeRecursive($array1, $array2) { foreach ($array2 as $index => $value2) { if ($index === ':' && $value2 === 'clear') { $array1 = null; unset($array2[$index]); } else { $value1 = isset($array1[$index]) ? $array1[$index] : null; if (is_numeric($index) && !is_array($value1) && !is_array($value2)) { if (!in_array($value2, $array1)) { $array1[] = $value2; } } elseif (is_array($value2)) { $value2 = arrayMergeRecursive(is_array($value1) ? $value1 : [], $value2); if (isset($value2)) { $array1[$index] = $value2; } else { unset($array1[$index]); } } else { $array1[$index] = $value2; } } } return $array1; }