Skip to content

Commit

Permalink
Optimize codes
Browse files Browse the repository at this point in the history
  • Loading branch information
steevenz committed May 29, 2018
1 parent 15c3a77 commit 778a94f
Show file tree
Hide file tree
Showing 14 changed files with 655 additions and 643 deletions.
72 changes: 36 additions & 36 deletions src/Config/Mimes.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@
'dms' => 'application/octet-stream',
'lha' => 'application/octet-stream',
'lzh' => 'application/octet-stream',
'exe' => [ 'application/octet-stream', 'application/x-msdownload' ],
'exe' => ['application/octet-stream', 'application/x-msdownload'],
'class' => 'application/octet-stream',
'psd' => [ 'application/x-photoshop', 'image/vnd.adobe.photoshop' ],
'psd' => ['application/x-photoshop', 'image/vnd.adobe.photoshop'],
'so' => 'application/octet-stream',
'sea' => 'application/octet-stream',
'dll' => 'application/octet-stream',
Expand All @@ -62,7 +62,7 @@
'application/x-download',
'binary/octet-stream',
],
'ai' => [ 'application/pdf', 'application/postscript' ],
'ai' => ['application/pdf', 'application/postscript'],
'eps' => 'application/postscript',
'ps' => 'application/postscript',
'smi' => 'application/smil',
Expand Down Expand Up @@ -114,11 +114,11 @@
'php3' => 'application/x-httpd-php',
'phtml' => 'application/x-httpd-php',
'phps' => 'application/x-httpd-php-source',
'js' => [ 'application/x-javascript', 'text/plain' ],
'js' => ['application/x-javascript', 'text/plain'],
'swf' => 'application/x-shockwave-flash',
'sit' => 'application/x-stuffit',
'tar' => 'application/x-tar',
'tgz' => [ 'application/x-tar', 'application/x-gzip-compressed' ],
'tgz' => ['application/x-tar', 'application/x-gzip-compressed'],
'z' => 'application/x-compress',
'xhtml' => 'application/xhtml+xml',
'xht' => 'application/xhtml+xml',
Expand All @@ -129,21 +129,21 @@
'application/s-compressed',
'multipart/x-zip',
],
'rar' => [ 'application/x-rar', 'application/rar', 'application/x-rar-compressed' ],
'rar' => ['application/x-rar', 'application/rar', 'application/x-rar-compressed'],
'mid' => 'audio/midi',
'midi' => 'audio/midi',
'mpga' => 'audio/mpeg',
'mp2' => 'audio/mpeg',
'mp3' => [ 'audio/mpeg', 'audio/mpg', 'audio/mpeg3', 'audio/mp3' ],
'aif' => [ 'audio/x-aiff', 'audio/aiff' ],
'aiff' => [ 'audio/x-aiff', 'audio/aiff' ],
'mp3' => ['audio/mpeg', 'audio/mpg', 'audio/mpeg3', 'audio/mp3'],
'aif' => ['audio/x-aiff', 'audio/aiff'],
'aiff' => ['audio/x-aiff', 'audio/aiff'],
'aifc' => 'audio/x-aiff',
'ram' => 'audio/x-pn-realaudio',
'rm' => 'audio/x-pn-realaudio',
'rpm' => 'audio/x-pn-realaudio-plugin',
'ra' => 'audio/x-realaudio',
'rv' => 'video/vnd.rn-realvideo',
'wav' => [ 'audio/x-wav', 'audio/wave', 'audio/wav' ],
'wav' => ['audio/x-wav', 'audio/wave', 'audio/wav'],
'bmp' => [
'image/bmp',
'image/x-bmp',
Expand All @@ -161,35 +161,35 @@
'jpeg' => 'image/jpeg',
'jpg' => 'image/jpeg',
'jpe' => 'image/jpeg',
'png' => [ 'image/png', 'image/x-png' ],
'png' => ['image/png', 'image/x-png'],
'tiff' => 'image/tiff',
'tif' => 'image/tiff',
'css' => [ 'text/css', 'text/plain' ],
'html' => [ 'text/html', 'text/plain' ],
'htm' => [ 'text/html', 'text/plain' ],
'shtml' => [ 'text/html', 'text/plain' ],
'css' => ['text/css', 'text/plain'],
'html' => ['text/html', 'text/plain'],
'htm' => ['text/html', 'text/plain'],
'shtml' => ['text/html', 'text/plain'],
'txt' => 'text/plain',
'text' => 'text/plain',
'log' => [ 'text/plain', 'text/x-log' ],
'log' => ['text/plain', 'text/x-log'],
'rtx' => 'text/richtext',
'rtf' => 'text/rtf',
'xml' => [ 'application/xml', 'text/xml', 'text/plain' ],
'xsl' => [ 'application/xml', 'text/xsl', 'text/xml' ],
'xml' => ['application/xml', 'text/xml', 'text/plain'],
'xsl' => ['application/xml', 'text/xsl', 'text/xml'],
'mpeg' => 'video/mpeg',
'mpg' => 'video/mpeg',
'mpe' => 'video/mpeg',
'qt' => 'video/quicktime',
'mov' => 'video/quicktime',
'avi' => [ 'video/x-msvideo', 'video/msvideo', 'video/avi', 'application/x-troff-msvideo' ],
'avi' => ['video/x-msvideo', 'video/msvideo', 'video/avi', 'application/x-troff-msvideo'],
'movie' => 'video/x-sgi-movie',
'doc' => [ 'application/msword', 'application/vnd.ms-office' ],
'doc' => ['application/msword', 'application/vnd.ms-office'],
'docx' => [
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'application/zip',
'application/msword',
'application/x-zip',
],
'dot' => [ 'application/msword', 'application/vnd.ms-office' ],
'dot' => ['application/msword', 'application/vnd.ms-office'],
'dotx' => [
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'application/zip',
Expand All @@ -202,28 +202,28 @@
'application/msword',
'application/x-zip',
],
'word' => [ 'application/msword', 'application/octet-stream' ],
'word' => ['application/msword', 'application/octet-stream'],
'xl' => 'application/excel',
'eml' => 'message/rfc822',
'json' => [ 'application/json', 'text/json' ],
'pem' => [ 'application/x-x509-user-cert', 'application/x-pem-file', 'application/octet-stream' ],
'p10' => [ 'application/x-pkcs10', 'application/pkcs10' ],
'json' => ['application/json', 'text/json'],
'pem' => ['application/x-x509-user-cert', 'application/x-pem-file', 'application/octet-stream'],
'p10' => ['application/x-pkcs10', 'application/pkcs10'],
'p12' => 'application/x-pkcs12',
'p7a' => 'application/x-pkcs7-signature',
'p7c' => [ 'application/pkcs7-mime', 'application/x-pkcs7-mime' ],
'p7m' => [ 'application/pkcs7-mime', 'application/x-pkcs7-mime' ],
'p7c' => ['application/pkcs7-mime', 'application/x-pkcs7-mime'],
'p7m' => ['application/pkcs7-mime', 'application/x-pkcs7-mime'],
'p7r' => 'application/x-pkcs7-certreqresp',
'p7s' => 'application/pkcs7-signature',
'crt' => [ 'application/x-x509-ca-cert', 'application/x-x509-user-cert', 'application/pkix-cert' ],
'crl' => [ 'application/pkix-crl', 'application/pkcs-crl' ],
'crt' => ['application/x-x509-ca-cert', 'application/x-x509-user-cert', 'application/pkix-cert'],
'crl' => ['application/pkix-crl', 'application/pkcs-crl'],
'der' => 'application/x-x509-ca-cert',
'kdb' => 'application/octet-stream',
'pgp' => 'application/pgp',
'gpg' => 'application/gpg-keys',
'sst' => 'application/octet-stream',
'csr' => 'application/octet-stream',
'rsa' => 'application/x-pkcs7',
'cer' => [ 'application/pkix-cert', 'application/x-x509-ca-cert' ],
'cer' => ['application/pkix-cert', 'application/x-x509-ca-cert'],
'3g2' => 'video/3gpp2',
'3gp' => 'video/3gp',
'mp4' => 'video/mp4',
Expand All @@ -235,13 +235,13 @@
'm3u' => 'text/plain',
'xspf' => 'application/xspf+xml',
'vlc' => 'application/videolan',
'wmv' => [ 'video/x-ms-wmv', 'video/x-ms-asf' ],
'wmv' => ['video/x-ms-wmv', 'video/x-ms-asf'],
'au' => 'audio/x-au',
'ac3' => 'audio/ac3',
'flac' => 'audio/x-flac',
'ogg' => 'audio/ogg',
'kmz' => [ 'application/vnd.google-earth.kmz', 'application/zip', 'application/x-zip' ],
'kml' => [ 'application/vnd.google-earth.kml+xml', 'application/xml', 'text/xml' ],
'kmz' => ['application/vnd.google-earth.kmz', 'application/zip', 'application/x-zip'],
'kml' => ['application/vnd.google-earth.kml+xml', 'application/xml', 'text/xml'],
'ics' => 'text/calendar',
'ical' => 'text/calendar',
'zsh' => 'text/x-scriptzsh',
Expand All @@ -260,16 +260,16 @@
'image/x-cdr',
'zz-application/zz-winassoc-cdr',
],
'wma' => [ 'audio/x-ms-wma', 'video/x-ms-asf' ],
'wma' => ['audio/x-ms-wma', 'video/x-ms-asf'],
'jar' => [
'application/java-archive',
'application/x-java-application',
'application/x-jar',
'application/x-compressed',
],
'svg' => [ 'image/svg+xml', 'application/xml', 'text/xml' ],
'svg' => ['image/svg+xml', 'application/xml', 'text/xml'],
'vcf' => 'text/x-vcard',
'ttf' => [ 'application/x-font-ttf', 'application/x-font-truetype' ],
'ttf' => ['application/x-font-ttf', 'application/x-font-truetype'],
'otf' => 'application/x-font-opentype',
'woff' => 'application/font-woff',
'woff2' => 'application/font-woff2',
Expand Down
108 changes: 55 additions & 53 deletions src/Directory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* @author Steeve Andrian Salim
* @copyright Copyright (c) Steeve Andrian Salim
*/

