Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 18 | class Suricate |
||
| 19 | { |
||
| 20 | |||
| 21 | const VERSION = '0.1.7'; |
||
| 22 | |||
| 23 | const CONF_DIR = '/conf/'; |
||
| 24 | |||
| 25 | protected $router; |
||
| 26 | |||
| 27 | private $config; |
||
| 28 | private $configFile; |
||
| 29 | |||
| 30 | private $useAutoloader = false; |
||
| 31 | |||
| 32 | private static $servicesContainer; |
||
| 33 | private static $servicesRepository; |
||
| 34 | |||
| 35 | private $servicesList = array( |
||
| 36 | 'Logger' => '\Suricate\Logger', |
||
| 37 | 'App' => '\Suricate\App', |
||
| 38 | 'I18n' => '\Suricate\I18n', |
||
| 39 | 'Error' => '\Suricate\Error', |
||
| 40 | 'Router' => '\Suricate\Router', |
||
| 41 | 'Request' => '\Suricate\Request', |
||
| 42 | 'Database' => '\Suricate\Database', |
||
| 43 | 'Cache' => '\Suricate\Cache', |
||
| 44 | 'CacheMemcache' => '\Suricate\Cache\Memcache', |
||
| 45 | 'CacheApc' => '\Suricate\Cache\Apc', |
||
| 46 | 'CacheFile' => '\Suricate\Cache\File', |
||
| 47 | 'Curl' => '\Suricate\Curl', |
||
| 48 | 'Response' => '\Suricate\Request', |
||
| 49 | 'Session' => '\Suricate\Session', |
||
| 50 | 'SessionNative' => '\Suricate\Session\Native', |
||
| 51 | 'SessionCookie' => '\Suricate\Session\Cookie', |
||
| 52 | 'SessionMemcache' => '\Suricate\Session\Memcache', |
||
| 53 | ); |
||
| 54 | |||
| 55 | |||
| 56 | public function __construct($paths = array(), $configFile = null) |
||
| 57 | { |
||
| 58 | if ($configFile !== null) { |
||
| 59 | $this->setConfigFile($configFile); |
||
| 60 | } |
||
| 61 | |||
| 62 | // Load helpers |
||
| 63 | require_once __DIR__ . DIRECTORY_SEPARATOR . 'Helper.php'; |
||
| 64 | |||
| 65 | $this->loadConfig(); |
||
| 66 | $this->setAppPaths($paths); |
||
| 67 | |||
| 68 | if ($this->useAutoloader) { |
||
| 69 | // Configure autoloader |
||
| 70 | require_once __DIR__ . DIRECTORY_SEPARATOR . 'AutoLoader.php'; |
||
| 71 | AutoLoader::register(); |
||
| 72 | } |
||
| 73 | |||
| 74 | // Define error handler |
||
| 75 | set_exception_handler(array('\Suricate\Error', 'handleException')); |
||
| 76 | set_error_handler(array('\Suricate\Error', 'handleError')); |
||
| 77 | register_shutdown_function(array('\Suricate\Error', 'handleShutdownError')); |
||
| 78 | |||
| 79 | self::$servicesRepository = new Container(); |
||
| 80 | |||
| 81 | $this->initServices(); |
||
| 82 | } |
||
| 83 | |||
| 84 | private function setAppPaths($paths = array()) |
||
| 92 | /** |
||
| 93 | * Initialize Framework services |
||
| 94 | * @return null |
||
| 95 | */ |
||
| 96 | private function initServices() |
||
| 97 | { |
||
| 98 | self::$servicesRepository->setWarehouse($this->servicesList); |
||
| 99 | |||
| 100 | self::$servicesRepository['Request']->parse(); |
||
| 101 | if (isset($this->config['App']['locale'])) { |
||
| 102 | $this->config['I18n'] = array('locale' => $this->config['App']['locale']); |
||
| 103 | } |
||
| 104 | // first sync, && init, dependency to Suricate::request |
||
| 105 | self::$servicesContainer = clone self::$servicesRepository; |
||
| 106 | |||
| 107 | foreach (array_keys($this->servicesList) as $serviceName) { |
||
| 108 | if (isset($this->config[$serviceName])) { |
||
| 109 | self::$servicesRepository[$serviceName]->configure($this->config[$serviceName]); |
||
| 110 | |||
| 111 | /** |
||
| 112 | TODO : remove sync in service creation |
||
| 113 | */ |
||
| 114 | self::$servicesContainer = clone self::$servicesRepository; |
||
| 115 | } |
||
| 116 | } |
||
| 117 | |||
| 118 | if (isset($this->config['Constants'])) { |
||
| 119 | foreach ($this->config['Constants'] as $constantName => $constantValue) { |
||
| 120 | $constantName = strtoupper($constantName); |
||
| 121 | define($constantName, $constantValue); |
||
| 122 | } |
||
| 123 | } |
||
| 124 | |||
| 125 | // final sync, repository is complete |
||
| 126 | self::$servicesContainer = clone self::$servicesRepository; |
||
| 127 | } |
||
| 128 | |||
| 129 | private function setConfigFile($configFile) |
||
| 141 | /** |
||
| 142 | * Load framework configuration from ini file |
||
| 143 | * @return null |
||
| 144 | */ |
||
| 145 | private function loadConfig() |
||
| 180 | |||
| 181 | private function configureAppMode() |
||
| 234 | /** |
||
| 235 | * Default setup template |
||
| 236 | * @return array setup |
||
| 237 | */ |
||
| 238 | private function getDefaultConfig() |
||
| 249 | |||
| 250 | public function run() |
||
| 254 | |||
| 255 | 1 | public static function __callStatic($name, $arguments) |
|
| 263 | } |
||
| 264 |
On one hand,
evalmight be exploited by malicious users if they somehow manage to inject dynamic content. On the other hand, with the emergence of faster PHP runtimes like the HHVM,evalprevents some optimization that they perform.