]> Raphaël G. Git Repositories - blogbundle/commitdiff
Add blog configuration and extension dependency injection
authorRaphaël Gertz <git@rapsys.eu>
Fri, 10 Nov 2023 12:43:07 +0000 (13:43 +0100)
committerRaphaël Gertz <git@rapsys.eu>
Fri, 10 Nov 2023 12:43:36 +0000 (13:43 +0100)
DependencyInjection/Configuration.php [new file with mode: 0644]
DependencyInjection/RapsysBlogExtension.php [new file with mode: 0644]

diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php
new file mode 100644 (file)
index 0000000..ae8a34f
--- /dev/null
@@ -0,0 +1,199 @@
+<?php declare(strict_types=1);
+
+/*
+ * This file is part of the Rapsys BlogBundle package.
+ *
+ * (c) Raphaël Gertz <symfony@rapsys.eu>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Rapsys\BlogBundle\DependencyInjection;
+
+use Symfony\Component\Config\Definition\Builder\TreeBuilder;
+use Symfony\Component\Config\Definition\ConfigurationInterface;
+
+use Rapsys\BlogBundle\RapsysBlogBundle;
+
+/**
+ * This is the class that validates and merges configuration from your app/config files.
+ *
+ * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/configuration.html}
+ */
+class Configuration implements ConfigurationInterface {
+       /**
+        * {@inheritdoc}
+        */
+       public function getConfigTreeBuilder(): TreeBuilder {
+               $treeBuilder = new TreeBuilder($alias = RapsysBlogBundle::getAlias());
+
+               // Here you should define the parameters that are allowed to
+               // configure your bundle. See the documentation linked above for
+               // more information on that topic.
+               //Set defaults
+               $defaults = [
+                       'contact' => [
+                               'name' => 'Rapsys blog',
+                               'mail' => 'blog@rapsys.eu'
+                       ],
+                       'copy' => [
+                               'by' => 'Rapsys',
+                               'link' => 'https://rapsys.eu',
+                               'long' => 'All rights reserved',
+                               'short' => 'Copyright 2019-2023',
+                               'title' => 'By Rapsys'
+                       ],
+                       'donate' => 'https://paypal.me/milongaraphael',
+                       'facebook' => [
+                               'apps' => [ 3728770287223690 ],
+                               'height' => 630,
+                               'width' => 1200
+                       ],
+                       'icon' => [
+                               'ico' => '@RapsysBlog/ico/icon.ico',
+                               //The png icon array
+                               //XXX: see https://www.emergeinteractive.com/insights/detail/the-essentials-of-favicons/
+                               //XXX: see https://caniuse.com/#feat=link-icon-svg
+                               'png' => [
+                                       //Default
+                                       256 => '@RapsysBlog/png/icon.256.png',
+
+                                       //For google
+                                       //Chrome for Android home screen icon
+                                       196 => '@RapsysBlog/png/icon.196.png',
+                                       //Google Developer Web App Manifest Recommendation
+                                       192 => '@RapsysBlog/png/icon.192.png',
+                                       //Chrome Web Store icon
+                                       128 => '@RapsysBlog/png/icon.128.png',
+
+                                       //Fallback
+                                       32 => '@RapsysBlog/png/icon.32.png',
+
+                                       //For apple
+                                       //XXX: old obsolete format: [57, 72, 76, 114, 120, 144]
+                                       //XXX: see https://webhint.io/docs/user-guide/hints/hint-apple-touch-icons/
+                                       //XXX: see https://developer.apple.com/library/archive/documentation/AppleApplications/Reference/SafariWebContent/ConfiguringWebApplications/ConfiguringWebApplications.html
+                                       //iPhone Retina
+                                       180 => '@RapsysBlog/png/icon.180.png',
+                                       //iPad Retina touch icon
+                                       167 => '@RapsysBlog/png/icon.167.png',
+                                       //iPad touch icon
+                                       152 => '@RapsysBlog/png/icon.152.png',
+                                       //iOS7
+                                       120 => '@RapsysBlog/png/icon.120.png',
+
+                                       //For windows
+                                       //XXX: see https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/platform-apis/dn255024(v=vs.85)
+                                       310 => '@RapsysBlog/png/icon.310.png',
+                                       150 => '@RapsysBlog/png/icon.150.png',
+                                       70 => '@RapsysBlog/png/icon.70.png'
+                               ],
+                               'svg' => '@RapsysBlog/svg/icon.svg'
+                       ],
+                       //XXX: revert to underscore because of that shit:
+                       //XXX: see https://symfony.com/doc/current/components/config/definition.html#normalization
+                       //XXX: see https://github.com/symfony/symfony/issues/7405
+                       //TODO: copy to '%rapsys_user.languages%',
+                       'languages' => [
+                               'en_gb' => 'English',
+                               'fr_fr' => 'French'
+                       ],
+                       //TODO: copy to '%kernel.default_locale%'
+                       'locale' => 'fr_fr',
+                       //TODO: copy to '%kernel.translator.fallbacks%'
+                       'locales' => [ 'fr_fr', 'en_gb' ],
+                       'logo' => [
+                               'png' => '@RapsysBlog/png/logo.png',
+                               'svg' => '@RapsysBlog/svg/logo.svg',
+                               'alt' => 'Rapsys\' dev log logo'
+                       ],
+                       'path' => is_link(($prefix = is_dir('public') ? './public/' : './').($link = 'bundles/'.str_replace('_', '', $alias))) && is_dir(realpath($prefix.$link)) || is_dir($prefix.$link) ? $link : dirname(__DIR__).'/Resources/public',
+                       'root' => 'rapsys_blog',
+                       'title' => 'Rapsys\' dev log',
+               ];
+
+               //Here we define the parameters that are allowed to configure the bundle.
+               //TODO: see https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php for default value and description
+               //TODO: see http://symfony.com/doc/current/components/config/definition.html
+               //XXX: use bin/console config:dump-reference to dump class infos
+
+               //Here we define the parameters that are allowed to configure the bundle.
+               $treeBuilder
+                       //Parameters
+                       ->getRootNode()
+                               ->addDefaultsIfNotSet()
+                               ->children()
+                                       ->arrayNode('contact')
+                                               ->addDefaultsIfNotSet()
+                                               ->children()
+                                                       ->scalarNode('name')->cannotBeEmpty()->defaultValue($defaults['contact']['name'])->end()
+                                                       ->scalarNode('mail')->cannotBeEmpty()->defaultValue($defaults['contact']['mail'])->end()
+                                               ->end()
+                                       ->end()
+                                       ->arrayNode('copy')
+                                               ->addDefaultsIfNotSet()
+                                               ->children()
+                                                       ->scalarNode('by')->defaultValue($defaults['copy']['by'])->end()
+                                                       ->scalarNode('link')->defaultValue($defaults['copy']['link'])->end()
+                                                       ->scalarNode('long')->defaultValue($defaults['copy']['long'])->end()
+                                                       ->scalarNode('short')->defaultValue($defaults['copy']['short'])->end()
+                                                       ->scalarNode('title')->defaultValue($defaults['copy']['title'])->end()
+                                               ->end()
+                                       ->end()
+                                       ->scalarNode('donate')->cannotBeEmpty()->defaultValue($defaults['donate'])->end()
+                                       //XXX: facebook required ???
+                                       //@see https://symfony.com/doc/current/components/config/definition.html
+                                       //->beforeNormalization()->castToArray()->end()
+                                       ->arrayNode('facebook')
+                                               ->addDefaultsIfNotSet()
+                                               ->children()
+                                                       ->arrayNode('apps')
+                                                               ->treatNullLike([])
+                                                               ->defaultValue($defaults['facebook']['apps'])
+                                                               ->scalarPrototype()->end()
+                                                       ->end()
+                                                       ->integerNode('height')->min(0)->defaultValue($defaults['facebook']['height'])->end()
+                                                       ->integerNode('width')->min(0)->defaultValue($defaults['facebook']['width'])->end()
+                                               ->end()
+                                       ->end()
+                                       ->arrayNode('icon')
+                                               ->addDefaultsIfNotSet()
+                                               ->children()
+                                                       ->scalarNode('ico')->defaultValue($defaults['icon']['ico'])->end()
+                                                       ->arrayNode('png')
+                                                               ->treatNullLike([])
+                                                               ->defaultValue($defaults['icon']['png'])
+                                                               ->scalarPrototype()->end()
+                                                       ->end()
+                                                       ->scalarNode('svg')->defaultValue($defaults['icon']['svg'])->end()
+                                               ->end()
+                                       ->end()
+                                       #TODO: see if we can't prevent key normalisation with ->normalizeKeys(false)
+                                       ->variableNode('languages')
+                                               ->treatNullLike([])
+                                               ->defaultValue($defaults['languages'])
+                                       ->end()
+                                       ->scalarNode('locale')->cannotBeEmpty()->defaultValue($defaults['locale'])->end()
+                                       #TODO: see if we can't prevent key normalisation with ->normalizeKeys(false)
+                                       ->variableNode('locales')
+                                               ->treatNullLike([])
+                                               ->defaultValue($defaults['locales'])
+                                       ->end()
+                                       ->arrayNode('logo')
+                                               ->addDefaultsIfNotSet()
+                                               ->children()
+                                                       ->scalarNode('png')->defaultValue($defaults['logo']['png'])->end()
+                                                       ->scalarNode('svg')->defaultValue($defaults['logo']['svg'])->end()
+                                                       ->scalarNode('alt')->defaultValue($defaults['logo']['alt'])->end()
+                                               ->end()
+                                       ->end()
+                                       ->scalarNode('path')->defaultValue($defaults['path'])->end()
+                                       ->scalarNode('root')->cannotBeEmpty()->defaultValue($defaults['root'])->end()
+                                       ->scalarNode('title')->cannotBeEmpty()->defaultValue($defaults['title'])->end()
+                               ->end()
+                       ->end();
+
+               return $treeBuilder;
+       }
+}
diff --git a/DependencyInjection/RapsysBlogExtension.php b/DependencyInjection/RapsysBlogExtension.php
new file mode 100644 (file)
index 0000000..d464df4
--- /dev/null
@@ -0,0 +1,100 @@
+<?php declare(strict_types=1);
+
+/*
+ * This file is part of the Rapsys BlogBundle package.
+ *
+ * (c) Raphaël Gertz <symfony@rapsys.eu>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Rapsys\BlogBundle\DependencyInjection;
+
+use Symfony\Component\DependencyInjection\ContainerBuilder;
+use Symfony\Component\DependencyInjection\Extension\Extension;
+use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
+use Symfony\Component\Translation\Loader\ArrayLoader;
+
+use Rapsys\BlogBundle\RapsysBlogBundle;
+
+/**
+ * This is the class that loads and manages your bundle configuration.
+ *
+ * @link http://symfony.com/doc/current/cookbook/bundles/extension.html
+ */
+class RapsysBlogExtension extends Extension implements PrependExtensionInterface {
+       /**
+        * Prepend the configuration
+        *
+        * @desc Preload the configuration to allow sourcing as parameters
+        * {@inheritdoc}
+        */
+       public function prepend(ContainerBuilder $container): void {
+               //Process the configuration
+               $configs = $container->getExtensionConfig($this->getAlias());
+
+               //Load configuration
+               $configuration = $this->getConfiguration($configs, $container);
+
+               //Process the configuration to get merged config
+               $config = $this->processConfiguration($configuration, $configs);
+
+               //Detect when no user configuration is provided
+               if ($configs === [[]]) {
+                       //Prepend default config
+                       $container->prependExtensionConfig($this->getAlias(), $config);
+               }
+
+               //Save configuration in parameters
+               $container->setParameter($this->getAlias(), $config);
+
+               //Store flattened array in parameters
+               //XXX: don't flatten rapsys_blog.icon.png key which is required to be an array
+               foreach($this->flatten($config, $this->getAlias(), 10, '.', ['rapsys_blog.icon.png', 'rapsys_blog.facebook.apps', 'rapsys_blog.locales', 'rapsys_blog.languages']) as $k => $v) {
+                       $container->setParameter($k, $v);
+               }
+       }
+
+       /**
+        * {@inheritdoc}
+        */
+       public function load(array $configs, ContainerBuilder $container): void {
+       }
+
+       /**
+        * {@inheritdoc}
+        */
+       public function getAlias(): string {
+               return RapsysBlogBundle::getAlias();
+       }
+
+       /**
+        * The function that parses the array to flatten it into a one level depth array
+        *
+        * @param $array The config values array
+        * @param $path The current key path
+        * @param $depth The maxmium depth
+        * @param $sep The separator string
+        * @param $skip The skipped paths array
+        * @return array The result array
+        */
+       protected function flatten($array, $path = '', $depth = 10, $sep = '.', $skip = []): array {
+               //Init res
+               $res = [];
+
+               //Flatten hashed array until depth reach zero
+               if ($depth && is_array($array) && $array !== [] && !in_array($path, $skip)) {
+                       foreach($array as $k => $v) {
+                               $sub = $path ? $path.$sep.$k:$k;
+                               $res += $this->flatten($v, $sub, $depth - 1, $sep, $skip);
+                       }
+               //Pass scalar value directly
+               } else {
+                       $res[$path] = $array;
+               }
+
+               //Return result
+               return $res;
+       }
+}