mirror of
https://github.com/typemill/typemill.git
synced 2025-07-31 03:10:19 +02:00
Version 1.1.0 Introducing Plugins, Event-Dispatcher and CookieConsent
This commit is contained in:
@@ -20,7 +20,6 @@ $dispatcher = new \Symfony\Component\EventDispatcher\EventDispatcher();
|
||||
************************/
|
||||
|
||||
$settings = Typemill\settings::loadSettings();
|
||||
$settings = $dispatcher->dispatch('onSettingsLoaded', new LoadSettingsEvent($settings))->getData();
|
||||
|
||||
/************************
|
||||
* INITIATE SLIM *
|
||||
@@ -34,23 +33,69 @@ $app = new \Slim\App($settings);
|
||||
|
||||
$container = $app->getContainer();
|
||||
|
||||
/************************
|
||||
* ADD CSRF PROTECTION *
|
||||
************************/
|
||||
|
||||
$container['csrf'] = function ($c) {
|
||||
$guard = new \Slim\Csrf\Guard();
|
||||
$guard->setPersistentTokenMode(true);
|
||||
|
||||
return $guard;
|
||||
};
|
||||
|
||||
/************************
|
||||
* LOAD PLUGINS *
|
||||
************************/
|
||||
|
||||
$plugins = new Typemill\Plugins();
|
||||
$pluginClassNames = $plugins->load();
|
||||
$routes = $middleware = array();
|
||||
$plugins = new Typemill\Plugins();
|
||||
$pluginNames = $plugins->load();
|
||||
$pluginSettings['plugins'] = array();
|
||||
|
||||
foreach($pluginClassNames as $pluginClassName)
|
||||
$routes = $middleware = array();
|
||||
|
||||
foreach($pluginNames as $pluginName)
|
||||
{
|
||||
$routes = $plugins->getNewRoutes($pluginClassName, $routes);
|
||||
$middleware = $plugins->getNewMiddleware($pluginClassName, $middleware);
|
||||
|
||||
$dispatcher->addSubscriber(new $pluginClassName($container));
|
||||
$className = $pluginName['className'];
|
||||
$name = $pluginName['name'];
|
||||
|
||||
/* if plugin is not in user settings yet */
|
||||
if(!isset($settings['settings']['plugins'][$name]))
|
||||
{
|
||||
/* then read the plugin default settings and write them to the users setting.yaml */
|
||||
$updateSettingsYaml = Typemill\settings::addPluginSettings($name);
|
||||
|
||||
/* if default settings are written successfully to user settings, update the pluginSettings */
|
||||
if($updateSettingsYaml)
|
||||
{
|
||||
$pluginSettings['plugins'][$name] = $updateSettingsYaml;
|
||||
}
|
||||
/* if not, then settingsYaml does not exist, so set plugin to false for further use */
|
||||
else
|
||||
{
|
||||
$pluginSettings['plugins'][$name] = false;
|
||||
}
|
||||
/* get settings from di-container and update them with the new plugin Settings */
|
||||
$DIsettings = $container->get('settings');
|
||||
$DIsettings->replace($pluginSettings);
|
||||
}
|
||||
|
||||
/* if the plugin is activated, add routes/middleware and add plugin as event subscriber */
|
||||
if(isset($settings['settings']['plugins'][$name]['active']) && $settings['settings']['plugins'][$name]['active'] != false)
|
||||
{
|
||||
$routes = $plugins->getNewRoutes($className, $routes);
|
||||
$middleware = $plugins->getNewMiddleware($className, $middleware);
|
||||
|
||||
$dispatcher->addSubscriber(new $className($container));
|
||||
}
|
||||
}
|
||||
|
||||
$dispatcher->dispatch('onPluginsLoaded', new LoadPluginsEvent($pluginClassNames));
|
||||
/* dispatch the event onPluginsLoaded */
|
||||
$dispatcher->dispatch('onPluginsLoaded', new LoadPluginsEvent($pluginNames));
|
||||
|
||||
/* dispatch settings event and get all setting-updates from plugins */
|
||||
/* TODO, how to update the settings with a plugin? You cannot replace the full settings in the container, so you have to add settings in the container directly */
|
||||
$dispatcher->dispatch('onSettingsLoaded', new LoadSettingsEvent($settings))->getData();
|
||||
|
||||
/******************************
|
||||
* ADD DISPATCHER TO CONTAINER *
|
||||
@@ -61,6 +106,15 @@ $container['dispatcher'] = function($container) use ($dispatcher)
|
||||
return $dispatcher;
|
||||
};
|
||||
|
||||
/********************************
|
||||
* ADD ASSET-FUNCTION FOR TWIG *
|
||||
********************************/
|
||||
|
||||
$container['assets'] = function($c)
|
||||
{
|
||||
return new \Typemill\Assets($c['request']->getUri()->getBaseUrl());
|
||||
};
|
||||
|
||||
/******************************
|
||||
* ADD FLASH MESSAGES FOR TIWG *
|
||||
******************************/
|
||||
@@ -70,39 +124,35 @@ $container['flash'] = function ()
|
||||
return new \Slim\Flash\Messages();
|
||||
};
|
||||
|
||||
/********************************
|
||||
* ADD ASSET-FUNCTION FOR TWIG *
|
||||
********************************/
|
||||
|
||||
$container['assets'] = function($c)
|
||||
{
|
||||
return new \Typemill\Assets($c['request']->getUri()->getBaseUrl());
|
||||
};
|
||||
|
||||
/************************
|
||||
* LOAD TWIG VIEW *
|
||||
************************/
|
||||
|
||||
$container['view'] = function ($container) use ($settings)
|
||||
$container['view'] = function ($container)
|
||||
{
|
||||
$path = array($settings['settings']['themePath'], $settings['settings']['authorPath']);
|
||||
$path = array($container->get('settings')['themePath'], $container->get('settings')['authorPath']);
|
||||
|
||||
$view = new \Slim\Views\Twig( $path, [
|
||||
'cache' => false,
|
||||
'autoescape' => false
|
||||
'autoescape' => false,
|
||||
'debug' => true
|
||||
]);
|
||||
|
||||
// Instantiate and add Slim specific extension
|
||||
$basePath = rtrim(str_ireplace('index.php', '', $container['request']->getUri()->getBasePath()), '/');
|
||||
$view->addExtension(new Slim\Views\TwigExtension($container['router'], $basePath));
|
||||
$view['baseUrl'] = $container['request']->getUri()->getBaseUrl();
|
||||
$view->addExtension(new Twig_Extension_Debug());
|
||||
$view->addExtension(new Typemill\Extensions\TwigCsrfExtension($container['csrf']));
|
||||
|
||||
/* use {{ base_url() }} in twig templates */
|
||||
$view['base_url'] = $container['request']->getUri()->getBaseUrl();
|
||||
|
||||
/* add flash messages to all views */
|
||||
$view->getEnvironment()->addGlobal('flash', $container->flash);
|
||||
|
||||
/* add asset-function to all views */
|
||||
$view->getEnvironment()->addGlobal('assets', $container->assets);
|
||||
|
||||
|
||||
return $view;
|
||||
};
|
||||
|
||||
@@ -131,6 +181,10 @@ foreach($middleware as $pluginMiddleware)
|
||||
}
|
||||
}
|
||||
|
||||
$app->add(new \Typemill\Middleware\ValidationErrorsMiddleware($container['view']));
|
||||
$app->add(new \Typemill\Middleware\OldInputMiddleware($container['view']));
|
||||
$app->add($container->get('csrf'));
|
||||
|
||||
/************************
|
||||
* ADD ROUTES *
|
||||
************************/
|
||||
|
Reference in New Issue
Block a user