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 |
||
| 8 | class BladeProvider |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * Path to a folder view common view can be stored. |
||
| 12 | * |
||
| 13 | * @var string |
||
| 14 | */ |
||
| 15 | protected static $baseViewPath; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Local path to blade cache storage. |
||
| 19 | * |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | protected static $cachePath; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * View factory. |
||
| 26 | * |
||
| 27 | * @var Factory |
||
| 28 | */ |
||
| 29 | protected static $viewFactory; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Service container factory. |
||
| 33 | * |
||
| 34 | * @var Container |
||
| 35 | */ |
||
| 36 | protected static $container; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Register blade engine in Bitrix. |
||
| 40 | * |
||
| 41 | * @param string $baseViewPath |
||
| 42 | * @param string $cachePath |
||
| 43 | */ |
||
| 44 | public static function register($baseViewPath = 'local/views', $cachePath = 'bitrix/cache/blade') |
||
|
|
|||
| 45 | { |
||
| 46 | static::$baseViewPath = $_SERVER['DOCUMENT_ROOT'].'/'.$baseViewPath; |
||
| 47 | static::$cachePath = $_SERVER['DOCUMENT_ROOT'].'/'.$cachePath; |
||
| 48 | |||
| 49 | static::instantiateServiceContainer(); |
||
| 50 | static::instantiateViewFactory(); |
||
| 51 | static::registerBitrixDirectives(); |
||
| 52 | |||
| 53 | global $arCustomTemplateEngines; |
||
| 54 | $arCustomTemplateEngines['blade'] = [ |
||
| 55 | 'templateExt' => ['blade'], |
||
| 56 | 'function' => 'renderBladeTemplate', |
||
| 57 | ]; |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Get view factory. |
||
| 62 | * |
||
| 63 | * @return Factory |
||
| 64 | */ |
||
| 65 | public static function getViewFactory() |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @return BladeCompiler |
||
| 72 | */ |
||
| 73 | public function getCompiler() |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Update paths where blade tries to find additional views. |
||
| 80 | * |
||
| 81 | * @param string $templateDir |
||
| 82 | */ |
||
| 83 | public static function addTemplateFolderToViewPaths($templateDir) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Undo addTemplateFolderToViewPaths |
||
| 106 | * |
||
| 107 | * @param string $templateDir |
||
| 108 | */ |
||
| 109 | public static function removeTemplateFolderFromViewPaths($templateDir) |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Instantiate service container if it's not instantiated yet. |
||
| 124 | */ |
||
| 125 | protected static function instantiateServiceContainer() |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Instantiate view factory. |
||
| 139 | */ |
||
| 140 | protected static function instantiateViewFactory() |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Create dir if it does not exist. |
||
| 158 | * |
||
| 159 | * @param string $path |
||
| 160 | */ |
||
| 161 | protected static function createDirIfNotExist($path) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Register bitrix directives. |
||
| 172 | */ |
||
| 173 | protected static function registerBitrixDirectives() |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @param BladeCompiler $compiler |
||
| 222 | */ |
||
| 223 | private static function registerHermitageDirectives($compiler) |
||
| 261 | } |
||
| 262 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: