1 <?php
2 /**
3 * @package FrameworkOnFramework
4 * @subpackage platform
5 * @copyright Copyright (C) 2010-2016 Nicholas K. Dionysopoulos / Akeeba Ltd. All rights reserved.
6 * @license GNU General Public License version 2 or later; see LICENSE.txt
7 */
8 // Protect from unauthorized access
9 defined('FOF_INCLUDED') or die;
10
11 abstract class FOFPlatformFilesystem implements FOFPlatformFilesystemInterface
12 {
13 /**
14 * The list of paths where platform class files will be looked for
15 *
16 * @var array
17 */
18 protected static $paths = array();
19
20 /**
21 * This method will crawl a starting directory and get all the valid files that will be analyzed by getInstance.
22 * Then it organizes them into an associative array.
23 *
24 * @param string $path Folder where we should start looking
25 * @param array $ignoreFolders Folder ignore list
26 * @param array $ignoreFiles File ignore list
27 *
28 * @return array Associative array, where the `fullpath` key contains the path to the file,
29 * and the `classname` key contains the name of the class
30 */
31 protected static function getFiles($path, array $ignoreFolders = array(), array $ignoreFiles = array())
32 {
33 $return = array();
34
35 $files = self::scanDirectory($path, $ignoreFolders, $ignoreFiles);
36
37 // Ok, I got the files, now I have to organize them
38 foreach($files as $file)
39 {
40 $clean = str_replace($path, '', $file);
41 $clean = trim(str_replace('\\', '/', $clean), '/');
42
43 $parts = explode('/', $clean);
44
45 // If I have less than 3 fragments, it means that the file was inside the generic folder
46 // (interface + abstract) so I have to skip it
47 if(count($parts) < 3)
48 {
49 continue;
50 }
51
52 $return[] = array(
53 'fullpath' => $file,
54 'classname' => 'FOFPlatform'.ucfirst($parts[0]).ucfirst(basename($parts[1], '.php'))
55 );
56 }
57
58 return $return;
59 }
60
61 /**
62 * Recursive function that will scan every directory unless it's in the ignore list. Files that aren't in the
63 * ignore list are returned.
64 *
65 * @param string $path Folder where we should start looking
66 * @param array $ignoreFolders Folder ignore list
67 * @param array $ignoreFiles File ignore list
68 *
69 * @return array List of all the files
70 */
71 protected static function scanDirectory($path, array $ignoreFolders = array(), array $ignoreFiles = array())
72 {
73 $return = array();
74
75 $handle = @opendir($path);
76
77 if(!$handle)
78 {
79 return $return;
80 }
81
82 while (($file = readdir($handle)) !== false)
83 {
84 if($file == '.' || $file == '..')
85 {
86 continue;
87 }
88
89 $fullpath = $path . '/' . $file;
90
91 if((is_dir($fullpath) && in_array($file, $ignoreFolders)) || (is_file($fullpath) && in_array($file, $ignoreFiles)))
92 {
93 continue;
94 }
95
96 if(is_dir($fullpath))
97 {
98 $return = array_merge(self::scanDirectory($fullpath, $ignoreFolders, $ignoreFiles), $return);
99 }
100 else
101 {
102 $return[] = $path . '/' . $file;
103 }
104 }
105
106 return $return;
107 }
108
109 /**
110 * Gets the extension of a file name
111 *
112 * @param string $file The file name
113 *
114 * @return string The file extension
115 */
116 public function getExt($file)
117 {
118 $dot = strrpos($file, '.') + 1;
119
120 return substr($file, $dot);
121 }
122
123 /**
124 * Strips the last extension off of a file name
125 *
126 * @param string $file The file name
127 *
128 * @return string The file name without the extension
129 */
130 public function stripExt($file)
131 {
132 return preg_replace('#\.[^.]*$#', '', $file);
133 }
134 }