Skip to content

Commit

Permalink
Merge branch 'master' of github.com:axypro/fs-ifs
Browse files Browse the repository at this point in the history
  • Loading branch information
vasa-c committed Jan 8, 2018
2 parents 1ebed30 + d4b21d7 commit c89dd14
Show file tree
Hide file tree
Showing 7 changed files with 524 additions and 10 deletions.
11 changes: 6 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
### dev
### 0.0.4 (11.05.2017)

* `Factory`: Factory of FS-implementation
* Travis: PHP 7.1

### 0.0.3 (02.04.2016)
Expand All @@ -12,7 +13,7 @@

### 0.0.1 (01.04.2016)

* IFS
* IFile
* Stat
* MetaData
* `IFS`
* `IFile`
* `Stat`
* `MetaData`
82 changes: 82 additions & 0 deletions Factory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
/**
* @package axy\fs\ifs
* @author Oleg Grigoriev <go.vasac@gmail.com>
*/

namespace axy\fs\ifs;

/**
* Factory of FS-implementation
*/
class Factory
{
/**
* @param mixed $params [optional]
* (null, string, IFS)
* @return IFS
* @throws \LogicException
*/
public static function build($params = null)
{
if ($params === null) {
return self::getStandard();
}
if (is_object($params)) {
if (!($params instanceof IFS)) {
throw new \LogicException(get_class($params).' does not implement IFS');
}
return $params;
}
if (!class_exists($params, true)) {
throw new \LogicException('FS class '.$params.' not found');
}
if (!is_subclass_of($params, self::$interface)) {
throw new \LogicException($params.' does not implement IFS');
}
return new $params();
}

/**
* Returns the standard implementation
*
* @return IFS
* @throws \LogicException
* the standard implementation not installed
*/
public static function getStandard()
{
if (self::$standard === null) {
$cn = self::$standardCN;
if (!class_exists($cn, true)) {
throw new \LogicException('The standard FS implementation not installed');
}
if (!is_subclass_of($cn, self::$interface)) {
throw new \LogicException('OMG, standard FS is fake');
}
self::$standard = new $cn;
}
return self::$standard;
}

/**
* Class name of the standard FS
*
* @var string
*/
private static $standardCN = 'axy\fs\real\FS';

/**
* Required interface of FS
*
* @var string
*/
private static $interface = 'axy\fs\ifs\IFS';

/**
* Cache of the standard instance
*
* @var IFS
*/
private static $standard;
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ For example, work with real FS is [axy/fs-real](https://github.com/axypro/fs-rea
* [Stat](doc/Stat.md) - result of `stat()`.
* [MetaData](doc/MetaData.md) - meta data of an opened file.
* [Exceptions](doc/errors.md).

* [Factory](doc/Factory.md).
17 changes: 17 additions & 0 deletions doc/Factory.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# `Factory`

Builds and returns an implementation of IFS.

## `getStandard(): IFS`

Returns the "standard" implementation ([axy/fs/real/FS](https://github.com/axypro/fs-real)).

If package `axy/fs-real` not installed thrown an exception.

## `build([$params]): IFS`

Builds implementation by parameters:

* `null` - returns the standard implementation
* a string - the class name of an implementation (must implement `IFS`)
* an object implements `IFS` - returns it
12 changes: 8 additions & 4 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@
</testsuites>

<filter>
<blacklist>
<directory>./vendor/</directory>
</blacklist>
<whitelist>
<directory>./</directory>
<exclude>
<directory>./vendor/</directory>
<directory>./tests/</directory>
<file>./index.php</file>
</exclude>
</whitelist>
</filter>
</phpunit>

83 changes: 83 additions & 0 deletions tests/FactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
/**
* @package axy\fs\ifs
* @author Oleg Grigoriev <go.vasac@gmail.com>
*/

namespace axy\fs\ifs\tests;

use axy\fs\ifs\Factory;
use axy\fs\ifs\tests\tst\FakeFS;

/**
* coversDefaultClass axy\fs\ifs\Factory
*/
class FactoryTest extends \PHPUnit_Framework_TestCase
{
/**
* covers ::build
*/
public function testBuild()
{
$fake = new FakeFS();
$this->assertSame($fake, Factory::build($fake));
$another = Factory::build(get_class($fake));
$this->assertInstanceOf(get_class($fake), $another);
$this->assertNotSame($fake, $another);
}

/**
* covers ::build
* @dataProvider providerBuildErrors
* @param mixed $params
* @param string $message
*/
public function testBuildErrors($params, $message)
{
$this->setExpectedException('LogicException', $message);
Factory::build($params);
}

/**
* @return array
*/
public function providerBuildErrors()
{
$data = [
[
$this,
'does not implement IFS',
],
[
__CLASS__,
'does not implement IFS',
],
[
__NAMESPACE__.'/NotFound',
'not found',
],
];
if (!class_exists('axy\fs\real\FS')) {
$data[] = [
null,
'not installed',
];
}
return $data;
}

/**
* covers ::getStandard
*/
public function testGetStandard()
{
if (class_exists('axy\fs\real\FS')) {
$imp = Factory::getStandard();
$this->assertInstanceOf('axy\fs\real\FS', $imp);
$this->assertSame($imp, Factory::getStandard());
return;
}
$this->setExpectedException('LogicException', 'not installed');
Factory::getStandard();
}
}
Loading

0 comments on commit c89dd14

Please sign in to comment.