-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:axypro/fs-ifs
- Loading branch information
Showing
7 changed files
with
524 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
Oops, something went wrong.