Total Complexity | 50 |
Total Lines | 202 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
Complex classes like Image often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Image, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class Image |
||
21 | { |
||
22 | /** |
||
23 | * Searches a appname, template and maybe language and type-specific image |
||
24 | * |
||
25 | * @param string $app |
||
26 | * @param string|array $image one or more image-name in order of precedence |
||
27 | * @param string $extension ='' extension to $image, makes sense only with an array |
||
28 | * @param boolean $add_cachebuster =false true: add a cachebuster to the returnd url |
||
29 | * |
||
30 | * @return string url of image or null if not found |
||
31 | */ |
||
32 | static function find($app,$image,$extension='',$add_cachebuster=false) |
||
33 | { |
||
34 | $image_map = self::map(null); |
||
35 | |||
36 | // array of images in descending precedence |
||
37 | if (is_array($image)) |
||
38 | { |
||
39 | foreach($image as $img) |
||
40 | { |
||
41 | if (($url = self::find($app, $img, $extension, $add_cachebuster))) |
||
42 | { |
||
43 | return $url; |
||
44 | } |
||
45 | } |
||
46 | //error_log(__METHOD__."('$app', ".array2string($image).", '$extension') NONE found!"); |
||
47 | return null; |
||
48 | } |
||
49 | |||
50 | $webserver_url = $GLOBALS['egw_info']['server']['webserver_url']; |
||
51 | |||
52 | // instance specific images have highest precedence |
||
53 | if (isset($image_map['vfs'][$image.$extension])) |
||
54 | { |
||
55 | $url = $webserver_url.$image_map['vfs'][$image.$extension]; |
||
56 | } |
||
57 | // then app specific ones |
||
58 | elseif(isset($image_map[$app][$image.$extension])) |
||
59 | { |
||
60 | $url = $webserver_url.$image_map[$app][$image.$extension]; |
||
61 | } |
||
62 | // then api |
||
63 | elseif(isset($image_map['api'][$image.$extension])) |
||
64 | { |
||
65 | $url = $webserver_url.$image_map['api'][$image.$extension]; |
||
66 | } |
||
67 | elseif(isset($image_map['phpgwapi'][$image.$extension])) |
||
68 | { |
||
69 | $url = $webserver_url.$image_map['phpgwapi'][$image.$extension]; |
||
70 | } |
||
71 | |||
72 | if (!empty($url)) |
||
73 | { |
||
74 | if ($add_cachebuster) |
||
75 | { |
||
76 | $url .= '?'.filemtime(EGW_SERVER_ROOT.substr($url, strlen($webserver_url))); |
||
77 | } |
||
78 | return $url; |
||
79 | } |
||
80 | |||
81 | // if image not found, check if it has an extension and try withoug |
||
82 | if (strpos($image, '.') !== false) |
||
83 | { |
||
84 | $name = null; |
||
85 | self::get_extension($image, $name); |
||
86 | return self::find($app, $name, $extension, $add_cachebuster); |
||
87 | } |
||
88 | //error_log(__METHOD__."('$app', '$image', '$extension') image NOT found!"); |
||
89 | return null; |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * Get extension (and optional basename without extension) of a given path |
||
94 | * |
||
95 | * @param string $path |
||
96 | * @param string &$name on return basename without extension |
||
97 | * @return string extension without dot, eg. 'php' |
||
98 | */ |
||
99 | protected static function get_extension($path, &$name=null) |
||
100 | { |
||
101 | $parts = explode('.', Vfs::basename($path)); |
||
102 | $ext = array_pop($parts); |
||
103 | $name = implode('.', $parts); |
||
104 | return $ext; |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * Scan filesystem for images of all apps |
||
109 | * |
||
110 | * For each application and image-name (without extension) one full path is returned. |
||
111 | * The path takes template-set and image-type-priority (now fixed to: png, jpg, gif, ico) into account. |
||
112 | * |
||
113 | * VFS image directory is treated like an application named 'vfs'. |
||
114 | * |
||
115 | * @param string $template_set =null 'default', 'idots', 'jerryr', default is template-set from user prefs |
||
116 | * |
||
117 | * @return array of application => image-name => full path |
||
118 | */ |
||
119 | public static function map($template_set=null) |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * Delete image map cache for ALL template sets |
||
209 | */ |
||
210 | public static function invalidate() |
||
222 | } |
||
223 | } |
||
224 | } |
||
225 |