| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | abstract class Ajde_Resource extends Ajde_Object_Standard | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |     const TYPE_JAVASCRIPT = 'js'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |     const TYPE_STYLESHEET = 'css'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |     public function __construct($type) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |         $this->setType($type); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     public function __toString() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |         return implode(", ", $this->_data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     abstract public function getFilename(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     abstract protected function getLinkUrl(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     public function getType() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         return $this->get('type'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     public function setPosition($position) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         $this->set('position', $position); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     public function getPosition() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         return $this->get('position'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     protected function _getLinkTemplateFilename() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $format = $this->hasFormat() ? $this->getFormat() : null; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         return self::getLinkTemplateFilename($this->getType(), $format); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     public static function getLinkTemplateFilename($type, $format = 'null') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         if (Ajde::app()->getDocument()->hasLayout()) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             $layout = Ajde::app()->getDocument()->getLayout(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             $layout = new Ajde_Layout(config("layout.frontend")); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $format = issetor($format, 'html'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $dirPrefixPatterns = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             APP_DIR, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             CORE_DIR | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         foreach ($dirPrefixPatterns as $dirPrefixPattern) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             $prefixedLayout = $dirPrefixPattern . LAYOUT_DIR; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             if (self::exist($prefixedLayout . $layout->getName() . '/link/' . $type . '.' . $format . '.php')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                 return $prefixedLayout . $layout->getName() . '/link/' . $type . '.' . $format . '.php'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     protected static function exist($filename) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         if (is_file(self::realpath($filename))) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |     protected static function realpath($filename) | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |         return LOCAL_ROOT . $filename; | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     public static function encodeFingerprint($array) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         return self::_urlEncode(serialize($array)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     public static function decodeFingerprint($fingerprint) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         return unserialize(self::_urlDecode($fingerprint)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     public static function _urlDecode($string) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         // return self::_rotUrl($string); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         return base64_decode($string); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     public static function _urlEncode($string) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         // return self::_rotUrl($string); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         return base64_encode($string); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     public static function _rotUrl($string) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         return strtr($string, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             '/-:?=&%#{}"; QXJKVWPYRHGB abcdefghijklmnopqrstuv123456789ACDEFILMNOSTUwxyz', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             'QXJKVWPYRHGB /-:?=&%#{}"; 123456789ACDEFILMNOSTUabcdefghijklmnopqrstuvwxyz'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     public function getLinkCode() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         ob_start(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         // variables for use in included link template | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         $url       = $this->getLinkUrl(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         $arguments = $this->hasArguments() ? $this->getArguments() : ''; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         // create temporary resource for link filename | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         $linkFilename = $this->_getLinkTemplateFilename(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         // TODO: performance gain? | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         // Ajde_Cache::getInstance()->addFile($linkFilename); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         if ($linkFilename) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             include LOCAL_ROOT . $linkFilename; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |             throw new Ajde_Exception('Link filename for ' . $url . ' not found'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         $contents = ob_get_contents(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         ob_end_clean(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         return $contents; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     public function getContents() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         ob_start(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         $filename = $this->getFilename(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         Ajde_Cache::getInstance()->addFile($filename); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         if ($this->exist($filename)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |             include $this->realpath($filename); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         $contents = ob_get_contents(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         ob_end_clean(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         return $contents; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 151 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 152 |  |  |  | 
            
                        
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: