Complex classes like File 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 File, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
68 | class File extends DataObject implements ShortcodeHandler, AssetContainer { |
||
69 | |||
70 | use ImageManipulation; |
||
71 | |||
72 | private static $default_sort = "\"Name\""; |
||
73 | |||
74 | private static $singular_name = "File"; |
||
75 | |||
76 | private static $plural_name = "Files"; |
||
77 | |||
78 | /** |
||
79 | * Permissions necessary to view files outside of the live stage (e.g. archive / draft stage). |
||
80 | * |
||
81 | * @config |
||
82 | * @var array |
||
83 | */ |
||
84 | private static $non_live_permissions = array('CMS_ACCESS_LeftAndMain', 'CMS_ACCESS_AssetAdmin', 'VIEW_DRAFT_CONTENT'); |
||
85 | |||
86 | private static $db = array( |
||
87 | "Name" =>"Varchar(255)", |
||
88 | "Title" =>"Varchar(255)", |
||
89 | "File" =>"DBFile", |
||
90 | // Only applies to files, doesn't inherit for folder |
||
91 | 'ShowInSearch' => 'Boolean(1)', |
||
92 | ); |
||
93 | |||
94 | private static $has_one = array( |
||
95 | "Parent" => "File", |
||
96 | "Owner" => "Member" |
||
97 | ); |
||
98 | |||
99 | private static $defaults = array( |
||
100 | "ShowInSearch" => 1, |
||
101 | ); |
||
102 | |||
103 | private static $extensions = array( |
||
104 | "Hierarchy", |
||
105 | "Versioned" |
||
106 | ); |
||
107 | |||
108 | private static $casting = array( |
||
109 | 'TreeTitle' => 'HTMLText' |
||
110 | ); |
||
111 | |||
112 | /** |
||
113 | * @config |
||
114 | * @var array List of allowed file extensions, enforced through {@link validate()}. |
||
115 | * |
||
116 | * Note: if you modify this, you should also change a configuration file in the assets directory. |
||
117 | * Otherwise, the files will be able to be uploaded but they won't be able to be served by the |
||
118 | * webserver. |
||
119 | * |
||
120 | * - If you are running Apache you will need to change assets/.htaccess |
||
121 | * - If you are running IIS you will need to change assets/web.config |
||
122 | * |
||
123 | * Instructions for the change you need to make are included in a comment in the config file. |
||
124 | */ |
||
125 | private static $allowed_extensions = array( |
||
126 | '', 'ace', 'arc', 'arj', 'asf', 'au', 'avi', 'bmp', 'bz2', 'cab', 'cda', 'css', 'csv', 'dmg', 'doc', |
||
127 | 'docx', 'dotx', 'dotm', 'flv', 'gif', 'gpx', 'gz', 'hqx', 'ico', 'jar', 'jpeg', 'jpg', 'js', 'kml', |
||
128 | 'm4a', 'm4v', 'mid', 'midi', 'mkv', 'mov', 'mp3', 'mp4', 'mpa', 'mpeg', 'mpg', 'ogg', 'ogv', 'pages', |
||
129 | 'pcx', 'pdf', 'png', 'pps', 'ppt', 'pptx', 'potx', 'potm', 'ra', 'ram', 'rm', 'rtf', 'sit', 'sitx', |
||
130 | 'tar', 'tgz', 'tif', 'tiff', 'txt', 'wav', 'webm', 'wma', 'wmv', 'xls', 'xlsx', 'xltx', 'xltm', 'zip', |
||
131 | 'zipx', |
||
132 | ); |
||
133 | |||
134 | /** |
||
135 | * @config |
||
136 | * @var array Category identifiers mapped to commonly used extensions. |
||
137 | */ |
||
138 | private static $app_categories = array( |
||
139 | 'archive' => array( |
||
140 | 'ace', 'arc', 'arj', 'bz', 'bz2', 'cab', 'dmg', 'gz', 'hqx', 'jar', 'rar', 'sit', 'sitx', 'tar', 'tgz', |
||
141 | 'zip', 'zipx', |
||
142 | ), |
||
143 | 'audio' => array( |
||
144 | 'aif', 'aifc', 'aiff', 'apl', 'au', 'avr', 'cda', 'm4a', 'mid', 'midi', 'mp3', 'ogg', 'ra', |
||
145 | 'ram', 'rm', 'snd', 'wav', 'wma', |
||
146 | ), |
||
147 | 'document' => array( |
||
148 | 'css', 'csv', 'doc', 'docx', 'dotm', 'dotx', 'htm', 'html', 'gpx', 'js', 'kml', 'pages', 'pdf', |
||
149 | 'potm', 'potx', 'pps', 'ppt', 'pptx', 'rtf', 'txt', 'xhtml', 'xls', 'xlsx', 'xltm', 'xltx', 'xml', |
||
150 | ), |
||
151 | 'image' => array( |
||
152 | 'alpha', 'als', 'bmp', 'cel', 'gif', 'ico', 'icon', 'jpeg', 'jpg', 'pcx', 'png', 'ps', 'tif', 'tiff', |
||
153 | ), |
||
154 | 'image/supported' => array( |
||
155 | 'gif', 'jpeg', 'jpg', 'png' |
||
156 | ), |
||
157 | 'flash' => array( |
||
158 | 'fla', 'swf' |
||
159 | ), |
||
160 | 'video' => array( |
||
161 | 'asf', 'avi', 'flv', 'ifo', 'm1v', 'm2v', 'm4v', 'mkv', 'mov', 'mp2', 'mp4', 'mpa', 'mpe', 'mpeg', |
||
162 | 'mpg', 'ogv', 'qt', 'vob', 'webm', 'wmv', |
||
163 | ), |
||
164 | ); |
||
165 | |||
166 | /** |
||
167 | * Map of file extensions to class type |
||
168 | * |
||
169 | * @config |
||
170 | * @var |
||
171 | */ |
||
172 | private static $class_for_file_extension = array( |
||
173 | '*' => 'File', |
||
174 | 'jpg' => 'Image', |
||
175 | 'jpeg' => 'Image', |
||
176 | 'png' => 'Image', |
||
177 | 'gif' => 'Image', |
||
178 | ); |
||
179 | |||
180 | /** |
||
181 | * @config |
||
182 | * @var If this is true, then restrictions set in {@link $allowed_max_file_size} and |
||
183 | * {@link $allowed_extensions} will be applied to users with admin privileges as |
||
184 | * well. |
||
185 | */ |
||
186 | private static $apply_restrictions_to_admin = true; |
||
187 | |||
188 | /** |
||
189 | * If enabled, legacy file dataobjects will be automatically imported into the APL |
||
190 | * |
||
191 | * @config |
||
192 | * @var bool |
||
193 | */ |
||
194 | private static $migrate_legacy_file = false; |
||
195 | |||
196 | /** |
||
197 | * @config |
||
198 | * @var boolean |
||
199 | */ |
||
200 | private static $update_filesystem = true; |
||
201 | |||
202 | public static function get_shortcodes() { |
||
205 | |||
206 | /** |
||
207 | * Replace"[file_link id=n]" shortcode with an anchor tag or link to the file. |
||
208 | * |
||
209 | * @param array $arguments Arguments passed to the parser |
||
210 | * @param string $content Raw shortcode |
||
211 | * @param ShortcodeParser $parser Parser |
||
212 | * @param string $shortcode Name of shortcode used to register this handler |
||
213 | * @param array $extra Extra arguments |
||
214 | * @return string Result of the handled shortcode |
||
215 | */ |
||
216 | public static function handle_shortcode($arguments, $content, $parser, $shortcode, $extra = array()) { |
||
262 | |||
263 | /** |
||
264 | * A file only exists if the file_exists() and is in the DB as a record |
||
265 | * |
||
266 | * Use $file->isInDB() to only check for a DB record |
||
267 | * Use $file->File->exists() to only check if the asset exists |
||
268 | * |
||
269 | * @return bool |
||
270 | */ |
||
271 | public function exists() { |
||
274 | |||
275 | /** |
||
276 | * Find a File object by the given filename. |
||
277 | * |
||
278 | * @param string $filename Filename to search for, including any custom parent directories. |
||
279 | * @return File |
||
280 | */ |
||
281 | public static function find($filename) { |
||
297 | |||
298 | /** |
||
299 | * Just an alias function to keep a consistent API with SiteTree |
||
300 | * |
||
301 | * @return string The link to the file |
||
302 | */ |
||
303 | public function Link() { |
||
306 | |||
307 | /** |
||
308 | * @deprecated 4.0 |
||
309 | */ |
||
310 | public function RelativeLink() { |
||
314 | |||
315 | /** |
||
316 | * Just an alias function to keep a consistent API with SiteTree |
||
317 | * |
||
318 | * @return string The absolute link to the file |
||
319 | */ |
||
320 | public function AbsoluteLink() { |
||
323 | |||
324 | /** |
||
325 | * @return string |
||
326 | */ |
||
327 | public function getTreeTitle() { |
||
330 | |||
331 | /** |
||
332 | * @param Member $member |
||
333 | * @return bool |
||
334 | */ |
||
335 | public function canView($member = null) { |
||
347 | |||
348 | /** |
||
349 | * Check if this file can be modified |
||
350 | * |
||
351 | * @param Member $member |
||
352 | * @return boolean |
||
353 | */ |
||
354 | public function canEdit($member = null) { |
||
366 | |||
367 | /** |
||
368 | * Check if a file can be created |
||
369 | * |
||
370 | * @param Member $member |
||
371 | * @param array $context |
||
372 | * @return boolean |
||
373 | */ |
||
374 | public function canCreate($member = null, $context = array()) { |
||
386 | |||
387 | /** |
||
388 | * Check if this file can be deleted |
||
389 | * |
||
390 | * @param Member $member |
||
391 | * @return boolean |
||
392 | */ |
||
393 | public function canDelete($member = null) { |
||
405 | |||
406 | /** |
||
407 | * Returns the fields to power the edit screen of files in the CMS. |
||
408 | * You can modify this FieldList by subclassing folder, or by creating a {@link DataExtension} |
||
409 | * and implemeting updateCMSFields(FieldList $fields) on that extension. |
||
410 | * |
||
411 | * @return FieldList |
||
412 | */ |
||
413 | public function getCMSFields() { |
||
456 | |||
457 | /** |
||
458 | * Returns a category based on the file extension. |
||
459 | * This can be useful when grouping files by type, |
||
460 | * showing icons on filelinks, etc. |
||
461 | * Possible group values are:"audio","mov","zip","image". |
||
462 | * |
||
463 | * @param string $ext Extension to check |
||
464 | * @return string |
||
465 | */ |
||
466 | public static function get_app_category($ext) { |
||
473 | |||
474 | /** |
||
475 | * For a category or list of categories, get the list of file extensions |
||
476 | * |
||
477 | * @param array|string $categories List of categories, or single category |
||
478 | * @return array |
||
479 | */ |
||
480 | public static function get_category_extensions($categories) { |
||
508 | |||
509 | /** |
||
510 | * Returns a category based on the file extension. |
||
511 | * |
||
512 | * @return string |
||
513 | */ |
||
514 | public function appCategory() { |
||
517 | |||
518 | |||
519 | /** |
||
520 | * Should be called after the file was uploaded |
||
521 | */ |
||
522 | public function onAfterUpload() { |
||
525 | |||
526 | /** |
||
527 | * Make sure the file has a name |
||
528 | */ |
||
529 | protected function onBeforeWrite() { |
||
545 | |||
546 | /** |
||
547 | * This will check if the parent record and/or name do not match the name on the underlying |
||
548 | * DBFile record, and if so, copy this file to the new location, and update the record to |
||
549 | * point to this new file. |
||
550 | * |
||
551 | * This method will update the File {@see DBFile} field value on success, so it must be called |
||
552 | * before writing to the database |
||
553 | * |
||
554 | * @param bool True if changed |
||
555 | */ |
||
556 | public function updateFilesystem() { |
||
586 | |||
587 | /** |
||
588 | * Collate selected descendants of this page. |
||
589 | * $condition will be evaluated on each descendant, and if it is succeeds, that item will be added |
||
590 | * to the $collator array. |
||
591 | * @param condition The PHP condition to be evaluated. The page will be called $item |
||
592 | * @param collator An array, passed by reference, to collect all of the matching descendants. |
||
593 | */ |
||
594 | public function collateDescendants($condition, &$collator) { |
||
603 | |||
604 | /** |
||
605 | * Setter function for Name. Automatically sets a default title, |
||
606 | * and removes characters that might be invalid on the filesystem. |
||
607 | * Also adds a suffix to the name if the filename already exists |
||
608 | * on the filesystem, and is associated to a different {@link File} database record |
||
609 | * in the same folder. This means"myfile.jpg" might become"myfile-1.jpg". |
||
610 | * |
||
611 | * Does not change the filesystem itself, please use {@link write()} for this. |
||
612 | * |
||
613 | * @param String $name |
||
614 | */ |
||
615 | public function setName($name) { |
||
660 | |||
661 | /** |
||
662 | * Gets the URL of this file |
||
663 | * |
||
664 | * @return string |
||
665 | */ |
||
666 | public function getAbsoluteURL() { |
||
672 | |||
673 | /** |
||
674 | * Gets the URL of this file |
||
675 | * |
||
676 | * @uses Director::baseURL() |
||
677 | * @param bool $grant Ensures that the url for any protected assets is granted for the current user. |
||
678 | * @return string |
||
679 | */ |
||
680 | public function getURL($grant = true) { |
||
685 | |||
686 | /** |
||
687 | * Get URL, but without resampling. |
||
688 | * |
||
689 | * @param bool $grant Ensures that the url for any protected assets is granted for the current user. |
||
690 | * @return string |
||
691 | */ |
||
692 | public function getSourceURL($grant = true) { |
||
697 | |||
698 | /** |
||
699 | * @todo Coupling with cms module, remove this method. |
||
700 | * |
||
701 | * @return string |
||
702 | */ |
||
703 | public function DeleteLink() { |
||
706 | |||
707 | public function getFilename() { |
||
716 | |||
717 | /** |
||
718 | * Ensure that parent folders are published before this one is published |
||
719 | * |
||
720 | * @todo Solve this via triggered publishing / ownership in the future |
||
721 | */ |
||
722 | public function onBeforePublish() { |
||
729 | |||
730 | /** |
||
731 | * Update the ParentID and Name for the given filename. |
||
732 | * |
||
733 | * On save, the underlying DBFile record will move the underlying file to this location. |
||
734 | * Thus it will not update the underlying Filename value until this is done. |
||
735 | * |
||
736 | * @param string $filename |
||
737 | * @return $this |
||
738 | */ |
||
739 | public function setFilename($filename) { |
||
762 | |||
763 | /** |
||
764 | * Returns the file extension |
||
765 | * |
||
766 | * @return string |
||
767 | */ |
||
768 | public function getExtension() { |
||
771 | |||
772 | /** |
||
773 | * Gets the extension of a filepath or filename, |
||
774 | * by stripping away everything before the last"dot". |
||
775 | * Caution: Only returns the last extension in"double-barrelled" |
||
776 | * extensions (e.g."gz" for"tar.gz"). |
||
777 | * |
||
778 | * Examples: |
||
779 | * -"myfile" returns"" |
||
780 | * -"myfile.txt" returns"txt" |
||
781 | * -"myfile.tar.gz" returns"gz" |
||
782 | * |
||
783 | * @param string $filename |
||
784 | * @return string |
||
785 | */ |
||
786 | public static function get_file_extension($filename) { |
||
789 | |||
790 | /** |
||
791 | * Given an extension, determine the icon that should be used |
||
792 | * |
||
793 | * @param string $extension |
||
794 | * @return string Icon filename relative to base url |
||
795 | */ |
||
796 | public static function get_icon_for_extension($extension) { |
||
811 | |||
812 | /** |
||
813 | * Return the type of file for the given extension |
||
814 | * on the current file name. |
||
815 | * |
||
816 | * @return string |
||
817 | */ |
||
818 | public function getFileType() { |
||
821 | |||
822 | /** |
||
823 | * Get descriptive type of file based on filename |
||
824 | * |
||
825 | * @param string $filename |
||
826 | * @return string Description of file |
||
827 | */ |
||
828 | public static function get_file_type($filename) { |
||
857 | |||
858 | /** |
||
859 | * Returns the size of the file type in an appropriate format. |
||
860 | * |
||
861 | * @return string|false String value, or false if doesn't exist |
||
862 | */ |
||
863 | public function getSize() { |
||
870 | |||
871 | /** |
||
872 | * Formats a file size (eg: (int)42 becomes string '42 bytes') |
||
873 | * |
||
874 | * @todo unit tests |
||
875 | * |
||
876 | * @param int $size |
||
877 | * @return string |
||
878 | */ |
||
879 | public static function format_size($size) { |
||
897 | |||
898 | /** |
||
899 | * Convert a php.ini value (eg: 512M) to bytes |
||
900 | * |
||
901 | * @todo unit tests |
||
902 | * |
||
903 | * @param string $iniValue |
||
904 | * @return int |
||
905 | */ |
||
906 | public static function ini2bytes($iniValue) { |
||
917 | |||
918 | /** |
||
919 | * Return file size in bytes. |
||
920 | * |
||
921 | * @return int |
||
922 | */ |
||
923 | public function getAbsoluteSize(){ |
||
926 | |||
927 | public function validate() { |
||
933 | |||
934 | /** |
||
935 | * Maps a {@link File} subclass to a specific extension. |
||
936 | * By default, files with common image extensions will be created |
||
937 | * as {@link Image} instead of {@link File} when using |
||
938 | * {@link Folder::constructChild}, {@link Folder::addUploadToFolder}), |
||
939 | * and the {@link Upload} class (either directly or through {@link FileField}). |
||
940 | * For manually instanciated files please use this mapping getter. |
||
941 | * |
||
942 | * Caution: Changes to mapping doesn't apply to existing file records in the database. |
||
943 | * Also doesn't hook into {@link Object::getCustomClass()}. |
||
944 | * |
||
945 | * @param String File extension, without dot prefix. Use an asterisk ('*') |
||
946 | * to specify a generic fallback if no mapping is found for an extension. |
||
947 | * @return String Classname for a subclass of {@link File} |
||
948 | */ |
||
949 | public static function get_class_for_file_extension($ext) { |
||
953 | |||
954 | /** |
||
955 | * See {@link get_class_for_file_extension()}. |
||
956 | * |
||
957 | * @param String|array |
||
958 | * @param String |
||
959 | */ |
||
960 | public static function set_class_for_file_extension($exts, $class) { |
||
971 | |||
972 | public function getMetaData() { |
||
977 | |||
978 | public function getMimeType() { |
||
983 | |||
984 | public function getStream() { |
||
989 | |||
990 | public function getString() { |
||
995 | |||
996 | public function setFromLocalFile($path, $filename = null, $hash = null, $variant = null, $config = array()) { |
||
1005 | |||
1006 | public function setFromStream($stream, $filename, $hash = null, $variant = null, $config = array()) { |
||
1015 | |||
1016 | public function setFromString($data, $filename, $hash = null, $variant = null, $config = array()) { |
||
1025 | |||
1026 | public function getIsImage() { |
||
1029 | |||
1030 | public function getHash() { |
||
1033 | |||
1034 | public function getVariant() { |
||
1037 | |||
1038 | /** |
||
1039 | * Return a html5 tag of the appropriate for this file (normally img or a) |
||
1040 | * |
||
1041 | * @return string |
||
1042 | */ |
||
1043 | public function forTemplate() { |
||
1046 | |||
1047 | /** |
||
1048 | * Return a html5 tag of the appropriate for this file (normally img or a) |
||
1049 | * |
||
1050 | * @return string |
||
1051 | */ |
||
1052 | public function getTag() { |
||
1059 | |||
1060 | public function requireDefaultRecords() { |
||
1073 | |||
1074 | /** |
||
1075 | * Joins one or more segments together to build a Filename identifier. |
||
1076 | * |
||
1077 | * Note that the result will not have a leading slash, and should not be used |
||
1078 | * with local file paths. |
||
1079 | * |
||
1080 | * @param string $part,... Parts |
||
1081 | * @return string |
||
1082 | */ |
||
1083 | public static function join_paths() { |
||
1099 | |||
1100 | public function deleteFile() { |
||
1103 | |||
1104 | public function getVisibility() { |
||
1107 | |||
1108 | public function publishFile() { |
||
1111 | |||
1112 | public function protectFile() { |
||
1115 | |||
1116 | public function grantFile() { |
||
1119 | |||
1120 | public function revokeFile() { |
||
1123 | |||
1124 | public function canViewFile() { |
||
1127 | } |
||
1128 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: