|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Classes; |
|
4
|
|
|
use Classes\Db\Iterators\DbTables; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* @author Pedro Alarcao <[email protected]> |
|
8
|
|
|
* @link https://github.com/pedro151/orm-generator |
|
9
|
|
|
*/ |
|
10
|
|
|
class CleanTrash |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @type CleanTrash |
|
14
|
|
|
*/ |
|
15
|
|
|
private static $instance; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @type int |
|
19
|
|
|
*/ |
|
20
|
|
|
private $countFileDeleted=0; |
|
21
|
|
|
|
|
22
|
|
|
final private function __construct (){ } |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @return \Classes\CleanTrash |
|
26
|
|
|
*/ |
|
27
|
|
|
public static function getInstance () |
|
28
|
|
|
{ |
|
29
|
|
|
if ( self::$instance === null ) |
|
30
|
|
|
{ |
|
31
|
|
|
self::$instance = new self(); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
return self::$instance; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
private function scanDir ( $directory ) |
|
38
|
|
|
{ |
|
39
|
|
|
if(!is_dir($directory)){ |
|
40
|
|
|
return array(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
$diretories = preg_grep ( '*\.ph*' , scandir ( $directory ) ); |
|
44
|
|
|
if(!$diretories){ |
|
|
|
|
|
|
45
|
|
|
return array(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
return array_diff ( $diretories, array ( |
|
49
|
|
|
'..' , '.' |
|
50
|
|
|
) ); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param string $path |
|
55
|
|
|
* @param \Classes\AdaptersDriver\AbsractAdapter $driver |
|
56
|
|
|
* @param int $schema |
|
57
|
|
|
* |
|
58
|
|
|
* @return array |
|
59
|
|
|
*/ |
|
60
|
|
|
private function diffFiles ( $path , $driver , $schema = 0 ) |
|
61
|
|
|
{ |
|
62
|
|
|
$tables = $driver->getTables ( $schema ); |
|
63
|
|
|
if(!$tables instanceof DbTables){ |
|
64
|
|
|
return array(); |
|
65
|
|
|
} |
|
66
|
|
|
$tablesName = $tables->toArrayFileName(); |
|
67
|
|
|
return array_diff ( $this->scanDir ( $path ) , $tablesName ); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param string $path |
|
72
|
|
|
* @param \Classes\AdaptersDriver\AbsractAdapter $driver |
|
73
|
|
|
* @param int $schema |
|
74
|
|
|
* |
|
75
|
|
|
* @return int |
|
76
|
|
|
*/ |
|
77
|
|
|
public function run ( $path , $driver , $schema = 0 ) |
|
78
|
|
|
{ |
|
79
|
|
|
$count = 0; |
|
80
|
|
|
foreach ( $this->diffFiles ( $path , $driver , $schema ) as $fileDel ) |
|
81
|
|
|
{ |
|
82
|
|
|
if ( unlink ( $path . DIRECTORY_SEPARATOR . $fileDel ) ) |
|
83
|
|
|
{ |
|
84
|
|
|
++ $count; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$this->countFileDeleted += $count; |
|
89
|
|
|
|
|
90
|
|
|
return $count; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @return int |
|
95
|
|
|
*/ |
|
96
|
|
|
public function getNumFilesDeleted () |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->countFileDeleted; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
} |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.