Skip to content

Commit

Permalink
Begin work on custom code plugins support.
Browse files Browse the repository at this point in the history
  • Loading branch information
BusterNeece committed Sep 21, 2018
1 parent dece3e9 commit 33210b9
Show file tree
Hide file tree
Showing 30 changed files with 820 additions and 253 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Expand Up @@ -32,3 +32,8 @@ web/static/yarn-error\.log
# Docker files
/docker-compose.yml
/*.tar.gz

# Plugins
/plugins/*
/plugins/**/*
!/plugins/.gitkeep
8 changes: 8 additions & 0 deletions bootstrap/app.php
Expand Up @@ -58,6 +58,10 @@
$autoloader = require(APP_INCLUDE_VENDOR . '/autoload.php');
$autoloader->setPsr4('Proxy\\', APP_INCLUDE_TEMP . '/proxies');

// Initialize plugins
$plugins = new \App\Plugins(APP_INCLUDE_ROOT.'/plugins');
$plugins->registerAutoloaders($autoloader);

// Set up DI container.
$di = new \Slim\Container([
'settings' => [
Expand All @@ -69,8 +73,12 @@
]
]);

$di[\App\Plugins::class] = $plugins;

// Define services.
$settings = require(dirname(__DIR__).'/config/settings.php');
call_user_func(include(dirname(__DIR__).'/config/services.php'), $di, $settings);

$plugins->registerServices($di, $settings);

return $di;
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -41,6 +41,7 @@
"php-http/guzzle6-adapter": "^1.1",
"slim/slim": "^3.0",
"supervisorphp/supervisor": "^3.0",
"symfony/event-dispatcher": "^4.1",
"symfony/finder": "^4.1",
"zendframework/zend-config": "^3.1.0",
"zendframework/zend-paginator": "^2.7"
Expand Down
128 changes: 64 additions & 64 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions config/events.php
@@ -0,0 +1,14 @@
<?php
return function (\Symfony\Component\EventDispatcher\EventDispatcher $dispatcher, \Slim\Container $di, $settings) {

$dispatcher->addSubscriber(new \App\EventHandler\DefaultRoutes(__DIR__.'/routes.php'));
$dispatcher->addSubscriber(new \App\EventHandler\DefaultView($di));
$dispatcher->addSubscriber(new \App\EventHandler\DefaultNowPlaying());

$dispatcher->addListener(\App\Event\SendWebhooks::NAME, function(\App\Event\SendWebhooks $event) use ($di) {
/** @var \App\Webhook\Dispatcher $webhook_dispatcher */
$webhook_dispatcher = $di[\App\Webhook\Dispatcher::class];

$webhook_dispatcher->dispatch($event);
});
};
78 changes: 21 additions & 57 deletions config/services.php
Expand Up @@ -233,46 +233,9 @@
$view = new App\View(dirname(__DIR__) . '/resources/templates');
$view->setFileExtension('phtml');

$view->registerFunction('service', function($service) use ($di) {
return $di->get($service);
});

$view->registerFunction('escapeJs', function($string) {
return json_encode($string);
});

$view->registerFunction('mailto', function ($address, $link_text = null) {
$address = substr(chunk_split(bin2hex(" $address"), 2, ";&#x"), 3, -3);
$link_text = $link_text ?? $address;

return '<a href="mailto:' . $address . '">' . $link_text . '</a>';
});

$view->registerFunction('pluralize', function ($word, $num = 0) {
if ((int)$num === 1) {
return $word;
} else {
return \Doctrine\Common\Inflector\Inflector::pluralize($word);
}
});

$view->registerFunction('truncate', function ($text, $length = 80) {
return \App\Utilities::truncate_text($text, $length);
});

/** @var \App\Session $session */
$session = $di[\App\Session::class];

$view->addData([
'app_settings' => $di['app_settings'],
'router' => $di['router'],
'request' => $di['request'],
'assets' => $di[\App\Assets::class],
'auth' => $di[\App\Auth::class],
'acl' => $di[\App\Acl::class],
'flash' => $session->getFlash(),
'customization' => $di[\App\Customization::class],
]);
/** @var \Symfony\Component\EventDispatcher\EventDispatcher $dispatcher */
$dispatcher = $di[\Symfony\Component\EventDispatcher\EventDispatcher::class];
$dispatcher->dispatch(\App\Event\BuildView::NAME, new \App\Event\BuildView($view));

return $view;
});
Expand Down Expand Up @@ -337,6 +300,21 @@
]);
};

$di[\Symfony\Component\EventDispatcher\EventDispatcher::class] = function($di) use ($settings) {
$dispatcher = new \Symfony\Component\EventDispatcher\EventDispatcher();

// Register application default events.
call_user_func(include(__DIR__.'/events.php'), $dispatcher, $di, $settings);

/** @var \App\Plugins $plugins */
$plugins = $di[\App\Plugins::class];

// Register plugin-provided events.
$plugins->registerEvents($dispatcher, $di, $settings);

return $dispatcher;
};

//
// AzuraCast-specific dependencies
//
Expand Down Expand Up @@ -401,23 +379,9 @@

$app = new \Slim\App($di);

// Get the current user entity object and assign it into the request if it exists.
$app->add(\App\Middleware\GetCurrentUser::class);

// Inject the application router into the request object.
$app->add(\App\Middleware\EnableRouter::class);

// Inject the session manager into the request object.
$app->add(\App\Middleware\EnableSession::class);

// Check HTTPS setting and enforce Content Security Policy accordingly.
$app->add(\App\Middleware\EnforceSecurity::class);

// Remove trailing slash from all URLs when routing.
$app->add(\App\Middleware\RemoveSlashes::class);

// Load routes
call_user_func(include(__DIR__.'/routes.php'), $app);
/** @var \Symfony\Component\EventDispatcher\EventDispatcher $dispatcher */
$dispatcher = $di[\Symfony\Component\EventDispatcher\EventDispatcher::class];
$dispatcher->dispatch(\App\Event\BuildRoutes::NAME, new \App\Event\BuildRoutes($app));

return $app;
};
Expand Down
Empty file added plugins/.gitkeep
Empty file.
22 changes: 22 additions & 0 deletions src/Event/BuildRoutes.php
@@ -0,0 +1,22 @@
<?php
namespace App\Event;

use Slim\App;
use Symfony\Component\EventDispatcher\Event;

class BuildRoutes extends Event
{
const NAME = 'build-routes';

protected $app;

public function __construct(App $app)
{
$this->app = $app;
}

public function getApp(): App
{
return $this->app;
}
}
22 changes: 22 additions & 0 deletions src/Event/BuildView.php
@@ -0,0 +1,22 @@
<?php
namespace App\Event;

use App\View;
use Symfony\Component\EventDispatcher\Event;

class BuildView extends Event
{
const NAME = 'build-view';

protected $view;

public function __construct(View $view)
{
$this->view = $view;
}

public function getView(): View
{
return $this->view;
}
}

0 comments on commit 33210b9

Please sign in to comment.