| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace XHGui\Twig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Slim\Router; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Slim\Slim as App; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Twig\Extension\AbstractExtension; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Twig\TwigFilter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Twig\TwigFunction; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | class TwigExtension extends AbstractExtension | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     /** @var App */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     protected $_app; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     /** @var Router */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     private $router; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     /** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     private $pathPrefix; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     public function __construct(App $app) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         $this->_app = $app; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         $this->router = $app->router(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         $this->pathPrefix = $app->config('path.prefix'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     public function getFunctions(): array | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |         $options = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |             'is_safe' => ['html'], | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |         return [ | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |             new TwigFunction('url', [$this, 'url']), | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |             new TwigFunction('static', [$this, 'staticUrl']), | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |             new TwigFunction('percent', [$this, 'makePercent'], $options), | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     public function getFilters(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $options = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             'is_safe' => ['html'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             new TwigFilter('as_bytes', [$this, 'formatBytes'], $options), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             new TwigFilter('as_time', [$this, 'formatTime'], $options), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             new TwigFilter('as_diff', [$this, 'formatDiff'], $options), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             new TwigFilter('as_percent', [$this, 'formatPercent'], $options), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             new TwigFilter('truncate', [$this, 'truncate']), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     public function truncate(string $input, int $length = 50): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         if (strlen($input) < $length) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             return $input; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         return substr($input, 0, $length) . "\xe2\x80\xa6"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * Get a URL for xhgui. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * @param string $name The file/path you want a link to | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * @param array|null $queryargs additional querystring arguments | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * @return string url | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     public function url(string $name, $queryargs = []): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         $query = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         if ($queryargs) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             $query = '?' . http_build_query($queryargs); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         // this is copy of \Slim\Slim::urlFor() to mix path prefix in | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         // \Slim\Slim::urlFor | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         return rtrim($this->pathPrefix(), '/') . $this->router->urlFor($name) . $query; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * Get the URL for static content relative to webroot | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * @param string $path The file/path you want a link to | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * @return string url | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     public function staticUrl(string $path): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         $rootUri = $this->pathPrefix(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         return rtrim($rootUri, '/') . '/' . $path; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     public function formatBytes($value): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         return number_format((float)$value) . ' <span class="units">bytes</span>'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     public function formatTime($value): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         return number_format((float)$value) . ' <span class="units">µs</span>'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     public function formatDiff($value): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         $class = $value > 0 ? 'diff-up' : 'diff-down'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         if ($value == 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             $class = 'diff-same'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         return sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             '<span class="%s">%s</span>', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             $class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |             number_format((float)$value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     public function makePercent($value, $total): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         $value = (false === empty($total)) ? $value / $total : 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         return $this->formatPercent($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     public function formatPercent($value): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         return number_format((float)$value * 100, 0) . ' <span class="units">%</span>'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     private function pathPrefix(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         if ($this->pathPrefix !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |             return $this->pathPrefix; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         $request = $this->_app->request(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         $rootUri = $request->getRootUri(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         // Get URL part prepending index.php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         $indexPos = strpos($rootUri, 'index.php'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         if ($indexPos > 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |             return substr($rootUri, 0, $indexPos); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         return $rootUri; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 150 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 151 |  |  |  |