vendor/wearemarketing/corebundle/DependencyInjection/Configuration.php line 250

Open in your IDE?
  1. <?php
  2. namespace WAM\Bundle\CoreBundle\DependencyInjection;
  3. use phpDocumentor\Reflection\Types\Self_;
  4. use Symfony\Component\Config\Definition\Builder\TreeBuilder;
  5. use Symfony\Component\Config\Definition\ConfigurationInterface;
  6. use WAM\Bundle\CoreBundle\Admin\CategoryAdmin;
  7. use WAM\Bundle\CoreBundle\Admin\PopupAdmin;
  8. use WAM\Bundle\CoreBundle\Admin\TagAdmin;
  9. use WAM\Bundle\CoreBundle\Entity\Context;
  10. use WAM\Bundle\CoreBundle\Exception\OptionsValidateException;
  11. use WAM\Bundle\CoreBundle\Manager\CategoryManager;
  12. use WAM\Bundle\CoreBundle\Manager\ContextManager;
  13. use WAM\Bundle\CoreBundle\Manager\LocaleManager;
  14. use WAM\Bundle\CoreBundle\Manager\PopupManager;
  15. use WAM\Bundle\CoreBundle\Manager\TagManager;
  16. use WAM\Bundle\CoreBundle\Model\Category;
  17. use WAM\Bundle\CoreBundle\Model\Email;
  18. use WAM\Bundle\CoreBundle\Model\Popup;
  19. use WAM\Bundle\CoreBundle\Model\Tag;
  20. use WAM\Bundle\CoreBundle\Util\NumberToken;
  21. /**
  22.  * This is the class that validates and merges configuration from your app/config files.
  23.  *
  24.  * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class}
  25.  */
  26. class Configuration implements ConfigurationInterface
  27. {
  28.     const SVG_PATH '%kernel.root_dir%/Resources/frontend/svg/initial';
  29.     const ROBOTS_PATH '%kernel.root_dir%/public/robots.txt';
  30.     const PDF_COMMAND_PATH '/opt/wkhtmltox/bin/wkhtmltopdf.sh';
  31.     const COOKIES_CLASS 'WAM\Bundle\CoreBundle\Entity\Cookies';
  32.     const COOKIES_ADMIN_CLASS 'WAM\Bundle\CoreBundle\Admin\CookiesAdmin';
  33.     const DEFAULT_TAG_CLASS Tag::class;
  34.     const DEFAULT_CATEGORY_CLASS Category::class;
  35.     const DEFAULT_CONTEXT_CLASS Context::class;
  36.     const DEFAULT_CATEGORY_CLASS_ADMIN CategoryAdmin::class;
  37.     const DEFAULT_TAG_CLASS_ADMIN TagAdmin::class;
  38.     const EMAIL_CLASS Email::class;
  39.     const DEFAULT_LOCALE_MATRIX = [
  40.         'es' => [
  41.             'hreflang' => 'es_ES',
  42.             'url' => null,
  43.             'label' => 'EspaƱol',
  44.         ],
  45.     ];
  46.     const DEFAULT_LOCALES = ['es'];
  47.     const DEFAULT_LOCALE_SWITCHE_MODE 'redirect_to_home';
  48.     const DEFAULT_ADMIN_LOCALE 'es';
  49.     const DEFAULT_ADMIN_PATH '/admin';
  50.     const DEFAULT_CONFIG_POPUP_ADMIN_CONTROLLER 'SonataAdminBundle:CRUD';
  51.     const DEFAULT_CONFIG_POPUP_ADMIN_TRANSLATION 'SonataAdminBundle';
  52.     const DEFAULT_CONFIG_ORM_ENABLED true;
  53.     const DEFAULT_CONFIG_MODEL_MANAGER_NAME 'default';
  54.     const DEFAULT_MIX_MANIFEST_PATH '%kernel.project_dir%/public/mix-manifest.json';
  55.     const DEFAULT_TRANSLATIONS_PATH '%kernel.project_dir%/public/translations.json';
  56.     /**
  57.      * {@inheritdoc}
  58.      */
  59.     public function getConfigTreeBuilder()
  60.     {
  61.         $treeBuilder = new TreeBuilder();
  62.         $treeBuilder->root('wam_core')
  63.             ->children()
  64.                 ->append($this->getLocaleNode())
  65.                 ->append($this->getAdminNode())
  66.                 ->append($this->getConfigNode())
  67.                 ->append($this->getCookiesNode())
  68.                 ->append($this->getEmailNode())
  69.                 ->append($this->getClassificationNode())
  70.                 ->append($this->getStringSequenceGeneratorsNode())
  71.                 ->arrayNode('preview')
  72.                     ->useAttributeAsKey('name')
  73.                     ->prototype('scalar')->end()
  74.                 ->end()
  75.                 ->arrayNode('mix')->addDefaultsIfNotSet()
  76.                     ->children()
  77.                         ->scalarNode('manifest_path')->defaultValue(self::DEFAULT_MIX_MANIFEST_PATH)->end()
  78.                         ->booleanNode('throw_exceptions')->defaultTrue()->end()
  79.                     ->end()
  80.                 ->end()
  81.                 ->scalarNode('privacy_menu_zone')->defaultValue('FORM_PRIVACY')->end()
  82.                 ->scalarNode('svg_folder_path')->defaultValue(self::SVG_PATH)->end()
  83.                 ->scalarNode('robots_path')->defaultValue(self::ROBOTS_PATH)->end()
  84.                 ->scalarNode('translations_path')->defaultValue(self::DEFAULT_TRANSLATIONS_PATH)->end()
  85.                 ->arrayNode('excluded_searchable_managers')->defaultValue([])
  86.                     ->prototype('scalar')->end()
  87.                 ->end()
  88.                 ->arrayNode('pdf')->addDefaultsIfNotSet()
  89.                     ->children()
  90.                         ->scalarNode('command_path')->defaultValue(self::PDF_COMMAND_PATH)
  91.                             ->isRequired()->cannotBeEmpty()
  92.                         ->end()
  93.                     ->end()
  94.                 ->end()
  95.                 ->end()
  96.             ->end();
  97.         return $treeBuilder;
  98.     }
  99.     public function getCookiesNode()
  100.     {
  101.         $root = (new TreeBuilder())->root('cookies');
  102.         $root->addDefaultsIfNotSet()
  103.             ->children()
  104.                 ->scalarNode('class')->defaultValue(self::COOKIES_CLASS)->end()
  105.                 ->scalarNode('admin_class')->defaultValue(self::COOKIES_ADMIN_CLASS)->end()
  106.             ->end()
  107.         ->end();
  108.         return $root;
  109.     }
  110.     public function getEmailNode()
  111.     {
  112.         $node = (new TreeBuilder())->root('email');
  113.         $node->addDefaultsIfNotSet()
  114.             ->children()
  115.                 ->scalarNode('class')->defaultValue(self::EMAIL_CLASS)->end()
  116.                 ->arrayNode('types')
  117.                     ->useAttributeAsKey('event'false)
  118.                     ->defaultValue([])
  119.                     ->arrayPrototype()
  120.                     ->children()
  121.                         ->scalarNode('name')->end()
  122.                         ->scalarNode('template_path')->end()
  123.                         ->arrayNode('elements')
  124.                             ->arrayPrototype()
  125.                             ->children()
  126.                                 ->scalarNode('name')->end()
  127.                                 ->scalarNode('description')->end()
  128.                                 ->scalarNode('type')->end()
  129.                             ->end()
  130.                         ->end()
  131.                     ->end()
  132.                     ->arrayNode('variables')
  133.                         ->arrayPrototype()
  134.                         ->children()
  135.                             ->scalarNode('name')->end()
  136.                             ->scalarNode('description')->end()
  137.                         ->end()
  138.                     ->end()
  139.                 ->end()
  140.             ->end()
  141.         ->end();
  142.         return $node;
  143.     }
  144.     public function getStringSequenceGeneratorsNode()
  145.     {
  146.         $root = (new TreeBuilder())->root('string_sequence_generators');
  147.         $root->addDefaultsIfNotSet([])
  148.             ->children()
  149.                 ->arrayNode('sequences')
  150.                     ->useAttributeAsKey('name')
  151.                     ->arrayPrototype()
  152.                     ->children()
  153.                         ->arrayNode('tokens')->defaultValue([])
  154.                             ->prototype('array')
  155.                             ->validate()
  156.                                 ->ifTrue(function ($value) {
  157.                                     if (isset($value['max_value'])) {
  158.                                         return NumberToken::class == $value['type']
  159.                                             and (strlen($value['max_value']) != strlen($value['base_value']));
  160.                                     }
  161.                                     return false;
  162.                                 })->thenInvalid('Config "max_value" has to length same as base_value')
  163.                             ->end()
  164.                             ->validate()
  165.                                 ->ifTrue(function ($value) {
  166.                                     return NumberToken::class == $value['type']
  167.                                         and (!isset($value['max_value']) or !$value['max_value']);
  168.                                 })->thenInvalid('Config "max_value" for number tokens is required')
  169.                             ->end()
  170.                             ->children()
  171.                                 ->scalarNode('token')->isRequired()->cannotBeEmpty()->end()
  172.                                 ->scalarNode('type')->isRequired()->cannotBeEmpty()->end()
  173.                                 ->scalarNode('base_value')->isRequired()->cannotBeEmpty()->end()
  174.                                 ->scalarNode('max_value')->end()
  175.                             ->end()
  176.                         ->end()
  177.                     ->end()
  178.                 ->end()
  179.             ->end()
  180.         ->end();
  181.         return $root;
  182.     }
  183.     public function getClassificationNode()
  184.     {
  185.         $root = (new TreeBuilder())->root('classification');
  186.         $root->addDefaultsIfNotSet()
  187.             ->children()
  188.                 ->arrayNode('class')->addDefaultsIfNotSet()
  189.                     ->children()
  190.                         ->scalarNode('tag')->cannotBeEmpty()
  191.                             ->defaultValue(self::DEFAULT_TAG_CLASS)->end()
  192.                         ->scalarNode('category')->cannotBeEmpty()
  193.                             ->defaultValue(self::DEFAULT_CATEGORY_CLASS)
  194.                         ->end()
  195.                         ->arrayNode('category_mapping')
  196.                             ->prototype('array')
  197.                                 ->children()
  198.                                     ->scalarNode('key')->end()
  199.                                     ->scalarNode('name')->end()
  200.                                     ->scalarNode('class')->end()
  201.                                 ->end()
  202.                             ->end()
  203.                         ->end()
  204.                         ->scalarNode('context')->cannotBeEmpty()
  205.                             ->defaultValue(self::DEFAULT_CONTEXT_CLASS)->end()
  206.                     ->end()
  207.                 ->end()
  208.                 ->arrayNode('admin')->addDefaultsIfNotSet()
  209.                     ->children()
  210.                         ->scalarNode('tag')->cannotBeEmpty()
  211.                             ->defaultValue(self::DEFAULT_TAG_CLASS_ADMIN)->end()
  212.                         ->scalarNode('category')->cannotBeEmpty()
  213.                             ->defaultValue(self::DEFAULT_CATEGORY_CLASS_ADMIN)->end()
  214.                     ->end()
  215.                 ->end()
  216.             ->end()
  217.         ->end();
  218.         return $root;
  219.     }
  220.     public function getLocaleNode()
  221.     {
  222.         $node = (new TreeBuilder())->root('locale');
  223.         $node
  224.             ->addDefaultsIfNotSet()
  225.             ->children()
  226.                 ->arrayNode('locales')->defaultValue(self::DEFAULT_LOCALES)
  227.                     ->prototype('scalar')->end()
  228.                 ->end()
  229.                 ->scalarNode('admin_locale')->defaultValue(self::DEFAULT_ADMIN_LOCALE)->end()
  230.                 ->scalarNode('admin_path')->defaultValue(self::DEFAULT_ADMIN_PATH)->end()
  231.                 ->arrayNode('disabled_locales')->defaultValue([])
  232.                     ->prototype('scalar')->end()
  233.                 ->end()
  234.                 ->scalarNode('guess_locale')->defaultFalse()->end()
  235.                 ->enumNode('locale_switcher_mode')->defaultValue(self::DEFAULT_LOCALE_SWITCHE_MODE)
  236.                     ->values(['redirect_to_home''redirect_to_same'])
  237.                 ->end()
  238.                 ->arrayNode('fallback')
  239.                 ->addDefaultsIfNotSet()
  240.                     ->children()
  241.                         ->scalarNode(LocaleManager::HREFLANG)->defaultValue('es_ES')->end()
  242.                         ->scalarNode(LocaleManager::URL)->defaultValue('%locale%')->end()
  243.                         ->scalarNode(LocaleManager::INTERNALS)->defaultValue('es')->end()
  244.                     ->end()
  245.                 ->end()
  246.                 ->arrayNode('matrix')->defaultValue(self::DEFAULT_LOCALE_MATRIX)
  247.                     ->useAttributeAsKey('name')
  248.                     ->prototype('array')
  249.                         ->children()
  250.                             ->scalarNode(LocaleManager::HREFLANG)->end()
  251.                             ->scalarNode(LocaleManager::URL)->end()
  252.                             ->scalarNode('label')->isRequired()->end()
  253.                         ->end()
  254.                     ->end()
  255.                 ->end()
  256.             ->end();
  257.         return $node;
  258.     }
  259.     public function getAdminNode()
  260.     {
  261.         $node = (new TreeBuilder())->root('admin');
  262.         $node
  263.             ->addDefaultsIfNotSet()
  264.             ->children()
  265.                 ->arrayNode('excluded_admin_groups')->defaultValue([])
  266.                     ->prototype('scalar')->end()
  267.                 ->end()
  268.                 ->arrayNode('translation')->addDefaultsIfNotSet()
  269.                     ->children()
  270.                         ->scalarNode('default_domain')->defaultValue('messages')->end()
  271.                         ->arrayNode('editable')->addDefaultsIfNotSet()
  272.                             ->children()
  273.                                 ->scalarNode('mode')->defaultValue('inline')->end()
  274.                                 ->scalarNode('type')->defaultValue('textarea')->end()
  275.                                 ->scalarNode('emptytext')->defaultValue('Empty')->end()
  276.                                 ->scalarNode('placement')->defaultValue('top')->end()
  277.                             ->end()
  278.                         ->end()
  279.                     ->end()
  280.                 ->end()
  281.                 ->arrayNode('templates')->addDefaultsIfNotSet()
  282.                     ->children()
  283.                         ->scalarNode('preview_form')
  284.                             ->defaultValue('@WAMAdminTheme/CRUD/preview_form.html.twig')->cannotBeEmpty()
  285.                         ->end()
  286.                     ->end()
  287.                 ->end()
  288.             ->end();
  289.         return $node;
  290.     }
  291.     public function getConfigNode()
  292.     {
  293.         $rootNode = (new TreeBuilder())->root('config');
  294.         $rootNode
  295.             ->addDefaultsIfNotSet()
  296.             ->children()
  297.                 ->scalarNode('model_manager_name')->cannotBeEmpty()
  298.                     ->defaultValue(self::DEFAULT_CONFIG_MODEL_MANAGER_NAME)
  299.                 ->end()
  300.                 ->booleanNode('orm_enabled')->defaultValue(self::DEFAULT_CONFIG_ORM_ENABLED)->end()
  301.             ->end();
  302.         return $rootNode;
  303.     }
  304.     public static function validateOptions($options)
  305.     {
  306.         self::validateLocaleOptions($options['locale']);
  307.     }
  308.     public static function validateLocaleOptions(array $options)
  309.     {
  310.         $accumulator = [];
  311.         foreach (LocaleManager::getTypes() as $type) {
  312.             $accumulator[$type] = [];
  313.         }
  314.         // inject the key (locale) as an 'internals' field
  315.         foreach ($options['matrix'] as $locale => $localeConfig) {
  316.             $options['matrix'][$locale]['internals'] = $locale;
  317.         }
  318.         foreach ($options['matrix'] as $name => $descriptor) {
  319.             foreach (LocaleManager::getTypes() as $type) {
  320.                 if (isset($accumulator[$type][$descriptor[$type]])) {
  321.                     throw new OptionsValidateException(sprintf('The identifier `%s` is already used in wam_core.locale.matrix, see `%s` and `%s` keys for `%s` type'$descriptor[$type], $accumulator[$type][$descriptor[$type]], $name$type));
  322.                 }
  323.                 $accumulator[$type][$descriptor[$type]] = $name;
  324.             }
  325.         }
  326.         if (count($options['locales']) != count($options['matrix'])) {
  327.             throw new OptionsValidateException(sprintf('The number of declared locales in matrix (%s) element is different than in locales (%s).'implode(','array_keys($options['matrix'])), implode(','$options['locales'])));
  328.         }
  329.         foreach ($options['locales'] as $locale) {
  330.             if (!array_key_exists($locale$options['matrix'])) {
  331.                 throw new OptionsValidateException(sprintf("The locale '%s' is not defined as a key of the matrix element."$locale));
  332.             }
  333.         }
  334.         return true;
  335.     }
  336. }