// ------------------------------------------------------------------------

namespace O2System\Filesystem;
Expand All @@ -34,30 +35,30 @@ class Directory extends SplDirectoryInfo
*
* @return bool|\O2System\Spl\Info\SplDirectoryInfo
*/
public function make( $dir = null, $mode = 0777, $recursive = true )
public function make($dir = null, $mode = 0777, $recursive = true)
{
$dir = is_null( $dir ) ? $this->getPathName() : $dir;
$dir = is_null($dir) ? $this->getPathName() : $dir;

if ( is_dir( $dir ) ) {
return new SplDirectoryInfo( $dir );
} elseif ( null !== ( $pathName = $this->getPathName() ) ) {
if ( mkdir(
if (is_dir($dir)) {
return new SplDirectoryInfo($dir);
} elseif (null !== ($pathName = $this->getPathName())) {
if (mkdir(
$makeDirectory = $pathName . DIRECTORY_SEPARATOR . str_replace(
[ '\\', '/' ],
['\\', '/'],
DIRECTORY_SEPARATOR,
$dir
),
$mode,
$recursive
) ) {
return new SplDirectoryInfo( $makeDirectory );
)) {
return new SplDirectoryInfo($makeDirectory);
}
} elseif ( mkdir(
$makeDirectory = str_replace( [ '\\', '/' ], DIRECTORY_SEPARATOR, $dir ),
} elseif (mkdir(
$makeDirectory = str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $dir),
$mode,
$recursive
) ) {
return new SplDirectoryInfo( $makeDirectory );
)) {
return new SplDirectoryInfo($makeDirectory);
}

return false;
Expand All @@ -74,9 +75,9 @@ public function make( $dir = null, $mode = 0777, $recursive = true )
*
* @return bool Returns TRUE on success or FALSE on failure.
*/
public function delete( $fileOnly = false )
public function delete($fileOnly = false)
{
return $this->recursiveDelete( $this->getRealPath(), $fileOnly );
return $this->recursiveDelete($this->getRealPath(), $fileOnly);
}

// ------------------------------------------------------------------------
Expand All @@ -89,23 +90,23 @@ public function delete( $fileOnly = false )
*
* @return bool Returns TRUE on success or FALSE on failure.
*/
private function recursiveDelete( $dir, $fileOnly = false )
private function recursiveDelete($dir, $fileOnly = false)
{
$dir = realpath( $dir );

if ( is_dir( $dir ) ) {
$iterator = new \RecursiveDirectoryIterator( $dir, \RecursiveDirectoryIterator::SKIP_DOTS );
$files = new \RecursiveIteratorIterator( $iterator, \RecursiveIteratorIterator::CHILD_FIRST );
foreach ( $files as $file ) {
if ( $file->isDir() ) {
$this->recursiveDelete( $file->getRealPath(), $fileOnly );
} elseif ( $file->isFile() ) {
unlink( $file->getRealPath() );
$dir = realpath($dir);

if (is_dir($dir)) {
$iterator = new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS);
$files = new \RecursiveIteratorIterator($iterator, \RecursiveIteratorIterator::CHILD_FIRST);
foreach ($files as $file) {
if ($file->isDir()) {
$this->recursiveDelete($file->getRealPath(), $fileOnly);
} elseif ($file->isFile()) {
unlink($file->getRealPath());
}
}

if ( $fileOnly === false ) {
rmdir( $dir );
if ($fileOnly === false) {
rmdir($dir);
}

return true;
Expand All @@ -128,12 +129,12 @@ private function recursiveDelete( $dir, $fileOnly = false )
*
* @return bool Returns TRUE on success or FALSE on failure.
*/
public function setGroup( $group )
public function setGroup($group)
{
$params[] = $this->getRealPath();
$params[] = $group;

return call_user_func_array( 'chgrp', $params );
return call_user_func_array('chgrp', $params);
}

// ------------------------------------------------------------------------
Expand All @@ -153,12 +154,12 @@ public function setGroup( $group )
*
* @return bool Returns TRUE on success or FALSE on failure.
*/
public function setMode( $mode )
public function setMode($mode)
{
$params[] = $this->getRealPath();
$params[] = $mode;

return call_user_func_array( 'chmod', $params );
return call_user_func_array('chmod', $params);
}

// ------------------------------------------------------------------------
Expand All @@ -173,34 +174,34 @@ public function setMode( $mode )
*
* @return bool Returns TRUE on success or FALSE on failure.
*/
public function setOwner( $user )
public function setOwner($user)
{
$params[] = $this->getRealPath();
$params[] = $user;

return call_user_func_array( 'chown', $params );
return call_user_func_array('chown', $params);
}

// ------------------------------------------------------------------------

public function findFilesByExtension( $extension )
public function findFilesByExtension($extension)
{
$extension = trim( $extension, '.' );
$extension = trim($extension, '.');

$directoryIterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator( $this->getRealPath() ),
new \RecursiveDirectoryIterator($this->getRealPath()),
\RecursiveIteratorIterator::SELF_FIRST
);

$result = [];

foreach ( $directoryIterator as $directoryFile ) {
if ( $directoryFile->isFile() ) {
if ( $extension === '*' ) {
array_push( $result, $directoryFile->getFilename() );
} elseif ( preg_match( '/\.' . $extension . '$/ui', $directoryFile->getFilename() ) ) {
if ( ! in_array( $directoryFile->getRealPath(), $result ) ) {
array_push( $result, $directoryFile->getRealPath() );
foreach ($directoryIterator as $directoryFile) {
if ($directoryFile->isFile()) {
if ($extension === '*') {
array_push($result, $directoryFile->getFilename());
} elseif (preg_match('/\.' . $extension . '$/ui', $directoryFile->getFilename())) {
if ( ! in_array($directoryFile->getRealPath(), $result)) {
array_push($result, $directoryFile->getRealPath());
}
}
}
Expand All @@ -215,26 +216,27 @@ public function findFilesByExtension( $extension )
* Directory::findFilesByFilename
*
* Find Files By Filename
*
*
* @param string $filename
*
* @return array
*/
public function findFilesByFilename( $filename )
public function findFilesByFilename($filename)
{
$directoryIterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator( $this->getRealPath() ),
new \RecursiveDirectoryIterator($this->getRealPath()),
\RecursiveIteratorIterator::SELF_FIRST
);

$result = [];

foreach ( $directoryIterator as $directoryFile ) {
if ( $directoryFile->isFile() ) {
if ( preg_match( '/\\' . $filename . '.*/ui', $directoryFile->getFilename() ) OR
preg_match( '/\\' . ucfirst( $filename ) . '.*/ui', $directoryFile->getFilename() )
foreach ($directoryIterator as $directoryFile) {
if ($directoryFile->isFile()) {
if (preg_match('/\\' . $filename . '.*/ui', $directoryFile->getFilename()) OR
preg_match('/\\' . ucfirst($filename) . '.*/ui', $directoryFile->getFilename())
) {
if ( ! in_array( $directoryFile->getRealPath(), $result ) ) {
array_push( $result, $directoryFile->getRealPath() );
if ( ! in_array($directoryFile->getRealPath(), $result)) {
array_push($result, $directoryFile->getRealPath());
}
}
}
Expand Down
Loading

0 comments on commit 778a94f

Please sign in to comment.