1
0
mirror of https://github.com/typemill/typemill.git synced 2025-01-17 21:38:25 +01:00
php-typemill/system/Plugins.php

111 lines
2.7 KiB
PHP
Raw Normal View History

2019-10-20 12:09:45 +02:00
<?php
namespace Typemill;
class Plugins
{
public function load()
{
$pluginFolder = $this->scanPluginFolder();
$classNames = array();
/* iterate over plugin folders */
foreach($pluginFolder as $plugin)
{
$className = '\\Plugins\\' . $plugin . '\\' . $plugin;
/* if plugin-class and subscribe-method exists, add classname to array */
if(class_exists($className) /* && method_exists($className, 'getSubscribedEvents') */)
{
$classNames[] = array('className' => $className, 'name' => $plugin);
}
}
return $classNames;
}
public function getNewRoutes($className, $routes)
{
/* if route-method exists in plugin-class */
if(method_exists($className, 'addNewRoutes'))
{
/* add the routes */
$pluginRoutes = $className::addNewRoutes();
/* multi-dimensional or simple array of routes */
if(isset($pluginRoutes[0]))
{
/* if they are properly formatted, add them to routes array */
foreach($pluginRoutes as $pluginRoute)
{
if($this->checkRouteArray($routes,$pluginRoute))
{
$pluginRoute['route'] = strtolower($pluginRoute['route']);
$routes[] = $pluginRoute;
}
}
}
elseif(is_array($routes))
{
if($this->checkRouteArray($routes,$pluginRoutes))
{
$pluginRoutes['route'] = strtolower($pluginRoutes['route']);
$routes[] = $pluginRoutes;
}
}
}
return $routes;
}
public function getNewMiddleware($className, $middleware)
{
if(method_exists($className, 'addNewMiddleware'))
{
$pluginMiddleware = $className::addNewMiddleware();
if($pluginMiddleware)
{
$middleware[] = $pluginMiddleware;
}
}
return $middleware;
}
private function checkRouteArray($routes,$route)
{
if(
isset($route['httpMethod']) AND in_array($route['httpMethod'], array('get','post','put','delete','head','patch','options'))
AND isset($route['route']) AND is_string($route['route'])
2020-10-03 21:28:20 +02:00
AND isset($route['class']) AND is_string($route['class']))
2019-10-20 12:09:45 +02:00
{
return true;
}
return false;
}
private function in_array_r($needle, $haystack, $strict = false)
{
foreach ($haystack as $item)
{
if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && $this->in_array_r($needle, $item, $strict)))
{
return true;
}
}
return false;
}
private function scanPluginFolder()
{
$pluginsDir = __DIR__ . '/../plugins';
/* check if plugins directory exists */
if(!is_dir($pluginsDir)){ return array(); }
/* get all plugins folder */
$plugins = array_diff(scandir($pluginsDir), array('..', '.'));
return $plugins;
}
2017-11-17 17:44:02 +01:00
}