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 | * @return bool True if changed |
||
555 | */ |
||
556 | public function updateFilesystem() { |
||
557 | if(!$this->config()->update_filesystem) { |
||
558 | return false; |
||
559 | } |
||
560 | |||
561 | // Check the file exists |
||
562 | if(!$this->File->exists()) { |
||
563 | return false; |
||
564 | } |
||
565 | |||
566 | // Avoid moving files on live; Rely on this being done on stage prior to publish. |
||
567 | if(Versioned::get_stage() !== Versioned::DRAFT) { |
||
568 | return false; |
||
569 | } |
||
570 | |||
571 | // Check path updated record will point to |
||
572 | // If no changes necessary, skip |
||
573 | $pathBefore = $this->File->getFilename(); |
||
574 | $pathAfter = $this->generateFilename(); |
||
575 | if($pathAfter === $pathBefore) { |
||
576 | return false; |
||
577 | } |
||
578 | |||
579 | // Copy record to new location via stream |
||
580 | $stream = $this->File->getStream(); |
||
581 | $this->File->setFromStream($stream, $pathAfter); |
||
582 | return true; |
||
583 | } |
||
584 | |||
585 | /** |
||
586 | * Collate selected descendants of this page. |
||
587 | * $condition will be evaluated on each descendant, and if it is succeeds, that item will be added |
||
588 | * to the $collator array. |
||
589 | * |
||
590 | * @param string $condition The PHP condition to be evaluated. The page will be called $item |
||
591 | * @param array $collator An array, passed by reference, to collect all of the matching descendants. |
||
592 | * @return true|null |
||
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 | * @return $this |
||
615 | */ |
||
616 | public function setName($name) { |
||
661 | |||
662 | /** |
||
663 | * Gets the URL of this file |
||
664 | * |
||
665 | * @return string |
||
666 | */ |
||
667 | public function getAbsoluteURL() { |
||
673 | |||
674 | /** |
||
675 | * Gets the URL of this file |
||
676 | * |
||
677 | * @uses Director::baseURL() |
||
678 | * @param bool $grant Ensures that the url for any protected assets is granted for the current user. |
||
679 | * @return string |
||
680 | */ |
||
681 | public function getURL($grant = true) { |
||
686 | |||
687 | /** |
||
688 | * Get URL, but without resampling. |
||
689 | * |
||
690 | * @param bool $grant Ensures that the url for any protected assets is granted for the current user. |
||
691 | * @return string |
||
692 | */ |
||
693 | public function getSourceURL($grant = true) { |
||
698 | |||
699 | /** |
||
700 | * @todo Coupling with cms module, remove this method. |
||
701 | * |
||
702 | * @return string |
||
703 | */ |
||
704 | public function DeleteLink() { |
||
707 | |||
708 | /** |
||
709 | * Get expected value of Filename tuple value. Will be used to trigger |
||
710 | * a file move on draft stage. |
||
711 | * |
||
712 | * @return string |
||
713 | */ |
||
714 | public function generateFilename() { |
||
715 | // Check if this file is nested within a folder |
||
716 | $parent = $this->Parent(); |
||
717 | if($parent && $parent->exists()) { |
||
718 | return $this->join_paths($parent->getFilename(), $this->Name); |
||
719 | } |
||
720 | return $this->Name; |
||
721 | } |
||
722 | |||
723 | /** |
||
724 | * Ensure that parent folders are published before this one is published |
||
725 | * |
||
726 | * @todo Solve this via triggered publishing / ownership in the future |
||
727 | */ |
||
728 | public function onBeforePublish() { |
||
735 | |||
736 | /** |
||
737 | * Update the ParentID and Name for the given filename. |
||
738 | * |
||
739 | * On save, the underlying DBFile record will move the underlying file to this location. |
||
740 | * Thus it will not update the underlying Filename value until this is done. |
||
741 | * |
||
742 | * @param string $filename |
||
743 | * @return $this |
||
744 | */ |
||
745 | public function setFilename($filename) { |
||
768 | |||
769 | /** |
||
770 | * Returns the file extension |
||
771 | * |
||
772 | * @return string |
||
773 | */ |
||
774 | public function getExtension() { |
||
777 | |||
778 | /** |
||
779 | * Gets the extension of a filepath or filename, |
||
780 | * by stripping away everything before the last"dot". |
||
781 | * Caution: Only returns the last extension in"double-barrelled" |
||
782 | * extensions (e.g."gz" for"tar.gz"). |
||
783 | * |
||
784 | * Examples: |
||
785 | * -"myfile" returns"" |
||
786 | * -"myfile.txt" returns"txt" |
||
787 | * -"myfile.tar.gz" returns"gz" |
||
788 | * |
||
789 | * @param string $filename |
||
790 | * @return string |
||
791 | */ |
||
792 | public static function get_file_extension($filename) { |
||
795 | |||
796 | /** |
||
797 | * Given an extension, determine the icon that should be used |
||
798 | * |
||
799 | * @param string $extension |
||
800 | * @return string Icon filename relative to base url |
||
801 | */ |
||
802 | public static function get_icon_for_extension($extension) { |
||
817 | |||
818 | /** |
||
819 | * Return the type of file for the given extension |
||
820 | * on the current file name. |
||
821 | * |
||
822 | * @return string |
||
823 | */ |
||
824 | public function getFileType() { |
||
827 | |||
828 | /** |
||
829 | * Get descriptive type of file based on filename |
||
830 | * |
||
831 | * @param string $filename |
||
832 | * @return string Description of file |
||
833 | */ |
||
834 | public static function get_file_type($filename) { |
||
863 | |||
864 | /** |
||
865 | * Returns the size of the file type in an appropriate format. |
||
866 | * |
||
867 | * @return string|false String value, or false if doesn't exist |
||
868 | */ |
||
869 | public function getSize() { |
||
876 | |||
877 | /** |
||
878 | * Formats a file size (eg: (int)42 becomes string '42 bytes') |
||
879 | * |
||
880 | * @todo unit tests |
||
881 | * |
||
882 | * @param int $size |
||
883 | * @return string |
||
884 | */ |
||
885 | public static function format_size($size) { |
||
886 | if($size < 1024) { |
||
887 | return $size . ' bytes'; |
||
888 | } |
||
889 | if($size < 1024*10) { |
||
890 | return (round($size/1024*10)/10). ' KB'; |
||
891 | } |
||
892 | if($size < 1024*1024) { |
||
893 | return round($size/1024) . ' KB'; |
||
894 | } |
||
895 | if($size < 1024*1024*10) { |
||
896 | return (round(($size/1024)/1024*10)/10) . ' MB'; |
||
897 | } |
||
898 | if($size < 1024*1024*1024) { |
||
899 | return round(($size/1024)/1024) . ' MB'; |
||
900 | } |
||
901 | return round($size/(1024*1024*1024)*10)/10 . ' GB'; |
||
902 | } |
||
903 | |||
904 | /** |
||
905 | * Convert a php.ini value (eg: 512M) to bytes |
||
906 | * |
||
907 | * @todo unit tests |
||
908 | * |
||
909 | * @param string $iniValue |
||
910 | * @return int |
||
911 | */ |
||
912 | public static function ini2bytes($iniValue) { |
||
913 | switch(strtolower(substr(trim($iniValue), -1))) { |
||
914 | case 'g': |
||
915 | $iniValue *= 1024; |
||
916 | case 'm': |
||
917 | $iniValue *= 1024; |
||
918 | case 'k': |
||
919 | $iniValue *= 1024; |
||
920 | } |
||
921 | return $iniValue; |
||
922 | } |
||
923 | |||
924 | /** |
||
925 | * Return file size in bytes. |
||
926 | * |
||
927 | * @return int |
||
928 | */ |
||
929 | public function getAbsoluteSize(){ |
||
932 | |||
933 | public function validate() { |
||
939 | |||
940 | /** |
||
941 | * Maps a {@link File} subclass to a specific extension. |
||
942 | * By default, files with common image extensions will be created |
||
943 | * as {@link Image} instead of {@link File} when using |
||
944 | * {@link Folder::constructChild}, {@link Folder::addUploadToFolder}), |
||
945 | * and the {@link Upload} class (either directly or through {@link FileField}). |
||
946 | * For manually instanciated files please use this mapping getter. |
||
947 | * |
||
948 | * Caution: Changes to mapping doesn't apply to existing file records in the database. |
||
949 | * Also doesn't hook into {@link Object::getCustomClass()}. |
||
950 | * |
||
951 | * @param String File extension, without dot prefix. Use an asterisk ('*') |
||
952 | * to specify a generic fallback if no mapping is found for an extension. |
||
953 | * @return String Classname for a subclass of {@link File} |
||
954 | */ |
||
955 | public static function get_class_for_file_extension($ext) { |
||
959 | |||
960 | /** |
||
961 | * See {@link get_class_for_file_extension()}. |
||
962 | * |
||
963 | * @param String|array |
||
964 | * @param String |
||
965 | */ |
||
966 | public static function set_class_for_file_extension($exts, $class) { |
||
977 | |||
978 | public function getMetaData() { |
||
983 | |||
984 | public function getMimeType() { |
||
989 | |||
990 | public function getStream() { |
||
995 | |||
996 | public function getString() { |
||
997 | if($this->File->exists()) { |
||
998 | return $this->File->getString(); |
||
999 | } |
||
1000 | } |
||
1001 | |||
1002 | public function setFromLocalFile($path, $filename = null, $hash = null, $variant = null, $config = array()) { |
||
1003 | $result = $this->File->setFromLocalFile($path, $filename, $hash, $variant, $config); |
||
1004 | |||
1005 | // Update File record to name of the uploaded asset |
||
1006 | if($result) { |
||
1007 | $this->setFilename($result['Filename']); |
||
1008 | } |
||
1009 | return $result; |
||
1010 | } |
||
1011 | |||
1012 | public function setFromStream($stream, $filename, $hash = null, $variant = null, $config = array()) { |
||
1021 | |||
1022 | public function setFromString($data, $filename, $hash = null, $variant = null, $config = array()) { |
||
1031 | |||
1032 | public function getIsImage() { |
||
1035 | |||
1036 | public function getFilename() { |
||
1037 | return $this->File->Filename; |
||
1038 | } |
||
1039 | |||
1040 | public function getHash() { |
||
1043 | |||
1044 | public function getVariant() { |
||
1047 | |||
1048 | /** |
||
1049 | * Return a html5 tag of the appropriate for this file (normally img or a) |
||
1050 | * |
||
1051 | * @return string |
||
1052 | */ |
||
1053 | public function forTemplate() { |
||
1056 | |||
1057 | /** |
||
1058 | * Return a html5 tag of the appropriate for this file (normally img or a) |
||
1059 | * |
||
1060 | * @return string |
||
1061 | */ |
||
1062 | public function getTag() { |
||
1069 | |||
1070 | public function requireDefaultRecords() { |
||
1083 | |||
1084 | /** |
||
1085 | * Joins one or more segments together to build a Filename identifier. |
||
1086 | * |
||
1087 | * Note that the result will not have a leading slash, and should not be used |
||
1088 | * with local file paths. |
||
1089 | * |
||
1090 | * @param string $part,... Parts |
||
1091 | * @return string |
||
1092 | */ |
||
1093 | public static function join_paths() { |
||
1109 | |||
1110 | public function deleteFile() { |
||
1113 | |||
1114 | public function getVisibility() { |
||
1117 | |||
1118 | public function publishFile() { |
||
1121 | |||
1122 | public function protectFile() { |
||
1125 | |||
1126 | public function grantFile() { |
||
1129 | |||
1130 | public function revokeFile() { |
||
1133 | |||
1134 | public function canViewFile() { |
||
1137 | } |
||
1138 |
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: