| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * This file is part of Symplify | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * Copyright (c) 2016 Tomas Votruba (http://tomasvotruba.cz). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | namespace Symplify\PHP7_Sculpin\DI; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Nette\DI\Compiler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Nette\DI\CompilerExtension; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Symfony\Component\Console\Command\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Symplify\PHP7_Sculpin\Console\Application; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Symplify\PHP7_Sculpin\Contract\Source\SourceFileFilter\SourceFileFilterInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Symplify\PHP7_Sculpin\DI\Helper\TypeAndCollectorTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Symplify\PHP7_Sculpin\Source\SourceFileStorage; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  | final class SculpinCompilerExtension extends CompilerExtension | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |     use TypeAndCollectorTrait; | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |      * @var string[] | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     private $defaultConfig = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |         'sourceDir' => '%appDir%/../../../source', | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |         'outputDir' => '%appDir%/../../../output', | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |         'postRoute' => 'blog/:year/:month/:day/:filename', | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |     ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 4 |  |     public function loadConfiguration() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 4 |  |         $config = $this->validateAndReturnConfig($this->defaultConfig); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 3 |  |         $this->loadConfigToParameters($config); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 3 |  |         $this->loadServicesFromConfig(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 38 | 3 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 2 |  |     public function beforeCompile() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 2 |  |         $this->collectByType(Application::class, Command::class, 'add'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 2 |  |         $this->collectByType(SourceFileStorage::class, SourceFileFilterInterface::class, 'addSourceFileFilter'); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 44 | 2 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 4 |  |     private function validateAndReturnConfig(array $defaultConfig) : array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 4 |  |         $defaultConfig = $this->validateConfig($defaultConfig); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 4 |  |         $defaultConfig['sourceDir'] = $this->expandParameter($defaultConfig['sourceDir']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 4 |  |         $this->ensureDirectoryExists($defaultConfig['sourceDir']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 3 |  |         $defaultConfig['sourceDir'] = realpath($defaultConfig['sourceDir']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 3 |  |         return $defaultConfig; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 55 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 3 |  |     private function loadConfigToParameters(array $config) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 3 |  |         $this->getContainerBuilder()->parameters += $config; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 60 | 3 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 3 |  |     private function loadServicesFromConfig() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 3 |  |         Compiler::loadDefinitions( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 3 |  |             $this->getContainerBuilder(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 3 |  |             $this->loadFromFile(__DIR__.'/../config/services.neon')['services'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         ); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 68 | 3 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 4 |  |     private function ensureDirectoryExists(string $sourceDir) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 4 |  |         if (!is_dir($sourceDir)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 1 |  |             throw new \Exception( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 1 |  |                 sprintf('Source directory "%s" was not found.', $sourceDir) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 77 | 3 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 4 |  |     private function expandParameter(string $value) : string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 4 |  |         return $this->getContainerBuilder() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 4 |  |             ->expand($value); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 84 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 85 |  |  |  | 
            
                        
This method has been deprecated.