|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Kirby\Modules; |
| 4 | + |
| 5 | +// Kirby dependencies |
| 6 | +use C; |
| 7 | +use Dir; |
| 8 | +use Error; |
| 9 | +use Str; |
| 10 | +use Tpl; |
| 11 | + |
| 12 | +/** |
| 13 | + * Modules |
| 14 | + * |
| 15 | + * @package Kirby Modules Plugin |
| 16 | + * @author Lukas Bestle <[email protected]> |
| 17 | + * @license MIT |
| 18 | + */ |
| 19 | +class Modules { |
| 20 | + protected $page; |
| 21 | + |
| 22 | + // Caches |
| 23 | + protected $modules; |
| 24 | + protected static $allModules; |
| 25 | + |
| 26 | + /** |
| 27 | + * Class constructor |
| 28 | + * |
| 29 | + * @param Page $page Kirby page that contains modules |
| 30 | + */ |
| 31 | + public function __construct($page) { |
| 32 | + $this->page = $page; |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Outputs all modules of the given page |
| 37 | + * |
| 38 | + * @param array $data Optional additional data to pass to each module |
| 39 | + * @param boolean $return Whether to output or return the module string |
| 40 | + * @return string |
| 41 | + */ |
| 42 | + public function output($data = array(), $return = false) { |
| 43 | + $result = ''; |
| 44 | + |
| 45 | + // Loop through all valid modules in the file system order |
| 46 | + foreach($this->modules() as $module) { |
| 47 | + $moduleObj = new Module($module); |
| 48 | + $moduleName = $moduleObj->name(); |
| 49 | + |
| 50 | + // Use the additional data but make sure that $module and $moduleName always win |
| 51 | + $moduleData = array_merge($data, compact('module', 'moduleName')); |
| 52 | + $result .= tpl::load($moduleObj->snippetFile(), $moduleData, true); |
| 53 | + } |
| 54 | + |
| 55 | + if($return) return $result; |
| 56 | + echo $result; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Returns a collection of module subpages of the given page |
| 61 | + * |
| 62 | + * @return Pages |
| 63 | + */ |
| 64 | + public function modules() { |
| 65 | + // Return from cache if possible |
| 66 | + if($this->modules) return $this->modules; |
| 67 | + |
| 68 | + // Determine where the modules live |
| 69 | + if($childPage = $this->page->find(static::parentUid())) { |
| 70 | + // Modules child page exists, use its children |
| 71 | + $modules = $childPage->children(); |
| 72 | + } else { |
| 73 | + // Try to use the direct subpages (filtered below) |
| 74 | + $modules = $this->page->children(); |
| 75 | + } |
| 76 | + |
| 77 | + // Filter the modules by visibility and valid module |
| 78 | + $modules = $modules->visible()->filter(function($page) { |
| 79 | + try { |
| 80 | + $module = new Module($page); |
| 81 | + return $module->validate(); |
| 82 | + } catch(Error $e) { |
| 83 | + return false; |
| 84 | + } |
| 85 | + }); |
| 86 | + |
| 87 | + return $this->modules = $modules; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Registers the page method and all blueprints within Kirby |
| 92 | + * Called only once when the plugin is loaded |
| 93 | + */ |
| 94 | + public static function register() { |
| 95 | + $kirby = kirby(); |
| 96 | + |
| 97 | + // Register $page->modules($data, $return) method |
| 98 | + // Calling it will call the modules() helper |
| 99 | + $kirby->set('page::method', 'modules', 'modules'); |
| 100 | + |
| 101 | + // Register blueprints and dummy templates for all modules |
| 102 | + foreach(static::allModules() as $module) { |
| 103 | + $kirby->set('blueprint', $module->template(), $module->blueprintFile()); |
| 104 | + $kirby->set('template', $module->template(), dirname(__DIR__) . DS . 'etc' . DS . 'template.php'); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Returns the base directory for the modules |
| 110 | + * Can be changed with the modules.directory option |
| 111 | + * |
| 112 | + * @return string |
| 113 | + */ |
| 114 | + public static function directory() { |
| 115 | + return c::get('modules.directory', kirby()->roots()->site() . DS . 'modules'); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Returns the UID of the modules pages |
| 120 | + * Can be changed with the modules.parent.uid option |
| 121 | + * |
| 122 | + * @return string |
| 123 | + */ |
| 124 | + public static function parentUid() { |
| 125 | + return c::get('modules.parent.uid', 'modules'); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Returns the template prefix for modules |
| 130 | + * Can be changed with the modules.template.prefix option |
| 131 | + * |
| 132 | + * @return string |
| 133 | + */ |
| 134 | + public static function templatePrefix() { |
| 135 | + return c::get('modules.template.prefix', 'module.'); |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Returns an array of all Module objects |
| 140 | + * |
| 141 | + * @return array |
| 142 | + */ |
| 143 | + public static function allModules() { |
| 144 | + // Return from cache if possible |
| 145 | + if(static::$allModules) return static::$allModules; |
| 146 | + $allModules = array(); |
| 147 | + |
| 148 | + // Read the modules directory |
| 149 | + $basePath = static::directory(); |
| 150 | + foreach(dir::read($basePath) as $name) { |
| 151 | + $path = $basePath . DS . $name; |
| 152 | + if(!is_dir($path)) continue; |
| 153 | + |
| 154 | + $module = new Module($name); |
| 155 | + if($module->validate()) $allModules[$name] = $module; |
| 156 | + } |
| 157 | + |
| 158 | + return static::$allModules = $allModules; |
| 159 | + } |
| 160 | +} |
0 commit comments