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 |
||
| 6 | class Startup{ |
||
| 7 | public static $urlParts; |
||
| 8 | private static $config; |
||
| 9 | private static $ctrlNS; |
||
| 10 | |||
| 11 | public static function run(array &$config,$url){ |
||
| 12 | @set_exception_handler(array('Startup', 'errorHandler')); |
||
| 13 | self::$config=$config; |
||
| 14 | self::startTemplateEngine($config); |
||
| 15 | |||
| 16 | session_start(); |
||
| 17 | |||
| 18 | $u=self::parseUrl($url); |
||
| 19 | |||
| 20 | if(($ru=Router::getRoute($url))!==false){ |
||
| 21 | if(\is_array($ru)) |
||
| 22 | self::runAction($ru); |
||
| 23 | else |
||
| 24 | echo $ru; |
||
| 25 | }else{ |
||
| 26 | self::setCtrlNS($config); |
||
| 27 | $u[0]=self::$ctrlNS.$u[0]; |
||
| 28 | if(class_exists($u[0])){ |
||
| 29 | self::runAction($u); |
||
| 30 | }else{ |
||
| 31 | print "Le contrôleur `".$u[0]."` n'existe pas <br/>"; |
||
| 32 | } |
||
| 33 | } |
||
| 34 | } |
||
| 35 | |||
| 36 | private static function setCtrlNS($config){ |
||
| 37 | $ns=$config["mvcNS"]["controllers"]; |
||
| 38 | if($ns!=="" && $ns!==null){ |
||
| 39 | $ns.="\\"; |
||
| 40 | } |
||
| 41 | self::$ctrlNS=$ns; |
||
| 42 | } |
||
| 43 | |||
| 44 | private static function parseUrl(&$url){ |
||
| 45 | if(!$url){ |
||
| 46 | $url="_default"; |
||
| 47 | } |
||
| 48 | View Code Duplication | if(StrUtils::endswith($url, "/")) |
|
| 49 | $url=substr($url, 0,strlen($url)-1); |
||
| 50 | self::$urlParts=explode("/", $url); |
||
| 51 | |||
| 52 | return self::$urlParts; |
||
| 53 | } |
||
| 54 | |||
| 55 | private static function startTemplateEngine($config){ |
||
| 56 | try { |
||
| 57 | $engineOptions=array('cache' => ROOT.DS."views/cache/"); |
||
| 58 | if(isset($config["templateEngine"])){ |
||
| 59 | $templateEngine=$config["templateEngine"]; |
||
| 60 | if(isset($config["templateEngineOptions"])){ |
||
| 61 | $engineOptions=$config["templateEngineOptions"]; |
||
| 62 | } |
||
| 63 | $engine=new $templateEngine($engineOptions); |
||
| 64 | if ($engine instanceof TemplateEngine){ |
||
| 65 | self::$config["templateEngine"]=$engine; |
||
| 66 | } |
||
| 67 | } |
||
| 68 | } catch (\Exception $e) { |
||
| 69 | echo $e->getTraceAsString(); |
||
| 70 | } |
||
| 71 | } |
||
| 72 | |||
| 73 | public static function runAction($u,$initialize=true,$finalize=true){ |
||
| 74 | $config=self::getConfig(); |
||
| 75 | $ctrl=$u[0]; |
||
| 76 | $controller=new $ctrl(); |
||
| 77 | if(!$controller instanceof Controller){ |
||
| 78 | print "`{$u[0]}` n'est pas une instance de contrôleur.`<br/>"; |
||
| 79 | return; |
||
| 80 | } |
||
| 81 | //Dependency injection |
||
| 82 | if(\array_key_exists("di", $config)){ |
||
| 83 | $di=$config["di"]; |
||
| 84 | if(\is_array($di)){ |
||
| 85 | foreach ($di as $k=>$v){ |
||
| 86 | $controller->$k=$v(); |
||
| 87 | } |
||
| 88 | } |
||
| 89 | } |
||
| 90 | |||
| 91 | if($initialize) |
||
| 92 | $controller->initialize(); |
||
| 93 | self::callController($controller,$u); |
||
| 94 | if($finalize) |
||
| 95 | $controller->finalize(); |
||
| 96 | } |
||
| 97 | |||
| 98 | public static function runAsString($u,$initialize=true,$finalize=true){ |
||
| 99 | \ob_start(); |
||
| 100 | self::runAction($u,$initialize,$finalize); |
||
| 101 | return \ob_get_clean(); |
||
| 102 | } |
||
| 103 | |||
| 104 | private static function callController(Controller $controller,$u){ |
||
| 129 | |||
| 130 | public static function getConfig(){ |
||
| 133 | |||
| 134 | public static function errorHandler($severity, $message, $filename, $lineno) { |
||
| 142 | } |
||
| 143 |