|
1 | 1 | <?php
|
| 2 | + |
2 | 3 | /**
|
3 | 4 | * Template Hierarchy should search for .blade.php files
|
4 | 5 | */
|
5 | 6 | array_map(function ($type) {
|
6 | 7 | add_filter("{$type}_template_hierarchy", function ($templates) {
|
7 |
| - return call_user_func_array('array_merge', array_map(function ($template) { |
| 8 | + $result = call_user_func_array('array_merge', array_map(function ($template) { |
8 | 9 | $transforms = [
|
9 | 10 | '%^/?(templates)?/?%' => Bladerunner\Config::repo('bladerunner.disable_option_hack') ? 'templates/' : '',
|
10 | 11 | '%(\.blade)?(\.php)?$%' => ''
|
11 | 12 | ];
|
12 | 13 | $normalizedTemplate = preg_replace(array_keys($transforms), array_values($transforms), $template);
|
| 14 | + |
| 15 | + $controllerPaths = collect([ |
| 16 | + apply_filters('bladerunner/controller/paths', []), |
| 17 | + get_stylesheet_directory() . '/controllers', |
| 18 | + ])->flatMap(function ($path) use ($normalizedTemplate) { |
| 19 | + $controllerPath = "{$path}/{$normalizedTemplate}.php"; |
| 20 | + if (file_exists($controllerPath)) { |
| 21 | + add_filter('bladerunner/controllers/heap', function ($heap) use ($controllerPath) { |
| 22 | + if (!in_array($controllerPath, $heap)) { |
| 23 | + $heap[] = $controllerPath; |
| 24 | + } |
| 25 | + return $heap; |
| 26 | + }); |
| 27 | + } |
| 28 | + return ["{$path}/{$normalizedTemplate}.php"]; |
| 29 | + })->unique()->toArray(); |
| 30 | + |
13 | 31 | return ["{$normalizedTemplate}.blade.php", "{$normalizedTemplate}.php"];
|
14 | 32 | }, $templates));
|
| 33 | + |
| 34 | + |
| 35 | + return $result; |
15 | 36 | });
|
16 | 37 | }, [
|
17 | 38 | 'index', '404', 'archive', 'author', 'category', 'tag', 'taxonomy', 'date', 'home',
|
18 | 39 | 'frontpage', 'page', 'paged', 'search', 'single', 'singular', 'attachment'
|
19 | 40 | ]);
|
| 41 | + |
| 42 | +add_filter('template_include', function ($template) { |
| 43 | + $heap = apply_filters('bladerunner/controllers/heap', []); |
| 44 | + if ($heap) { |
| 45 | + foreach ($heap as $controllerFile) { |
| 46 | + require_once $controllerFile; |
| 47 | + $class = get_declared_classes(); |
| 48 | + $class = '\\' . end($class); |
| 49 | + $controller = new $class(); |
| 50 | + if (is_subclass_of($class, "\\Bladerunner\\Controller") && $controller->__getView()) { |
| 51 | + $controller->__setup(); |
| 52 | + echo view($controller->__getView(), $controller->__getData()); |
| 53 | + return null; |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + return $template; |
| 58 | +}, PHP_INT_MAX); |
| 59 | + |
| 60 | +//TODO: Make the filter bladerunner/controller/path to array as bladerunner/controller/paths to get use in eg plugins |
0 commit comments