This repository has been archived by the owner on Jan 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 7afbbfb
Showing
6 changed files
with
445 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace Kirby\Modules; | ||
|
||
// Redirect to the page where the module appears | ||
if($page->parent()->uid() === Modules::uid()) { | ||
go($page->parent()->parent()); | ||
} else { | ||
go($page->parent()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
namespace Kirby\Modules; | ||
|
||
// Kirby dependencies | ||
use Error; | ||
use Obj; | ||
use Page; | ||
use Str; | ||
|
||
/** | ||
* Module | ||
* | ||
* @package Kirby Modules Plugin | ||
* @author Lukas Bestle <lukas@getkirby.com> | ||
* @license MIT | ||
*/ | ||
class Module extends Obj { | ||
public $name; | ||
public $template; | ||
public $blueprintFile; | ||
public $snippetFile; | ||
|
||
/** | ||
* Class constructor | ||
* | ||
* @param string $name Name of the module directory or module page | ||
*/ | ||
public function __construct($name) { | ||
$templatePrefix = Modules::templatePrefix(); | ||
|
||
// Get the module name from the template if a page is given | ||
if(is_a($name, 'Page')) { | ||
// Validate that the page is a module | ||
if(!str::startsWith($name->intendedTemplate(), $templatePrefix)) { | ||
throw new Error('The given page is no module.'); | ||
} | ||
|
||
$prefixLength = str::length($templatePrefix); | ||
$name = str::substr($name->intendedTemplate(), $prefixLength); | ||
} | ||
|
||
$this->name = $name; | ||
$this->template = $templatePrefix . $name; | ||
|
||
// Store the file paths of the module | ||
$basePath = Modules::directory() . DS . $name; | ||
$this->blueprintFile = $basePath . DS . $name . '.yml'; | ||
$this->snippetFile = $basePath . DS . $name . '.html.php'; | ||
} | ||
|
||
/** | ||
* Validates if the module is valid and active | ||
* | ||
* @return boolean | ||
*/ | ||
public function validate() { | ||
return is_file($this->blueprintFile) && is_file($this->snippetFile); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
<?php | ||
|
||
namespace Kirby\Modules; | ||
|
||
// Kirby dependencies | ||
use C; | ||
use Dir; | ||
use Error; | ||
use Str; | ||
use Tpl; | ||
|
||
/** | ||
* Modules | ||
* | ||
* @package Kirby Modules Plugin | ||
* @author Lukas Bestle <lukas@getkirby.com> | ||
* @license MIT | ||
*/ | ||
class Modules { | ||
protected $page; | ||
|
||
// Caches | ||
protected $modules; | ||
protected static $allModules; | ||
|
||
/** | ||
* Class constructor | ||
* | ||
* @param Page $page Kirby page that contains modules | ||
*/ | ||
public function __construct($page) { | ||
$this->page = $page; | ||
} | ||
|
||
/** | ||
* Outputs all modules of the given page | ||
* | ||
* @param array $data Optional additional data to pass to each module | ||
* @param boolean $return Whether to output or return the module string | ||
* @return string | ||
*/ | ||
public function output($data = array(), $return = false) { | ||
$result = ''; | ||
|
||
// Loop through all valid modules in the file system order | ||
foreach($this->modules() as $module) { | ||
$moduleObj = new Module($module); | ||
$moduleName = $moduleObj->name(); | ||
|
||
// Use the additional data but make sure that $module and $moduleName always win | ||
$moduleData = array_merge($data, compact('module', 'moduleName')); | ||
$result .= tpl::load($moduleObj->snippetFile(), $moduleData, true); | ||
} | ||
|
||
if($return) return $result; | ||
echo $result; | ||
} | ||
|
||
/** | ||
* Returns a collection of module subpages of the given page | ||
* | ||
* @return Pages | ||
*/ | ||
public function modules() { | ||
// Return from cache if possible | ||
if($this->modules) return $this->modules; | ||
|
||
// Determine where the modules live | ||
if($childPage = $this->page->find(static::parentUid())) { | ||
// Modules child page exists, use its children | ||
$modules = $childPage->children(); | ||
} else { | ||
// Try to use the direct subpages (filtered below) | ||
$modules = $this->page->children(); | ||
} | ||
|
||
// Filter the modules by visibility and valid module | ||
$modules = $modules->visible()->filter(function($page) { | ||
try { | ||
$module = new Module($page); | ||
return $module->validate(); | ||
} catch(Error $e) { | ||
return false; | ||
} | ||
}); | ||
|
||
return $this->modules = $modules; | ||
} | ||
|
||
/** | ||
* Registers the page method and all blueprints within Kirby | ||
* Called only once when the plugin is loaded | ||
*/ | ||
public static function register() { | ||
$kirby = kirby(); | ||
|
||
// Register $page->modules($data, $return) method | ||
// Calling it will call the modules() helper | ||
$kirby->set('page::method', 'modules', 'modules'); | ||
|
||
// Register blueprints and dummy templates for all modules | ||
foreach(static::allModules() as $module) { | ||
$kirby->set('blueprint', $module->template(), $module->blueprintFile()); | ||
$kirby->set('template', $module->template(), dirname(__DIR__) . DS . 'etc' . DS . 'template.php'); | ||
} | ||
} | ||
|
||
/** | ||
* Returns the base directory for the modules | ||
* Can be changed with the modules.directory option | ||
* | ||
* @return string | ||
*/ | ||
public static function directory() { | ||
return c::get('modules.directory', kirby()->roots()->site() . DS . 'modules'); | ||
} | ||
|
||
/** | ||
* Returns the UID of the modules pages | ||
* Can be changed with the modules.parent.uid option | ||
* | ||
* @return string | ||
*/ | ||
public static function parentUid() { | ||
return c::get('modules.parent.uid', 'modules'); | ||
} | ||
|
||
/** | ||
* Returns the template prefix for modules | ||
* Can be changed with the modules.template.prefix option | ||
* | ||
* @return string | ||
*/ | ||
public static function templatePrefix() { | ||
return c::get('modules.template.prefix', 'module.'); | ||
} | ||
|
||
/** | ||
* Returns an array of all Module objects | ||
* | ||
* @return array | ||
*/ | ||
public static function allModules() { | ||
// Return from cache if possible | ||
if(static::$allModules) return static::$allModules; | ||
$allModules = array(); | ||
|
||
// Read the modules directory | ||
$basePath = static::directory(); | ||
foreach(dir::read($basePath) as $name) { | ||
$path = $basePath . DS . $name; | ||
if(!is_dir($path)) continue; | ||
|
||
$module = new Module($name); | ||
if($module->validate()) $allModules[$name] = $module; | ||
} | ||
|
||
return static::$allModules = $allModules; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
/** | ||
* Kirby Modules Plugin | ||
* | ||
* @author Lukas Bestle <lukas@getkirby.com> | ||
*/ | ||
|
||
// Define autoloader | ||
load(array( | ||
'kirby\\modules\\modules' => __DIR__ . DS . 'lib' . DS . 'modules.php', | ||
'kirby\\modules\\module' => __DIR__ . DS . 'lib' . DS . 'module.php', | ||
)); | ||
|
||
// Register page method and blueprints | ||
Kirby\Modules\Modules::register(); | ||
|
||
/** | ||
* Helper function to output modules for a given page | ||
* You can also use $page->modules($data, $return) | ||
* | ||
* @param Page $page Kirby page that contains modules | ||
* @param array $data Optional additional data to pass to each module | ||
* @param boolean $return Whether to output or return the module string | ||
* @return string | ||
*/ | ||
function modules($page, $data = array(), $return = false) { | ||
$modules = new Kirby\Modules\Modules($page); | ||
return $modules->output($data, $return); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"name": "modules", | ||
"description": "Kirby Modules Plugin", | ||
"author": "Lukas Bestle <lukas@getkirby.com>", | ||
"license": "MIT", | ||
"version": "1.0.0", | ||
"type": "kirby-plugin" | ||
} |
Oops, something went wrong.