diff --git a/README.md b/README.md index 239a6e1..23c4fe8 100644 --- a/README.md +++ b/README.md @@ -66,15 +66,15 @@ Here is an example of a basic `events.php` file for handling an event: addListener(\App\Event\BuildRoutes::NAME, function(\App\Event\BuildRoutes $event) { + $dispatcher->addListener(\App\Event\BuildRoutes::class, function(\App\Event\BuildRoutes $event) { $app = $event->getApp(); - + // Modify the app's routes here }, -5); }; ``` -As you can see, each event's name is available as a constant on its class named `::NAME`, and each event listener receives an instance of that event class complete with relevant metadata already attached. Listeners also have a priority (the last argument in the function call); this number can be positive or negative, with the default handler tending to be around zero. Higher numbers are dispatched before lower numbers. +As you can see, each event listener that you register has to provide the event that it listens to as a callable to the `addListener` method's first parameter, and each event listener receives an instance of that event class complete with relevant metadata already attached. Listeners also have a priority (the last argument in the function call); this number can be positive or negative, with the default handler tending to be around zero. Higher numbers are dispatched before lower numbers. Below is a listing of the events that can be overridden by plugins: diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..0fc7c9b --- /dev/null +++ b/composer.json @@ -0,0 +1,17 @@ +{ + "name": "azuracast/example-plugin", + "description": "An example AzuraCast plugin, demonstrating the capabilities and common use-cases of the plugin system.", + "type": "azuracast-plugin", + "authors": [ + { + "name": "Buster Neece", + "email": "buster@busterneece.com" + } + ], + "autoload": { + "psr-4": { + "Plugin\\ExamplePlugin\\": "src" + } + }, + "require": {} +} diff --git a/events.php b/events.php index 40ed233..b137e03 100644 --- a/events.php +++ b/events.php @@ -1,29 +1,35 @@ -addListener(Event\BuildConsoleCommands::NAME, function (Event\BuildConsoleCommands $event) { - $event->getConsole()->addCommands([ - new \Plugin\ExamplePlugin\Command\ListStations, - ]); - }, -1); - - // Tell the view handler to look for templates in this directory too - $dispatcher->addListener(Event\BuildView::NAME, function(Event\BuildView $event) { - $event->getView()->addFolder('example', __DIR__.'/templates'); - }); - - // Add a new route handled exclusively by the plugin. - $dispatcher->addListener(Event\BuildRoutes::NAME, function(Event\BuildRoutes $event) { - $app = $event->getApp(); - - $app->get('/example', \Plugin\ExamplePlugin\Controller\HelloWorld::class) - ->setName('example-plugin:index:index') - ->add(\Azura\Middleware\EnableView::class); - }); - - // You can also add classes that implement the EventSubscriberInterface - $dispatcher->addSubscriber(new \Plugin\ExamplePlugin\EventHandler\AllTheListeners); -}; \ No newline at end of file +addListener(Event\BuildConsoleCommands::class, function (Event\BuildConsoleCommands $event) { + $console = $event->getConsole(); + + $console->command( + 'example:list-stations', + \Plugin\ExamplePlugin\Command\ListStations::class, + )->setDescription('An example function to list stations in a table view.'); + }, -1); + + // Tell the view handler to look for templates in this directory too + $dispatcher->addListener(Event\BuildView::class, function(Event\BuildView $event) { + $event->getView()->addFolder('example', __DIR__.'/templates'); + }); + + // Add a new route handled exclusively by the plugin. + $dispatcher->addListener(Event\BuildRoutes::class, function(Event\BuildRoutes $event) { + $app = $event->getApp(); + + $app->get('/example', \Plugin\ExamplePlugin\Controller\HelloWorld::class) + ->setName('example-plugin:index:index') + ->add(\App\Middleware\EnableView::class); + }); + + // You can also add classes that implement the EventSubscriberInterface + $dispatcher->addSubscriber(new \Plugin\ExamplePlugin\EventHandler\AllTheListeners); +}; diff --git a/services.php b/services.php index 5b5bcab..45bcbc5 100644 --- a/services.php +++ b/services.php @@ -1,4 +1,7 @@ -setName('example:list-stations') - ->setDescription('An example function to list stations in a table view.'); - } - - /** - * {@inheritdoc} - */ - protected function execute( - \Symfony\Component\Console\Input\InputInterface $input, - \Symfony\Component\Console\Output\OutputInterface $output - ) { - $io = new \Symfony\Component\Console\Style\SymfonyStyle($input, $output); - $io->title('Example Plugin: Stations'); - - $headers = [ - 'ID', - 'Name', - 'Frontend', - 'Backend', - 'Remotes', - ]; - - $rows = []; - - /** @var \App\Radio\Adapters $adapters */ - $adapters = $this->get(\App\Radio\Adapters::class); - - /** @var \Doctrine\ORM\EntityManager $em */ - $em = $this->get(\Doctrine\ORM\EntityManager::class); - - $stations = $em - ->getRepository(\App\Entity\Station::class) - ->findAll(); - - foreach($stations as $station) { - /** @var \App\Entity\Station $station */ - - $backend = $adapters->getBackendAdapter($station); - $frontend = $adapters->getFrontendAdapter($station); - - $rows[] = [ - $station->getId(), - $station->getName(), - ucfirst($station->getBackendType()).' ('.($backend->isRunning($station) ? 'Running' : 'Stopped').')', - ucfirst($station->getFrontendType()).' ('.($frontend->isRunning($station) ? 'Running' : 'Stopped').')', - $station->getRemotes()->count(), - ]; - } - - - $table = (new \Symfony\Component\Console\Helper\Table($output)) - ->setHeaders($headers) - ->setRows($rows); - - $table->render(); - - return 0; - } -} +title('Example Plugin: Stations'); + + $headers = [ + 'ID', + 'Name', + 'Frontend', + 'Backend', + 'Remotes', + ]; + + $rows = []; + + $stations = $entityManager + ->getRepository(\App\Entity\Station::class) + ->findAll(); + + foreach($stations as $station) { + /** @var \App\Entity\Station $station */ + + $backend = $adapters->getBackendAdapter($station); + $frontend = $adapters->getFrontendAdapter($station); + + $rows[] = [ + $station->getId(), + $station->getName(), + ucfirst($station->getBackendType()).' ('.($backend->isRunning($station) ? 'Running' : 'Stopped').')', + ucfirst($station->getFrontendType()).' ('.($frontend->isRunning($station) ? 'Running' : 'Stopped').')', + $station->getRemotes()->count(), + ]; + } + + $io->table($headers, $rows); + + return 0; + } +} diff --git a/src/Controller/HelloWorld.php b/src/Controller/HelloWorld.php index b403131..f27a9a2 100644 --- a/src/Controller/HelloWorld.php +++ b/src/Controller/HelloWorld.php @@ -1,13 +1,16 @@ -getView()->renderToResponse($response, 'example::hello_world'); - } -} \ No newline at end of file +getView()->renderToResponse($response, 'example::hello_world'); + } +} diff --git a/src/EventHandler/AllTheListeners.php b/src/EventHandler/AllTheListeners.php index 1bdbd18..0d0f17c 100644 --- a/src/EventHandler/AllTheListeners.php +++ b/src/EventHandler/AllTheListeners.php @@ -1,46 +1,50 @@ - 'methodName') - * * array('eventName' => array('methodName', $priority)) - * * array('eventName' => array(array('methodName1', $priority), array('methodName2'))) - * - * @return array The event names to listen to - */ - public static function getSubscribedEvents() - { - return [ - Event\Radio\GenerateRawNowPlaying::NAME => [ - ['setListenerCount', -20] - ], - ]; - } - - public function setListenerCount(Event\Radio\GenerateRawNowPlaying $event) - { - $np_raw = $event->getRawResponse(); - - $np_raw['listeners']['current'] = mt_rand(5, 25); - $np_raw['listeners']['unique'] = mt_rand(0, $np_raw['listeners']['current']); - $np_raw['listeners']['total'] = $np_raw['listeners']['current']; - - $event->setRawResponse($np_raw); - } -} \ No newline at end of file + 'methodName') + * * array('eventName' => array('methodName', $priority)) + * * array('eventName' => array(array('methodName1', $priority), array('methodName2'))) + * + * @return array The event names to listen to + */ + public static function getSubscribedEvents() + { + return [ + Event\Radio\GenerateRawNowPlaying::class => [ + ['setListenerCount', -20] + ], + ]; + } + + public function setListenerCount(Event\Radio\GenerateRawNowPlaying $event) + { + $np_raw = $event->getResult()->toArray(); + + $np_raw['listeners']['current'] = mt_rand(5, 25); + $np_raw['listeners']['unique'] = mt_rand(0, $np_raw['listeners']['current']); + $np_raw['listeners']['total'] = $np_raw['listeners']['current']; + + $event->setResult(Result::fromArray($np_raw)); + } +} diff --git a/templates/hello_world.phtml b/templates/hello_world.phtml index 16ba08c..2124f9e 100644 --- a/templates/hello_world.phtml +++ b/templates/hello_world.phtml @@ -1,13 +1,20 @@ -layout('minimal', ['title' => __('Hello World!'), 'page_class' => 'four-zero-content']) ?> +layout('minimal', ['title' => __('Hello World!'), 'page_class' => 'example-content']) ?> -
-
-

200

- Hello World! +
+
+
+
+

Hello World!

+
-
-
+