From 6d52a9820628e13ff5c4a769267ffb63c6a4e9ea Mon Sep 17 00:00:00 2001 From: Khoubza Younes Date: Wed, 29 Sep 2021 16:10:39 +0100 Subject: [PATCH] fix symfony configuration and sort by priority --- src/Cli/Prime/Notifier/NotifySendNotifier.php | 1 + src/Cli/Prime/Presenter/CliPresenter.php | 29 +++- .../DependencyInjection/Configuration.php | 134 ++++++++++++------ .../FlasherCliExtension.php | 6 +- src/Cli/Symfony/Resources/config/config.php | 2 +- 5 files changed, 123 insertions(+), 49 deletions(-) diff --git a/src/Cli/Prime/Notifier/NotifySendNotifier.php b/src/Cli/Prime/Notifier/NotifySendNotifier.php index e99559eb..3105cc57 100644 --- a/src/Cli/Prime/Notifier/NotifySendNotifier.php +++ b/src/Cli/Prime/Notifier/NotifySendNotifier.php @@ -39,6 +39,7 @@ final class NotifySendNotifier extends AbstractNotifier $default = array( 'binary' => 'notify-send', 'expire_time' => 0, + 'priority' => 1, ); $options = array_replace($default, $options); diff --git a/src/Cli/Prime/Presenter/CliPresenter.php b/src/Cli/Prime/Presenter/CliPresenter.php index 20559376..a04da570 100644 --- a/src/Cli/Prime/Presenter/CliPresenter.php +++ b/src/Cli/Prime/Presenter/CliPresenter.php @@ -14,6 +14,11 @@ final class CliPresenter implements PresenterInterface */ private $notifiers = array(); + /** + * @var NotifierInterface[] + */ + private $sorted = array(); + public function render(Response $response) { $notifier = $this->createNotifier(); @@ -26,12 +31,34 @@ final class CliPresenter implements PresenterInterface $this->notifiers[] = $notifier; } + public function getSortedNotifiers() + { + if (0 !== count($this->sorted)) { + return $this->sorted; + } + + $this->sorted = $this->notifiers; + + usort($this->sorted, static function (NotifierInterface $a, NotifierInterface $b) { + $priorityA = $a->getPriority(); + $priorityB = $b->getPriority(); + + if ($priorityA == $priorityB) { + return 0; + } + + return $priorityA < $priorityB ? 1 : -1; + }); + + return $this->sorted; + } + /** * @return NotifierInterface */ private function createNotifier() { - foreach ($this->notifiers as $notifier) { + foreach ($this->getSortedNotifiers() as $notifier) { if ($notifier->isSupported()) { return $notifier; } diff --git a/src/Cli/Symfony/DependencyInjection/Configuration.php b/src/Cli/Symfony/DependencyInjection/Configuration.php index 93866f6f..5e28439a 100644 --- a/src/Cli/Symfony/DependencyInjection/Configuration.php +++ b/src/Cli/Symfony/DependencyInjection/Configuration.php @@ -8,17 +8,23 @@ use Symfony\Component\Config\Definition\ConfigurationInterface; final class Configuration implements ConfigurationInterface { + /** + * @return TreeBuilder + */ public function getConfigTreeBuilder() { - $treeBuilder = new TreeBuilder('flasher_cli'); - $rootNode = $this->getRootNode($treeBuilder, 'flasher_cli'); + $name = 'flasher_cli'; + $treeBuilder = new TreeBuilder($name); + $rootNode = $this->getRootNode($treeBuilder, $name); $rootNode ->children() ->booleanNode('render_all') + ->isRequired() ->defaultValue(false) ->end() ->booleanNode('render_immediately') + ->isRequired() ->defaultValue(true) ->end() ->scalarNode('title') @@ -35,6 +41,10 @@ final class Configuration implements ConfigurationInterface ->prototype('variable')->end() ->defaultValue(array()) ->end() + ->arrayNode('sounds') + ->prototype('variable')->end() + ->defaultValue(array()) + ->end() ->append($this->addNotifiersConfig()) ->end() ; @@ -44,11 +54,12 @@ final class Configuration implements ConfigurationInterface private function addNotifiersConfig() { - $treeBuilder = new TreeBuilder('notifiers'); - $rootNode = $this->getRootNode($treeBuilder, 'notifiers'); + $name = 'notifiers'; + $rootNode = $this->getRootNode(new TreeBuilder($name), $name); $rootNode ->addDefaultsIfNotSet() + ->fixXmlConfig('notifier') ->children() ->append($this->addGrowlNotifyNotifier()) ->append($this->addKDialogNotifier()) @@ -67,15 +78,20 @@ final class Configuration implements ConfigurationInterface */ private function addGrowlNotifyNotifier() { - $treeBuilder = new TreeBuilder('growl_notify'); - $rootNode = $this->getRootNode($treeBuilder, 'growl_notify'); + $name = 'growl_notify'; + $rootNode = $this->getRootNode(new TreeBuilder($name), $name); $rootNode ->addDefaultsIfNotSet() ->children() ->booleanNode('enabled')->defaultValue(true)->end() - ->scalarNode('service')->defaultValue('flasher.cli.growl_notify')->end() - ->scalarNode('binary')->defaultValue('notify-send')->end() + ->scalarNode('priority') + ->beforeNormalization() + ->always(function ($v) { return (int) $v; }) + ->end() + ->defaultValue(0) + ->end() + ->scalarNode('binary')->defaultValue('growlnotify')->end() ->arrayNode('binary_paths') ->beforeNormalization() ->ifString() @@ -86,7 +102,7 @@ final class Configuration implements ConfigurationInterface ->scalarNode('title')->defaultValue(null)->end() ->booleanNode('mute')->defaultValue(true)->end() ->arrayNode('icons') - ->prototype('scalar')->end() + ->prototype('variable')->end() ->defaultValue(array()) ->end() ->arrayNode('sounds') @@ -104,15 +120,20 @@ final class Configuration implements ConfigurationInterface */ private function addKDialogNotifier() { - $treeBuilder = new TreeBuilder('kdialog_notifier'); - $rootNode = $this->getRootNode($treeBuilder, 'kdialog_notifier'); + $name = 'kdialog_notifier'; + $rootNode = $this->getRootNode(new TreeBuilder($name), $name); $rootNode ->addDefaultsIfNotSet() ->children() ->booleanNode('enabled')->defaultValue(true)->end() - ->scalarNode('service')->defaultValue('flasher.cli.kdialog_notifier')->end() - ->scalarNode('binary')->defaultValue('notify-send')->end() + ->scalarNode('priority') + ->beforeNormalization() + ->always(function ($v) { return (int) $v; }) + ->end() + ->defaultValue(0) + ->end() + ->scalarNode('binary')->defaultValue('kdialog')->end() ->arrayNode('binary_paths') ->beforeNormalization() ->ifString() @@ -123,11 +144,11 @@ final class Configuration implements ConfigurationInterface ->scalarNode('title')->defaultValue(null)->end() ->booleanNode('mute')->defaultValue(true)->end() ->arrayNode('icons') - ->prototype('scalar')->end() + ->prototype('variable')->end() ->defaultValue(array()) ->end() ->arrayNode('sounds') - ->prototype('scalar')->end() + ->prototype('variable')->end() ->defaultValue(array()) ->end() ->end() @@ -141,15 +162,20 @@ final class Configuration implements ConfigurationInterface */ private function addNotifuNotifier() { - $treeBuilder = new TreeBuilder('notifu_notifier'); - $rootNode = $this->getRootNode($treeBuilder, 'notifu_notifier'); + $name = 'notifu_notifier'; + $rootNode = $this->getRootNode(new TreeBuilder($name), $name); $rootNode ->addDefaultsIfNotSet() ->children() ->booleanNode('enabled')->defaultValue(true)->end() - ->scalarNode('service')->defaultValue('flasher.cli.notifu_notifier')->end() - ->scalarNode('binary')->defaultValue('notify-send')->end() + ->scalarNode('priority') + ->beforeNormalization() + ->always(function ($v) { return (int) $v; }) + ->end() + ->defaultValue(0) + ->end() + ->scalarNode('binary')->defaultValue('notifu')->end() ->arrayNode('binary_paths') ->beforeNormalization() ->ifString() @@ -160,11 +186,11 @@ final class Configuration implements ConfigurationInterface ->scalarNode('title')->defaultValue(null)->end() ->booleanNode('mute')->defaultValue(true)->end() ->arrayNode('icons') - ->prototype('scalar')->end() + ->prototype('variable')->end() ->defaultValue(array()) ->end() ->arrayNode('sounds') - ->prototype('scalar')->end() + ->prototype('variable')->end() ->defaultValue(array()) ->end() ->end() @@ -178,14 +204,19 @@ final class Configuration implements ConfigurationInterface */ private function addNotifySendNotifier() { - $treeBuilder = new TreeBuilder('notify_send'); - $rootNode = $this->getRootNode($treeBuilder, 'notify_send'); + $name = 'notify_send'; + $rootNode = $this->getRootNode(new TreeBuilder($name), $name); $rootNode ->addDefaultsIfNotSet() ->children() ->booleanNode('enabled')->defaultValue(true)->end() - ->scalarNode('service')->defaultValue('flasher.cli.notify_send')->end() + ->scalarNode('priority') + ->beforeNormalization() + ->always(function ($v) { return (int) $v; }) + ->end() + ->defaultValue(1) + ->end() ->scalarNode('binary')->defaultValue('notify-send')->end() ->arrayNode('binary_paths') ->beforeNormalization() @@ -197,11 +228,11 @@ final class Configuration implements ConfigurationInterface ->scalarNode('title')->defaultValue(null)->end() ->booleanNode('mute')->defaultValue(true)->end() ->arrayNode('icons') - ->prototype('scalar')->end() + ->prototype('variable')->end() ->defaultValue(array()) ->end() ->arrayNode('sounds') - ->prototype('scalar')->end() + ->prototype('variable')->end() ->defaultValue(array()) ->end() ->end() @@ -215,15 +246,20 @@ final class Configuration implements ConfigurationInterface */ private function addSnoreToastNotifier() { - $treeBuilder = new TreeBuilder('snore_toast_notifier'); - $rootNode = $this->getRootNode($treeBuilder, 'snore_toast_notifier'); + $name = 'snore_toast_notifier'; + $rootNode = $this->getRootNode(new TreeBuilder($name), $name); $rootNode ->addDefaultsIfNotSet() ->children() ->booleanNode('enabled')->defaultValue(true)->end() - ->scalarNode('service')->defaultValue('flasher.cli.snore_toast_notifier')->end() - ->scalarNode('binary')->defaultValue('notify-send')->end() + ->scalarNode('priority') + ->beforeNormalization() + ->always(function ($v) { return (int) $v; }) + ->end() + ->defaultValue(0) + ->end() + ->scalarNode('binary')->defaultValue('snoretoast')->end() ->arrayNode('binary_paths') ->beforeNormalization() ->ifString() @@ -234,11 +270,11 @@ final class Configuration implements ConfigurationInterface ->scalarNode('title')->defaultValue(null)->end() ->booleanNode('mute')->defaultValue(true)->end() ->arrayNode('icons') - ->prototype('scalar')->end() + ->prototype('variable')->end() ->defaultValue(array()) ->end() ->arrayNode('sounds') - ->prototype('scalar')->end() + ->prototype('variable')->end() ->defaultValue(array()) ->end() ->end() @@ -252,15 +288,20 @@ final class Configuration implements ConfigurationInterface */ private function addTerminalNotifierNotifier() { - $treeBuilder = new TreeBuilder('terminal_notifier_notifier'); - $rootNode = $this->getRootNode($treeBuilder, 'terminal_notifier_notifier'); + $name = 'terminal_notifier_notifier'; + $rootNode = $this->getRootNode(new TreeBuilder($name), $name); $rootNode ->addDefaultsIfNotSet() ->children() ->booleanNode('enabled')->defaultValue(true)->end() - ->scalarNode('service')->defaultValue('flasher.cli.terminal_notifier_notifier')->end() - ->scalarNode('binary')->defaultValue('notify-send')->end() + ->scalarNode('priority') + ->beforeNormalization() + ->always(function ($v) { return (int) $v; }) + ->end() + ->defaultValue(0) + ->end() + ->scalarNode('binary')->defaultValue('terminal-notifier')->end() ->arrayNode('binary_paths') ->beforeNormalization() ->ifString() @@ -271,11 +312,11 @@ final class Configuration implements ConfigurationInterface ->scalarNode('title')->defaultValue(null)->end() ->booleanNode('mute')->defaultValue(true)->end() ->arrayNode('icons') - ->prototype('scalar')->end() + ->prototype('variable')->end() ->defaultValue(array()) ->end() ->arrayNode('sounds') - ->prototype('scalar')->end() + ->prototype('variable')->end() ->defaultValue(array()) ->end() ->end() @@ -289,15 +330,20 @@ final class Configuration implements ConfigurationInterface */ private function addToasterNotifier() { - $treeBuilder = new TreeBuilder('toaster_send'); - $rootNode = $this->getRootNode($treeBuilder, 'toaster_send'); + $name = 'toaster'; + $rootNode = $this->getRootNode(new TreeBuilder($name), $name); $rootNode ->addDefaultsIfNotSet() ->children() ->booleanNode('enabled')->defaultValue(true)->end() - ->scalarNode('service')->defaultValue('flasher.cli.toaster_send')->end() - ->scalarNode('binary')->defaultValue('notify-send')->end() + ->scalarNode('priority') + ->beforeNormalization() + ->always(function ($v) { return (int) $v; }) + ->end() + ->defaultValue(0) + ->end() + ->scalarNode('binary')->defaultValue('toast')->end() ->arrayNode('binary_paths') ->beforeNormalization() ->ifString() @@ -308,11 +354,11 @@ final class Configuration implements ConfigurationInterface ->scalarNode('title')->defaultValue(null)->end() ->booleanNode('mute')->defaultValue(true)->end() ->arrayNode('icons') - ->prototype('scalar')->end() + ->prototype('variable')->end() ->defaultValue(array()) ->end() ->arrayNode('sounds') - ->prototype('scalar')->end() + ->prototype('variable')->end() ->defaultValue(array()) ->end() ->end() diff --git a/src/Cli/Symfony/DependencyInjection/FlasherCliExtension.php b/src/Cli/Symfony/DependencyInjection/FlasherCliExtension.php index e5b06edf..60a755df 100644 --- a/src/Cli/Symfony/DependencyInjection/FlasherCliExtension.php +++ b/src/Cli/Symfony/DependencyInjection/FlasherCliExtension.php @@ -17,7 +17,6 @@ final class FlasherCliExtension extends Extension $configuration = new Configuration(); $config = $this->processConfiguration($configuration, $configs); - dd($config); $loader = new Loader\PhpFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); $loader->load('config.php'); @@ -37,7 +36,7 @@ final class FlasherCliExtension extends Extension $this->registerNotifier($container, $config, 'notify_send'); $this->registerNotifier($container, $config, 'snore_toast_notifier'); $this->registerNotifier($container, $config, 'terminal_notifier_notifier'); - $this->registerNotifier($container, $config, 'toaster_send'); + $this->registerNotifier($container, $config, 'toaster'); } private function registerNotifier(ContainerBuilder $container, array $config, $notifier) @@ -49,11 +48,12 @@ final class FlasherCliExtension extends Extension private function createConfigFor(array $config, $notifier) { - $options = $config[$notifier]; + $options = $config['notifiers'][$notifier]; $options['title'] = $config['title']; $options['mute'] = $config['mute']; $options['icons'] = array_replace_recursive($config['icons'], $options['icons']); + $options['sounds'] = array_replace_recursive($config['sounds'], $options['sounds']); return $options; } diff --git a/src/Cli/Symfony/Resources/config/config.php b/src/Cli/Symfony/Resources/config/config.php index 09273ff9..8190b4fd 100644 --- a/src/Cli/Symfony/Resources/config/config.php +++ b/src/Cli/Symfony/Resources/config/config.php @@ -63,7 +63,7 @@ $container ->addTag('flasher.cli_notifier'); $container - ->register('flasher.cli.toaster_send', 'Flasher\Cli\Prime\Notifier\ToasterNotifier') + ->register('flasher.cli.toaster', 'Flasher\Cli\Prime\Notifier\ToasterNotifier') ->addArgument(array()) ->addTag('flasher.cli_notifier');