| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | * @package   s9e\TextFormatter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | * @copyright Copyright (c) 2010-2019 The s9e Authors | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | * @license   http://www.opensource.org/licenses/mit-license.php The MIT License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | namespace s9e\TextFormatter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | abstract class Bundle | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | 	* Return a cached instance of the parser | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | 	* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | 	* @return Parser | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | 	*/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 | 4 |  | 	public static function getCachedParser() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 | 4 |  | 		if (!isset(static::$parser)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 | 4 |  | 			static::$parser = static::getParser(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 | 4 |  | 		return static::$parser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 	* Return a cached instance of the renderer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 	* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 	* @return Renderer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 	*/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 5 |  | 	public static function getCachedRenderer() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 5 |  | 		if (!isset(static::$renderer)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 5 |  | 			static::$renderer = static::getRenderer(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 5 |  | 		return static::$renderer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 	* Return a new instance of s9e\TextFormatter\Parser | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 	* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 	* @return Parser | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 	*/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 	abstract public static function getParser(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 	* Return a new instance of s9e\TextFormatter\Renderer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 	* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 	* @return Renderer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 	*/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 	abstract public static function getRenderer(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 	* Return the source of the JavaScript parser if available | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 	* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 	* @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 	*/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 1 |  | 	public static function getJS() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 1 |  | 		return ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 	* Parse given text using a singleton instance of the bundled Parser | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 	* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 	* @param  string $text Original text | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 	* @return string       Intermediate representation | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 	*/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 4 |  | 	public static function parse($text) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 4 |  | 		if (isset(static::$beforeParse)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 1 |  | 			$text = call_user_func(static::$beforeParse, $text); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 4 |  | 		$xml = static::getCachedParser()->parse($text); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 4 |  | 		if (isset(static::$afterParse)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 1 |  | 			$xml = call_user_func(static::$afterParse, $xml); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 4 |  | 		return $xml; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 	* Render an intermediate representation using a singleton instance of the bundled Renderer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 	* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 	* @param  string $xml    Intermediate representation | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 	* @param  array  $params Stylesheet parameters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 	* @return string         Rendered result | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 95 |  |  | 	*/ | 
            
                                                                        
                            
            
                                    
            
            
                | 96 | 5 |  | 	public static function render($xml, array $params = []) | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  | 	{ | 
            
                                                                        
                            
            
                                    
            
            
                | 98 | 5 |  | 		$renderer = static::getCachedRenderer(); | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 100 | 5 |  | 		if (!empty($params)) | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  | 		{ | 
            
                                                                        
                            
            
                                    
            
            
                | 102 | 1 |  | 			$renderer->setParameters($params); | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 105 | 5 |  | 		if (isset(static::$beforeRender)) | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  | 		{ | 
            
                                                                        
                            
            
                                    
            
            
                | 107 | 1 |  | 			$xml = call_user_func(static::$beforeRender, $xml); | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 110 | 5 |  | 		$output = $renderer->render($xml); | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 112 | 5 |  | 		if (isset(static::$afterRender)) | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  | 		{ | 
            
                                                                        
                            
            
                                    
            
            
                | 114 | 1 |  | 			$output = call_user_func(static::$afterRender, $output); | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 117 | 5 |  | 		return $output; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 	* Reset the cached parser and renderer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 	* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 	* @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 	*/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 1 |  | 	public static function reset() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 | 1 |  | 		static::$parser   = null; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 | 1 |  | 		static::$renderer = null; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 	* Transform an intermediate representation back to its original form | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 	* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 	* @param  string $xml Intermediate representation | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 	* @return string      Original text | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 	*/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 | 3 |  | 	public static function unparse($xml) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 | 3 |  | 		if (isset(static::$beforeUnparse)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 | 1 |  | 			$xml = call_user_func(static::$beforeUnparse, $xml); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 | 3 |  | 		$text = Unparser::unparse($xml); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 | 3 |  | 		if (isset(static::$afterUnparse)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 | 1 |  | 			$text = call_user_func(static::$afterUnparse, $text); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 | 3 |  | 		return $text; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 152 |  |  | 	} | 
            
                                                        
            
                                    
            
            
                | 153 |  |  | } |