Last active
August 29, 2015 14:14
-
-
Save unstoppablecarl/9c478e968b0ad60649f6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<?php namespace Herbert\Framework; | |
/** | |
* Class Plugin | |
* @package Herbert\Framework | |
* @function Message $message | |
* @function Controller $controller | |
* @function General $general | |
* @function Response $response | |
* @function View $view | |
* @function Panel $panel | |
* @function Route $route | |
* @function Http $http | |
* @function Enqueue $enqueue | |
* @function Database $database | |
* @function Shortcode $shortcode | |
* @function Widget $widget | |
*/ | |
class Plugin { | |
/** | |
* @var array | |
*/ | |
protected $config = []; | |
/** | |
* @var array | |
*/ | |
protected $buildFiles = [ | |
'activate.php', | |
'deactivate.php', | |
'panels.php', | |
'routes.php', | |
'enqueue.php', | |
'api.php', | |
'shortcodes.php', | |
'widgets.php', | |
]; | |
/** | |
* @var array | |
*/ | |
protected $lazyLoadClassMap = [ | |
'message' => 'Herbert\Framework\Message', | |
'controller' => 'Herbert\Framework\Controller', | |
'general' => 'Herbert\Framework\General', | |
'response' => 'Herbert\Framework\Response', | |
'view' => 'Herbert\Framework\View', | |
'panel' => 'Herbert\Framework\Panel', | |
'route' => 'Herbert\Framework\Route', | |
'http' => 'Herbert\Framework\Http', | |
'enqueue' => 'Herbert\Framework\Enqueue', | |
'database' => 'Herbert\Framework\Database', | |
'shortcode' => 'Herbert\Framework\Shortcode', | |
'widget' => 'Herbert\Framework\Widget', | |
]; | |
public $siteUrl; | |
public $adminUrl; | |
public $name; | |
public function config($key = null){ | |
if($key === null){ | |
return $this->config; | |
} | |
return $this->config[$key]; | |
} | |
/** | |
* @param array $config | |
*/ | |
public function __construct(array $config) | |
{ | |
$this->configure($config); | |
$this->build(); | |
} | |
/** | |
* @param array $config | |
*/ | |
protected function configure(array $config) | |
{ | |
$siteUrl = get_site_url(); | |
$this->siteUrl = rtrim($siteUrl, '/'); | |
$adminUrl = get_admin_url(); | |
$this->adminUrl = rtrim($adminUrl, '/'); | |
$this->name = $config['name']; | |
// allow lazy load classes to be overridden | |
if(!empty($config['lazy_load_class_map'])){ | |
$this->lazyLoadClassMap = array_merge($this->lazyLoadClassMap, $config['lazy_load_class_map']); | |
} | |
// build files defined by config | |
if(!empty($config['build_files'])){ | |
$this->buildFiles = $config['build_files']; | |
} | |
$c = $config; | |
$c['path']['base'] = str_replace('framework/Plugin.php', '', __FILE__); | |
$c['path']['core'] = $c['path']['base'] . $c['core']; | |
$c['path']['plugin'] = $c['path']['base'] . $c['plugin'] . '/'; | |
$c['path']['controllers'] = $c['path']['plugin'] . 'controllers' . '/'; | |
$c['path']['views'] = $c['path']['plugin'] . $c['views'] . '/'; | |
$c['path']['assets'] = $c['path']['plugin'] . $c['assets'] . '/'; | |
$c['path']['widgets'] = $c['path']['plugin'] . 'widgets' . '/'; | |
$c['url']['base'] = str_replace('framework/', '', plugin_dir_url(__FILE__)); | |
$c['url']['plugin'] = $c['url']['base'] . $c['plugin'] . '/'; | |
$c['url']['assets'] = $c['url']['plugin'] . $c['assets'] . '/'; | |
$this->config = $c; | |
} | |
/** | |
* Builds the plugin. | |
*/ | |
protected function build() | |
{ | |
foreach($this->buildFiles as $file) | |
{ | |
$this->loadFile($file); | |
} | |
} | |
/** | |
* require_once each file within a different function context to ensure no global variables are carried from one file to the next | |
* @param string $file | |
*/ | |
protected function loadFile($file) | |
{ | |
$plugin = $this; | |
require_once $this->config['path']['plugin'] . $file; | |
} | |
/** | |
* Magic getter to bypass referencing plugin. | |
* | |
* @param $prop | |
* @return mixed | |
*/ | |
public function __get($prop) | |
{ | |
if(!property_exists($this, $prop) && !empty($this->lazyLoadClassMap[$prop])) { | |
$Class = $this->lazyLoadClassMap[$prop]; | |
$this->{$prop} = new $Class($this); | |
} | |
return $this->{$prop}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment