| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace GeminiLabs\Pollux\Config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use GeminiLabs\Pollux\Application; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Symfony\Component\Yaml\Yaml; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 8 |  |  | class Config | 
            
                                                                        
                            
            
                                    
            
            
                | 9 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 10 |  |  | 	const RAW_STRINGS = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  | 		'__', '_n', '_x', 'sprintf', | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  | 	]; | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  | 	 * @var Application | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  | 	protected $app; | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 	public function __construct( Application $app ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 		$this->app = $app; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 22 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  | 	 * @return array | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 	public function get() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 		$configFile = $this->getFile(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 		$configYaml = $this->getYaml(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 		if( $this->shouldGenerate( $configYaml )) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 			file_put_contents( $configFile, sprintf( '<?php // DO NOT MODIFY THIS FILE DIRECTLY!%sreturn %s;', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 				PHP_EOL, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 				$this->parseYaml( $configYaml ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 			)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 		return include $configFile; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 38 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  | 	 * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 	public function getFile( $filename = 'pollux-config.php' ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 		$filename = apply_filters( 'pollux/config/dist/file', $filename ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 		$storagePath = trailingslashit( apply_filters( 'pollux/config/dist/location', WP_CONTENT_DIR )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 		if( !is_dir( $storagePath )) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 			mkdir( $storagePath, 0775 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 		return sprintf( '%s%s', $storagePath, $filename ); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 51 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  | 	 * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 	public function getYaml() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 		$theme = wp_get_theme(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 		$configYaml = apply_filters( 'pollux/config/src/file', 'pollux.yml' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 		$configLocations = apply_filters( 'pollux/config/src/location', [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 			trailingslashit( trailingslashit( $theme->theme_root ) . $theme->stylesheet ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 			trailingslashit( trailingslashit( $theme->theme_root ) . $theme->template ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 			trailingslashit( WP_CONTENT_DIR ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 			trailingslashit( ABSPATH ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 			trailingslashit( dirname( ABSPATH )), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 			trailingslashit( dirname( dirname( ABSPATH ))), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 		]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 		foreach( (array) $configLocations as $location ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 			if( !file_exists( $location . $configYaml ))continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 			return $location . $configYaml; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 		return $this->app->path( 'defaults.yml' ); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 73 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  | 	 * @param string $yamlFile | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  | 	 * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 	public function parseYaml( $yamlFile ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 		$config = wp_parse_args( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 			Yaml::parse( file_get_contents( $yamlFile )), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 			Yaml::parse( file_get_contents( $this->app->path( 'defaults.yml' ))) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 		return $this->parseRawStrings( var_export( $config, true )); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 86 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  | 	 * @param string $config | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  | 	 * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 	protected function parseRawStrings( $config ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 		$strings = apply_filters( 'pollux/config/raw_strings', static::RAW_STRINGS ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 		return stripslashes( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 			preg_replace( '/(\')((' . implode( '|', $strings ) . ')\(?.+\))(\')/', '$2', $config ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 		); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 98 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  | 	 * @param string $configYaml | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  | 	 * @return bool | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 	protected function shouldGenerate( $configYaml ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 		$configFile = $this->getFile(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 		if( !file_exists( $configFile )) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 			return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 		return filemtime( $configYaml ) >= filemtime( $configFile ); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 111 |  |  | 	} | 
            
                                                        
            
                                    
            
            
                | 112 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 113 |  |  |  |