mirror of
https://github.com/flextype/flextype.git
synced 2025-08-12 08:04:05 +02:00
Remove Event, Filter, View classes from Flextype project
This commit is contained in:
@@ -1,104 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package Flextype
|
||||
*
|
||||
* @author Sergey Romanenko <awilum@yandex.ru>
|
||||
* @link http://flextype.org
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flextype;
|
||||
|
||||
use Flextype\Component\Arr\Arr;
|
||||
|
||||
class Events
|
||||
{
|
||||
|
||||
/**
|
||||
* Events
|
||||
*
|
||||
* @var array
|
||||
* @access protected
|
||||
*/
|
||||
protected static $events = [];
|
||||
|
||||
/**
|
||||
* Protected constructor since this is a static class.
|
||||
*
|
||||
* @access protected
|
||||
*/
|
||||
protected function __construct()
|
||||
{
|
||||
// Nothing here
|
||||
}
|
||||
|
||||
/**
|
||||
* Hooks a function on to a specific event.
|
||||
*
|
||||
* @access public
|
||||
* @param string $event_name Event name
|
||||
* @param mixed $added_function Added function
|
||||
* @param integer $priority Priority. Default is 10
|
||||
* @param array $args Arguments
|
||||
* @return void
|
||||
*/
|
||||
public static function addListener(string $event_name, $added_function, int $priority = 10, array $args = null) : void
|
||||
{
|
||||
// Hooks a function on to a specific event.
|
||||
static::$events[] = array(
|
||||
'event_name' => $event_name,
|
||||
'function' => $added_function,
|
||||
'priority' => $priority,
|
||||
'args' => $args
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Run functions hooked on a specific event.
|
||||
*
|
||||
* @access public
|
||||
* @param string $event_name Event name
|
||||
* @param array $args Arguments
|
||||
* @param boolean $return Return data or not. Default is false
|
||||
* @return mixed
|
||||
*/
|
||||
public static function dispatch(string $event_name, array $args = [], bool $return = false)
|
||||
{
|
||||
// Redefine arguments
|
||||
$event_name = $event_name;
|
||||
$return = $return;
|
||||
|
||||
// Run event
|
||||
if (count(static::$events) > 0) {
|
||||
|
||||
// Sort actions by priority
|
||||
$events = Arr::subvalSort(static::$events, 'priority');
|
||||
|
||||
// Loop through $events array
|
||||
foreach ($events as $action) {
|
||||
|
||||
// Execute specific action
|
||||
if ($action['event_name'] == $event_name) {
|
||||
// isset arguments ?
|
||||
if (isset($args)) {
|
||||
// Return or Render specific action results ?
|
||||
if ($return) {
|
||||
return call_user_func_array($action['function'], $args);
|
||||
} else {
|
||||
call_user_func_array($action['function'], $args);
|
||||
}
|
||||
} else {
|
||||
if ($return) {
|
||||
return call_user_func_array($action['function'], $action['args']);
|
||||
} else {
|
||||
call_user_func_array($action['function'], $action['args']);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,114 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package Flextype
|
||||
*
|
||||
* @author Sergey Romanenko <awilum@yandex.ru>
|
||||
* @link http://flextype.org
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flextype;
|
||||
|
||||
class Filters
|
||||
{
|
||||
|
||||
/**
|
||||
* @var Flextype
|
||||
*/
|
||||
protected $flextype;
|
||||
|
||||
/**
|
||||
* Filters
|
||||
*
|
||||
* @var array
|
||||
* @access protected
|
||||
*/
|
||||
protected static $filters = [];
|
||||
|
||||
/**
|
||||
* Protected constructor since this is a static class.
|
||||
*
|
||||
* @access protected
|
||||
*/
|
||||
protected function __construct()
|
||||
{
|
||||
// Nothing here
|
||||
}
|
||||
|
||||
/**
|
||||
* Add filter
|
||||
*
|
||||
* @access public
|
||||
* @param string $filter_name The name of the filter to hook the $function_to_add to.
|
||||
* @param string $function_to_add The name of the function to be called when the filter is applied.
|
||||
* @param integer $priority Function to add priority - default is 10.
|
||||
* @param integer $accepted_args The number of arguments the function accept default is 1.
|
||||
* @return bool
|
||||
*/
|
||||
public static function addListener($filter_name, $function_to_add, $priority = 10, $accepted_args = 1) : bool
|
||||
{
|
||||
// Redefine arguments
|
||||
$filter_name = (string) $filter_name;
|
||||
$function_to_add = $function_to_add;
|
||||
$priority = (int) $priority;
|
||||
$accepted_args = (int) $accepted_args;
|
||||
|
||||
// Check that we don't already have the same filter at the same priority. Thanks to WP :)
|
||||
if (isset(static::$filters[$filter_name]["$priority"])) {
|
||||
foreach (static::$filters[$filter_name]["$priority"] as $filter) {
|
||||
if ($filter['function'] == $function_to_add) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static::$filters[$filter_name]["$priority"][] = array('function' => $function_to_add, 'accepted_args' => $accepted_args);
|
||||
|
||||
// Sort
|
||||
ksort(static::$filters[$filter_name]["$priority"]);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Dispatch filters
|
||||
*
|
||||
* @access public
|
||||
* @param string $filter_name The name of the filter hook.
|
||||
* @param mixed $value The value on which the filters hooked.
|
||||
* @return mixed
|
||||
*/
|
||||
public static function dispatch(string $filter_name, $value)
|
||||
{
|
||||
$args = array_slice(func_get_args(), 2);
|
||||
|
||||
if (! isset(static::$filters[$filter_name])) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
foreach (static::$filters[$filter_name] as $priority => $functions) {
|
||||
if (! is_null($functions)) {
|
||||
foreach ($functions as $function) {
|
||||
$all_args = array_merge(array($value), $args);
|
||||
$function_name = $function['function'];
|
||||
$accepted_args = $function['accepted_args'];
|
||||
if ($accepted_args == 1) {
|
||||
$the_args = array($value);
|
||||
} elseif ($accepted_args > 1) {
|
||||
$the_args = array_slice($all_args, 0, $accepted_args);
|
||||
} elseif ($accepted_args == 0) {
|
||||
$the_args = null;
|
||||
} else {
|
||||
$the_args = $all_args;
|
||||
}
|
||||
$value = call_user_func_array($function_name, $the_args);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
}
|
@@ -1,228 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package Flextype
|
||||
*
|
||||
* @author Sergey Romanenko <awilum@yandex.ru>
|
||||
* @link http://flextype.org
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flextype;
|
||||
|
||||
use Flextype\Component\Filesystem\Filesystem;
|
||||
|
||||
class View
|
||||
{
|
||||
/**
|
||||
* Path to view file.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $view_file;
|
||||
|
||||
/**
|
||||
* View variables.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $vars = array();
|
||||
|
||||
/**
|
||||
* Global view variables.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected static $global_vars = array();
|
||||
|
||||
/**
|
||||
* The output.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $output;
|
||||
|
||||
/**
|
||||
* Create a new view object.
|
||||
*
|
||||
* // Create new view object
|
||||
* $view = new View('blog/views/backend/index');
|
||||
*
|
||||
* // Assign some new variables
|
||||
* $view->assign('msg', 'Some message...');
|
||||
*
|
||||
* // Get view
|
||||
* $output = $view->render();
|
||||
*
|
||||
* // Display view
|
||||
* echo $output;
|
||||
*
|
||||
* @param string $view Name of the view file
|
||||
* @param array $variables Array of view variables
|
||||
*/
|
||||
public function __construct(string $view, array $variables = [])
|
||||
{
|
||||
// Set view file
|
||||
// From current theme folder or from plugin folder
|
||||
if (Filesystem::fileExists($theme_view_file = THEMES_PATH . '/' . Config::get('site.theme') . '/templates/' . $view . '.php')) {
|
||||
$this->view_file = $theme_view_file;
|
||||
} else {
|
||||
$this->view_file = PLUGINS_PATH . '/' . $view . '.php';
|
||||
}
|
||||
|
||||
// Is view file exists ?
|
||||
if (Filesystem::fileExists($this->view_file) === false) {
|
||||
throw new RuntimeException(vsprintf("%s(): The '%s' view does not exist.", array(__METHOD__, $view)));
|
||||
}
|
||||
|
||||
// Set view variables
|
||||
$this->vars = $variables;
|
||||
}
|
||||
|
||||
/**
|
||||
* View factory
|
||||
*
|
||||
* // Create new view object, assign some variables
|
||||
* // and displays the rendered view in the browser.
|
||||
* View::factory('blog/views/backend/index')
|
||||
* ->assign('msg', 'Some message...')
|
||||
* ->display();
|
||||
*
|
||||
* @param string $view Name of the view file
|
||||
* @param array $variables Array of view variables
|
||||
* @return View
|
||||
*/
|
||||
public static function factory(string $view, array $variables = [])
|
||||
{
|
||||
return new View($view, $variables);
|
||||
}
|
||||
|
||||
/**
|
||||
* Assign a view variable.
|
||||
*
|
||||
* $view->assign('msg', 'Some message...');
|
||||
*
|
||||
* @param string $key Variable name
|
||||
* @param mixed $value Variable value
|
||||
* @param boolean $global Set variable available in all views
|
||||
* @return View
|
||||
*/
|
||||
public function assign(string $key, $value, bool $global = false)
|
||||
{
|
||||
// Assign a new view variable (global or locale)
|
||||
if ($global === false) {
|
||||
$this->vars[$key] = $value;
|
||||
} else {
|
||||
View::$global_vars[$key] = $value;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Include the view file and extracts the view variables before returning the generated output.
|
||||
*
|
||||
* // Get view
|
||||
* $output = $view->render();
|
||||
*
|
||||
* // Display output
|
||||
* echo $output;
|
||||
*
|
||||
* @param string $filter Callback function used to filter output
|
||||
* @return string
|
||||
*/
|
||||
public function render($filter = null) : string
|
||||
{
|
||||
// Is output empty ?
|
||||
if (empty($this->output)) {
|
||||
|
||||
// Extract variables as references
|
||||
extract(array_merge($this->vars, View::$global_vars), EXTR_REFS);
|
||||
|
||||
// Turn on output buffering
|
||||
ob_start();
|
||||
|
||||
// Include view file
|
||||
include($this->view_file);
|
||||
|
||||
// Output...
|
||||
$this->output = ob_get_clean();
|
||||
}
|
||||
|
||||
// Filter output ?
|
||||
if ($filter !== null) {
|
||||
$this->output = call_user_func($filter, $this->output);
|
||||
}
|
||||
|
||||
// Return output
|
||||
return $this->output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays the rendered view in the browser.
|
||||
*
|
||||
* $view->display();
|
||||
*
|
||||
*/
|
||||
public function display()
|
||||
{
|
||||
echo $this->render();
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic setter method that assigns a view variable.
|
||||
*
|
||||
* @param string $key Variable name
|
||||
* @param mixed $value Variable value
|
||||
*/
|
||||
public function __set(string $key, $value)
|
||||
{
|
||||
$this->vars[$key] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic getter method that returns a view variable.
|
||||
*
|
||||
* @param string $key Variable name
|
||||
* @return mixed
|
||||
*/
|
||||
public function __get(string $key)
|
||||
{
|
||||
if (isset($this->vars[$key])) {
|
||||
return $this->vars[$key];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic isset method that checks if a view variable is set.
|
||||
*
|
||||
* @param string $key Variable name
|
||||
* @return boolean
|
||||
*/
|
||||
public function __isset(string $key)
|
||||
{
|
||||
return isset($this->vars[$key]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic unset method that unsets a view variable.
|
||||
*
|
||||
* @param string $key Variable name
|
||||
*/
|
||||
public function __unset(string $key)
|
||||
{
|
||||
unset($this->vars[$key]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method that magically converts the view object into a string.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function __toString() : string
|
||||
{
|
||||
return $this->render();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user