| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the CRUDlexSilex2 package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * (c) Philip Lehmann-Böhm <[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 CRUDlex\Silex; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use CRUDlex\EntityDefinitionFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use CRUDlex\EntityDefinitionValidator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use CRUDlex\Service; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use League\Flysystem\Adapter\Local; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use League\Flysystem\Filesystem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Pimple\Container; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Pimple\ServiceProviderInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Silex\Api\BootableProviderInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Silex\Application; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use Silex\Provider\LocaleServiceProvider; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use Silex\Provider\SessionServiceProvider; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | use Silex\Provider\TranslationServiceProvider; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | use Silex\Provider\TwigServiceProvider; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | use Symfony\Component\Translation\Loader\YamlFileLoader; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  * The ServiceProvider setups and initializes the service for Silex. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | class ServiceProvider implements ServiceProviderInterface, BootableProviderInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * Holds the directory of the locales. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     protected $localeDir; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * Initializes the available locales. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @param Container $app | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * the application container | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     protected function initLocales(Container $app) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $locales = Service::getLocales(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $app['translator']->addLoader('yaml', new YamlFileLoader()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         foreach ($locales as $locale) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             $app['translator']->addResource('yaml', $this->localeDir.'/'.$locale.'.yml', $locale); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * Initializes needed but yet missing service providers. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * @param Container $app | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * the application container | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     protected function initMissingServiceProviders(Container $app) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         if (!$app->offsetExists('translator')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             $app->register(new LocaleServiceProvider()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             $app->register(new TranslationServiceProvider(), [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                 'locale_fallbacks' => ['en'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         if (!$app->offsetExists('session')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             $app->register(new SessionServiceProvider()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         if (!$app->offsetExists('twig')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             $app->register(new TwigServiceProvider()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * Creates an EntityDefinitionValidator according to the configuration. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * @param Container $app | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * the Container instance of the Silex application | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     protected function createEntityDefinitionValidator(Container $app) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         $doValidate = !$app->offsetExists('crud.validateentitydefinition') || $app['crud.validateentitydefinition'] === true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         $validator  = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         if ($doValidate) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             $validator = $app->offsetExists('crud.entitydefinitionvalidator') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |                 ? $app['crud.entitydefinitionvalidator'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                 : new EntityDefinitionValidator(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         return $validator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      * ServiceProvider constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     public function __construct() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         $this->localeDir = __DIR__.'/../../../../CRUDlex/src/locales'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * Sets the directory containing the locales. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      * @param string $localeDir | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * the directory containing the locales. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 112 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |     public function setLocaleDir($localeDir) | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |         $this->localeDir = $localeDir; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      * Implements ServiceProviderInterface::register() registering $app['crud']. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      * $app['crud'] contains an instance of the ServiceProvider afterwards. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      * @param Container $app | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      * the Container instance of the Silex application | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     public function register(Container $app) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         if (!$app->offsetExists('crud.filesystem')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             $app['crud.filesystem'] = new Filesystem(new Local(getcwd())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         $validator = $this->createEntityDefinitionValidator($app); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         $app['crud'] = function() use ($app, $validator) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |             $crudFileCachingDirectory = $app->offsetExists('crud.filecachingdirectory') ? $app['crud.filecachingdirectory'] : null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |             $entityDefinitionFactory  = $app->offsetExists('crud.entitydefinitionfactory') ? $app['crud.entitydefinitionfactory'] : new EntityDefinitionFactory(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |             $result                   = new Service($app['crud.file'], $crudFileCachingDirectory, $app['url_generator'], $app['translator'], $app['crud.datafactory'], $entityDefinitionFactory, $app['crud.filesystem'], $validator); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |             return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |      * Initializes the crud service right after boot. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |      * @param Application $app | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |      * the Container instance of the Silex application | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |     public function boot(Application $app) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         $this->initMissingServiceProviders($app); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         $this->initLocales($app); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         $twigSetup = new TwigSetup(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         $twigSetup->registerTwigExtensions($app); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 155 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 156 |  |  |  |