| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * Highlighter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * Copyright (C) 2016, Some right reserved. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @author Kacper "Kadet" Donat <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * Contact with author: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * Xmpp: [email protected] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * E-mail: [email protected] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * From Kadet with love. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | namespace Kadet\Highlighter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Kadet\Highlighter\Formatter\CliFormatter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Kadet\Highlighter\Formatter\FormatterInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Kadet\Highlighter\Formatter\HtmlFormatter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Kadet\Highlighter\Language\Language; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Kadet\Highlighter\Language\PlainText; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use Kadet\Highlighter\Utils\Singleton; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * KeyLighter helper class, used to simplify usage. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  * @package Kadet\Highlighter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | class KeyLighter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     use Singleton; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     const VERSION = '0.8.0-dev'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * Registered aliases | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     private $_languages = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         'name'      => [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         'mime'      => [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         'extension' => [] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     private $_formatters = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     /** @var FormatterInterface */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     private $_formatter = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * @param string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @return Language | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 1 |  |     public function getLanguage($name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 1 |  |         $embedded = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 1 |  |         if (($pos = strpos($name, '>')) !== false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 1 |  |             $embedded[] = self::getLanguage(trim(substr($name, $pos + 1))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 1 |  |             $name       = trim(substr($name, 0, $pos)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 1 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 1 |  |         return $this->languageByName($name, [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             'embedded' => $embedded | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 1 |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 4 |  |     public function languageByName($name, $params = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 4 |  |         return isset($this->_languages['name'][$name]) ? | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 4 |  |             $this->_languages['name'][$name]($params) : | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 4 |  |             new PlainText($params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 2 |  |     public function languageByMime($mime, $params = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 2 |  |         return isset($this->_languages['mime'][$mime]) ? | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 2 |  |             $this->_languages['mime'][$mime]($params) : | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 2 |  |             new PlainText($params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 2 |  |     public function languageByExt($filename, $params = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 2 |  |         foreach($this->_languages['extension'] as $mask => $class) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 2 |  |             if(fnmatch($mask, $filename)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 2 |  |                 return $class($params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 2 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 1 |  |         return new PlainText($params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * @param callable|string $language | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * @param array[string]   $aliases | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      * @deprecated Will be removed in 1.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     public function registerLanguage($language, $aliases) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         $this->register($language, ['name' => $aliases]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 10 |  |     public function setDefaultFormatter(FormatterInterface $formatter) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 10 |  |         $this->_formatter = $formatter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 10 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 3 |  |     public function registeredLanguages($by = 'name', $class = false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         return array_map(function ($e) use($class) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 3 |  |             return $e([])->getFQN($class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 3 |  |         }, $this->_languages[$by]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 1 |  |     public function getDefaultFormatter() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 1 |  |         return $this->_formatter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     public function addFormatter($name, FormatterInterface $formatter) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         $this->_formatters[$name] = $formatter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  |     public function getFormatter($name) | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |         return isset($this->_formatters[$name]) ? $this->_formatters[$name] : false; | 
            
                                                                        
                            
            
                                    
            
            
                | 131 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     public function registeredFormatters() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         return $this->_formatters; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 | 3 |  |     public function highlight($source, Language $language, FormatterInterface $formatter = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 | 3 |  |         $formatter = $formatter ?: $this->getDefaultFormatter(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 | 3 |  |         return $formatter->format($language->parse($source)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 | 10 |  |     public function __construct() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 | 10 |  |         $this->setDefaultFormatter( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 | 10 |  |             php_sapi_name() === 'cli' ? new CliFormatter() : new HtmlFormatter() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 | 10 |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 | 10 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 | 1 |  |     public function init() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 | 1 |  |         foreach(include __DIR__.'/Config/aliases.php' as $alias) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 | 1 |  |             $class = $alias[0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 | 1 |  |             unset($alias[0]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 | 1 |  |             $this->register($class, $alias); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 | 1 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 | 1 |  |         $this->_formatters = include __DIR__.'/Config/formatters.php'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |      * @param string|callable $class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |      * @param array           $options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 | 12 |  |     public function register($class, array $options) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 | 12 |  |         if(!is_callable($class) && is_subclass_of($class, Language::class)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 | 12 |  |             $class = function($arguments = []) use ($class) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 | 11 |  |                 return new $class($arguments); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 | 12 |  |             }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 | 12 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 | 12 |  |         foreach($options as $name => $values) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 | 12 |  |             $this->_languages[$name] = array_merge($this->_languages[$name], array_fill_keys($values, $class)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 | 12 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 | 12 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 179 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 180 |  |  |  |