| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the puli/manager package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * (c) Bernhard Schussek <[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 Puli\Manager\Factory\Generator\Repository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Puli\Manager\Api\Factory\Generator\GeneratorRegistry; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Puli\Manager\Api\Factory\Generator\ServiceGenerator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Puli\Manager\Api\Php\Import; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Puli\Manager\Api\Php\Method; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Puli\Manager\Assert\Assert; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Webmozart\PathUtil\Path; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * Generates the setup code for a {@link PathMappingRepository}. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * @since  1.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * @author Bernhard Schussek <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | class PathMappingRepositoryGenerator implements ServiceGenerator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     private static $defaultOptions = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         'store' => array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |             'type' => 'null', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         'optimize' => false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 39 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 40 | 35 |  |     public function generateNewInstance($varName, Method $targetMethod, GeneratorRegistry $generatorRegistry, array $options = array()) | 
            
                                                        
            
                                    
            
            
                | 41 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 42 | 35 |  |         Assert::keyExists($options, 'rootDir', 'The "rootDir" option is missing.'); | 
            
                                                        
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 44 | 34 |  |         $options = array_replace_recursive(self::$defaultOptions, $options); | 
            
                                                        
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 46 | 34 |  |         $kvsGenerator = $generatorRegistry->getServiceGenerator(GeneratorRegistry::KEY_VALUE_STORE, $options['store']['type']); | 
            
                                                        
            
                                    
            
            
                | 47 | 34 |  |         $kvsOptions = $options['store']; | 
            
                                                        
            
                                    
            
            
                | 48 | 34 |  |         $kvsOptions['rootDir'] = $options['rootDir']; | 
            
                                                        
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 50 | 34 |  |         if ('json-file' === $kvsOptions['type']) { | 
            
                                                        
            
                                    
            
            
                | 51 | 27 |  |             $kvsOptions['serializeStrings'] = false; | 
            
                                                        
            
                                    
            
            
                | 52 | 27 |  |             $kvsOptions['serializeArrays'] = false; | 
            
                                                        
            
                                    
            
            
                | 53 | 27 |  |             $kvsOptions['escapeSlash'] = false; | 
            
                                                        
            
                                    
            
            
                | 54 | 27 |  |             $kvsOptions['prettyPrint'] = true; | 
            
                                                        
            
                                    
            
            
                | 55 | 27 |  |         } | 
            
                                                        
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 57 | 34 |  |         $kvsGenerator->generateNewInstance('store', $targetMethod, $generatorRegistry, $kvsOptions); | 
            
                                                        
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 59 | 34 |  |         $relPath = Path::makeRelative($options['rootDir'], $targetMethod->getClass()->getDirectory()); | 
            
                                                        
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 61 |  |  |         $escPath = $relPath | 
            
                                                        
            
                                    
            
            
                | 62 | 34 |  |             ? '__DIR__.'.var_export('/'.$relPath, true) | 
            
                                                        
            
                                    
            
            
                | 63 | 34 |  |             : '__DIR__'; | 
            
                                                        
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 65 | 34 |  |         $className = ($options['optimize'] ? 'Optimized' : '').'PathMappingRepository'; | 
            
                                                        
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 67 | 34 |  |         $targetMethod->getClass()->addImport(new Import('Puli\\Repository\\'.$className)); | 
            
                                                        
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 69 | 34 |  |         $targetMethod->addBody(sprintf('$%s = new %s($store, %s);', $varName, $className, $escPath)); | 
            
                                                        
            
                                    
            
            
                | 70 | 34 |  |     } | 
            
                                                        
            
                                    
            
            
                | 71 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 72 |  |  |  |