|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the EasyAdminBundle. |
| 5 | + * |
| 6 | + * (c) Javier Eguiluz <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace JavierEguiluz\Bundle\EasyAdminBundle\DependencyInjection; |
| 13 | + |
| 14 | +use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
| 15 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 16 | + |
| 17 | +class EasyAdminExtension extends Extension |
| 18 | +{ |
| 19 | + private $defaultConfigOptions = array( |
| 20 | + 'site_name' => 'ACME', |
| 21 | + 'list_max_results' => 15, |
| 22 | + 'list_actions' => array('edit'), |
| 23 | + 'entities' => array(), |
| 24 | + ); |
| 25 | + |
| 26 | + public function load(array $configs, ContainerBuilder $container) |
| 27 | + { |
| 28 | + $configuration = new Configuration(); |
| 29 | + $config = $this->processConfiguration($configuration, $configs); |
| 30 | + |
| 31 | + $options = array_replace($this->defaultConfigOptions, $config); |
| 32 | + $options['entities'] = $this->processEntityConfiguration($options['entities']); |
| 33 | + |
| 34 | + $container->setParameter('easy_admin.config', $options); |
| 35 | + } |
| 36 | + |
| 37 | + protected function processEntityConfiguration($entitiesConfiguration) |
| 38 | + { |
| 39 | + if (0 === count($entitiesConfiguration)) { |
| 40 | + return $entitiesConfiguration; |
| 41 | + } |
| 42 | + |
| 43 | + $entitiesConfigurationKeys = array_keys($entitiesConfiguration); |
| 44 | + if (is_integer($entitiesConfigurationKeys[0])) { |
| 45 | + return $this->processEntityConfigurationFromSimpleParameters($entitiesConfiguration); |
| 46 | + } |
| 47 | + |
| 48 | + return $this->processEntityConfigurationFromComplexParameters($entitiesConfiguration); |
| 49 | + } |
| 50 | + |
| 51 | + private function processEntityConfigurationFromSimpleParameters($config) |
| 52 | + { |
| 53 | + $entities = array(); |
| 54 | + foreach ($config as $entityClass) { |
| 55 | + $parts = explode('\\', $entityClass); |
| 56 | + $entityName = array_pop($parts); |
| 57 | + |
| 58 | + $entities[$entityName] = array( |
| 59 | + 'label' => $entityName, |
| 60 | + 'name' => $entityName, |
| 61 | + 'class' => $entityClass, |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + return $entities; |
| 66 | + } |
| 67 | + |
| 68 | + private function processEntityConfigurationFromComplexParameters($config) |
| 69 | + { |
| 70 | + $entities = array(); |
| 71 | + foreach ($config as $customEntityName => $entityConfiguration) { |
| 72 | + $parts = explode('\\', $entityConfiguration['class']); |
| 73 | + $realEntityName = array_pop($parts); |
| 74 | + |
| 75 | + // copy the original entity to not loose any of its configuration |
| 76 | + $entities[$realEntityName] = $config[$customEntityName]; |
| 77 | + |
| 78 | + // process the original configuration to use the format needed by the application |
| 79 | + $entities[$realEntityName]['label'] = $customEntityName; |
| 80 | + $entities[$realEntityName]['name'] = $realEntityName; |
| 81 | + $entities[$realEntityName]['class'] = $entityConfiguration['class']; |
| 82 | + } |
| 83 | + |
| 84 | + return $entities; |
| 85 | + } |
| 86 | +} |
0 commit comments