Skip to content

Commit e8a1223

Browse files
committed
Fixing files & handlers classes
1 parent adb5c9e commit e8a1223

File tree

11 files changed

+795
-752
lines changed

11 files changed

+795
-752
lines changed

src/Abstracts/AbstractFile.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
// ------------------------------------------------------------------------
1616

17+
use O2System\Filesystem\File;
1718
use O2System\Psr\Patterns\AbstractDataStoragePattern;
1819

1920
/**
@@ -23,13 +24,7 @@
2324
*/
2425
abstract class AbstractFile extends AbstractDataStoragePattern
2526
{
26-
/**
27-
* AbstractFile::$filePath
28-
*
29-
* Path to the file.
30-
*
31-
* @var string
32-
*/
27+
protected $fileExtension;
3328
protected $filePath;
3429

3530
// ------------------------------------------------------------------------
@@ -59,6 +54,10 @@ final public function createFile( $filePath )
5954
{
6055
$this->filePath = $filePath;
6156

57+
if( pathinfo( $this->filePath, PATHINFO_EXTENSION) === '') {
58+
$this->filePath .= $this->fileExtension;
59+
}
60+
6261
return $this;
6362
}
6463

src/Config/Mimes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
| MIME TYPES
1616
| -------------------------------------------------------------------
1717
| This file contains an array of mime types. It is used by the
18-
| Upload class to help identify allowed file types.
18+
| Uploader class to help identify allowed file types.
1919
|
2020
*/
2121
return [

src/File.php

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@
2323
*/
2424
class File extends SplFileInfo
2525
{
26+
private $filePath;
27+
28+
public function __construct( $filePath = null )
29+
{
30+
if ( isset( $filePath ) ) {
31+
$this->filePath = $filePath;
32+
parent::__construct( $filePath );
33+
}
34+
}
35+
2636
/**
2737
* File::setGroup
2838
*
@@ -297,6 +307,28 @@ public function read( $useIncludePath = false, $context = null )
297307

298308
// ------------------------------------------------------------------------
299309

310+
public function create( $filePath = null, $mode = 'wb' )
311+
{
312+
$filePath = isset( $filePath ) ? $filePath : $this->filePath;
313+
$dir = dirname( $filePath );
314+
315+
if ( ! is_writable( $dir ) ) {
316+
if ( ! file_exists( $dir ) ) {
317+
mkdir( $dir, 0777, true );
318+
}
319+
}
320+
321+
if ( ! $fp = @fopen( $filePath, $mode ) ) {
322+
return false;
323+
}
324+
325+
parent::__construct( $filePath );
326+
327+
return $fp;
328+
}
329+
330+
// ------------------------------------------------------------------------
331+
300332
/**
301333
* File::write
302334
*
@@ -307,24 +339,24 @@ public function read( $useIncludePath = false, $context = null )
307339
*
308340
* @return bool
309341
*/
310-
public function write( $contents, $mode = 'wb' )
342+
public function write( $filePath, $contents, $mode = 'wb' )
311343
{
312-
if ( ! $fp = @fopen( $this->getRealPath(), $mode ) ) {
313-
return false;
314-
}
344+
if ( false !== ( $fp = $this->create( $filePath, $mode ) ) ) {
345+
flock( $fp, LOCK_EX );
315346

316-
flock( $fp, LOCK_EX );
317-
318-
for ( $result = $written = 0, $length = strlen( $contents ); $written < $length; $written += $result ) {
319-
if ( ( $result = fwrite( $fp, substr( $contents, $written ) ) ) === false ) {
320-
break;
347+
for ( $result = $written = 0, $length = strlen( $contents ); $written < $length; $written += $result ) {
348+
if ( ( $result = fwrite( $fp, substr( $contents, $written ) ) ) === false ) {
349+
break;
350+
}
321351
}
322-
}
323352

324-
flock( $fp, LOCK_UN );
325-
fclose( $fp );
353+
flock( $fp, LOCK_UN );
354+
fclose( $fp );
326355

327-
return is_int( $result );
356+
return is_int( $result );
357+
}
358+
359+
return false;
328360
}
329361

330362
// ------------------------------------------------------------------------

src/Files/CsvFile.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
// ------------------------------------------------------------------------
1616

1717
use O2System\Filesystem\Abstracts\AbstractFile;
18+
use O2System\Filesystem\File;
1819

1920
/**
2021
* Class CsvFile
@@ -23,6 +24,8 @@
2324
*/
2425
class CsvFile extends AbstractFile
2526
{
27+
protected $fileExtension = '.csv';
28+
2629
/**
2730
* CsvFile::readFile
2831
*
@@ -67,9 +70,14 @@ public function writeFile( $filePath = null, array $options = [] )
6770
? $this->filePath
6871
: $filePath;
6972

70-
$handle = fopen( $filePath, 'wb' );
73+
$handle = ( new File() )->create( $filePath );
7174

72-
foreach ( $this->getArrayCopy() as $list ) {
75+
foreach ( $this->getArrayCopy() as $key => $value ) {
76+
if ( ! is_array( $value ) ) {
77+
$list = [ $key, $value ];
78+
} else {
79+
$list = $value;
80+
}
7381
fputcsv( $handle, $list );
7482
}
7583

src/Files/IniFile.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
*/
2525
class IniFile extends AbstractFile
2626
{
27+
protected $fileExtension = '.ini';
28+
2729
/**
2830
* IniFile::readFile
2931
*
@@ -122,6 +124,6 @@ public function writeFile( $filePath = null, array $options = [] )
122124
}
123125
}
124126

125-
return ( new File( $filePath ) )->write( $content );
127+
return ( new File() )->write( $filePath, $content );
126128
}
127129
}

src/Files/JsonFile.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
*/
2525
class JsonFile extends AbstractFile
2626
{
27+
protected $fileExtension = '.json';
28+
2729
/**
2830
* JsonFile::readFile
2931
*
@@ -66,7 +68,7 @@ public function writeFile( $filePath = null, array $options = [] )
6668
: $filePath;
6769

6870
if ( $this->count() ) {
69-
return ( new File( $filePath ) )->write( json_encode( $this->getArrayCopy(), JSON_PRETTY_PRINT ) );
71+
return ( new File() )->write( $filePath, json_encode( $this->getArrayCopy(), JSON_PRETTY_PRINT ) );
7072
}
7173
}
7274
}

src/Files/XmlFile.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
*/
2525
class XmlFile extends AbstractFile
2626
{
27+
protected $fileExtension = '.xml';
28+
2729
/**
2830
* XmlFile::readFile
2931
*
@@ -67,10 +69,11 @@ public function writeFile( $filePath = null, array $options = [] )
6769
if ( $this->count() ) {
6870
$root = '<' . pathinfo( $filePath, PATHINFO_FILENAME ) . '/>';
6971

72+
$contents = $this->getArrayCopy();
7073
$xml = new \SimpleXMLElement( $root );
71-
array_walk_recursive( $this->getArrayCopy(), [ $xml, 'addChild' ] );
74+
array_walk_recursive( $contents, [ &$xml, 'addChild' ] );
7275

73-
return ( new File( $filePath ) )->write( $xml->asXML() );
76+
return ( new File() )->write( $filePath, $xml->asXML() );
7477
}
7578
}
7679
}

0 commit comments

Comments
 (0)