Skip to content

Commit

Permalink
Created mkDir method
Browse files Browse the repository at this point in the history
  • Loading branch information
onysko committed Dec 11, 2014
1 parent 1467dd1 commit 4f0a758
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 24 deletions.
22 changes: 7 additions & 15 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,10 @@ class FileServiceConfig extends \samson\core\Config
/**@var string Configured module/service identifier */
public $__id = 'fs';

/**@var string Set Amazon Web Services as web-application file service using its class name */
public $fileServiceClassName = 'samson\fs\AWSFileService';

/** Specify all other AWS related parameters */
public $bucket = '...';
/**@var string Set Amazon Web Services as web-application file service using its identifier */
public $fileServiceID = 'samson\fs\AWSFileService';
}
```
> When configuring external file service all configuration is do via ```fs``` module configuration class. You can define any public field in \samson\core\Config ancestor class and it will be automatically passed to internal file service handle as configuration, only one rule should be met - external file service field that you want to configure should be declared as ```public```. For example, you can read more information about configurating [SamsonPHP AWS file service ](https://github.com/samsonos/php_fs_aws/).

## Usage

Expand All @@ -49,29 +45,25 @@ To work with this SamsonPHP file service you should get file service instance po
$fs = & m('fs');
```
After this you can use all available methods from [```AbstractFileService``` interface](https://github.com/samsonos/php_fs/blob/master/src/IFileSystem.php), which this SamsonPHP file service(```fs```) implements.
All this method call acts like a proxy and passes them to currently configured file service(by default ```\samson\fs\LocalFileService```).
All this method call act like a proxy and passes them to currently configured file service(by default ```php_fs_local```).

Example usage:
```php
if (!$fs->exists(...)) {
$fs->write(...);
foreach ($fs->dir(...) as $file) {
...
}
$fs->delete();
}
```

### Using service in tests
First of all you should create file service instance and store it as test class field:
First of all you should create service instance:
```php
// Create service instance
$this->fileService = new \samson\fs\FileService(__DIR__.'../');
// Create instance
$this->fileService = new FileService(__DIR__.'../');
```
In other places called after service creation you should retrieve service object via factory method:
```php
// Get instance using services factory as error will signal other way
$this->fileService = \samson\core\Service::getInstance('samson\fs\FileService');
```

> All other SamsonPHP modules must and already use this file service approach when working with file systems.
> All other SamsonPHP modules must and use this file service approach when working with files.
10 changes: 10 additions & 0 deletions src/AbstractFileService.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,14 @@ public function copyPath($filePath, $newPath)
);
}
}

/**
* Create catalog in selected location
* @param string $path Path for new catalog
* @return boolean Result of catalog creating
*/
public function mkDir($path)
{
return true;
}
}
10 changes: 10 additions & 0 deletions src/FileService.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,14 @@ public function copyPath($filePath, $newPath)
{
return $this->fileService->copyPath($filePath, $newPath);
}

/**
* Create catalog in selected location
* @param string $path Path for new catalog
* @return boolean Result of catalog creating
*/
public function mkDir($path)
{
return $this->fileService->mkDir($path);
}
}
7 changes: 7 additions & 0 deletions src/IFileSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,11 @@ public function copyPath($filePath, $newPath);
* @return array $result Resulting collection used in recursion
*/
public function dir($path, $restrict = array(), & $result = array());

/**
* Create catalog in selected location
* @param string $path Path for new catalog
* @return boolean Result of catalog creating
*/
public function mkDir($path);
}
25 changes: 20 additions & 5 deletions src/LocalFileService.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,20 +96,21 @@ public function isDir($filePath)
public function dir($path, $restrict = array(), & $result = array())
{
// Check if we can read this path
if (($handle = @opendir($path)) !== false) {
if (($handle = opendir($path)) !== false) {
// Fastest reading method
while (false !== ($entry = readdir($handle))) {
// Ignore root paths
if ($entry != '..' && $entry != '.') {
// Build full REAL path to entry
$fullPath = realpath($path . '/' . $entry);

// Check if this folder is not in ignored list
if ($this->isDir($fullPath) && (in_array($fullPath, $restrict) === false)) {
// If this is a file
if (!$this->isDir($fullPath)) {
$result[] = $fullPath;
} elseif (in_array($fullPath, $restrict) === false) {
// Check if this folder is not in ignored list
// If this is a folder - go deeper in recursion
$this->dir($fullPath, $restrict, $result);
} else { // If this is a file
$result[] = $fullPath;
}
}
}
Expand All @@ -123,4 +124,18 @@ public function dir($path, $restrict = array(), & $result = array())

return $result;
}

/**
* Create catalog in selected location
* @param string $path Path for new catalog
* @return boolean Result of catalog creating
*/
public function mkDir($path)
{
if (!file_exists($path)) {
mkdir($path, 0775, true);
return true;
}
return false;
}
}
9 changes: 5 additions & 4 deletions tests/MainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ public function testRelativePath()
// Create test dir
$testDir = sys_get_temp_dir().'/testDir/';
if (!$this->fileService->exists($testDir)) {
mkdir($testDir, 0777);
$this->fileService->mkDir($testDir);
$this->fileService->mkDir($testDir);
}

$testDirRelative = $this->fileService->relativePath($testDir, $fileName, sys_get_temp_dir());
Expand All @@ -123,7 +124,7 @@ public function testCopy()
// Create test dir
$testDir = sys_get_temp_dir().'/testDir/';
if (!$this->fileService->exists($testDir)) {
mkdir($testDir, 0777);
$this->fileService->mkDir($testDir);
}

// Try to null source file
Expand Down Expand Up @@ -185,9 +186,9 @@ public function testMime()
public function testDir()
{
// Scan project root dir
$files = $this->fileService->dir(dirname(dirname(__FILE__)).'TEST');
$files = $this->fileService->dir(dirname(dirname(__FILE__)));

// Perform test
$this->assertEquals(false, in_array(__FILE__, $files), 'File service dir failed - This file is not found in listing');
$this->assertEquals(true, in_array(__FILE__, $files), 'File service dir failed - This file is not found in listing');
}
}

0 comments on commit 4f0a758

Please sign in to comment.