-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Detect automatically the available asset manager
- Loading branch information
1 parent
201420a
commit fa54167
Showing
5 changed files
with
251 additions
and
9 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,95 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Foxy package. | ||
* | ||
* (c) François Pluchino <francois.pluchino@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Foxy\Asset; | ||
|
||
use Foxy\Exception\RuntimeException; | ||
|
||
/** | ||
* Asset Manager finder. | ||
* | ||
* @author François Pluchino <francois.pluchino@gmail.com> | ||
*/ | ||
class AssetManagerFinder | ||
{ | ||
/** | ||
* @var AssetManagerInterface[] | ||
*/ | ||
private $managers; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param AssetManagerInterface[] $managers The asset managers | ||
*/ | ||
public function __construct(array $managers = array()) | ||
{ | ||
foreach ($managers as $manager) { | ||
if ($manager instanceof AssetManagerInterface) { | ||
$this->addManager($manager); | ||
} | ||
} | ||
} | ||
|
||
public function addManager(AssetManagerInterface $manager) | ||
{ | ||
$this->managers[$manager->getName()] = $manager; | ||
} | ||
|
||
/** | ||
* Find the asset manager. | ||
* | ||
* @param null|string $manager The name of the asset manager | ||
* | ||
* @throws RuntimeException When the asset manager does not exist | ||
* @throws RuntimeException When the asset manager is not found | ||
* | ||
* @return AssetManagerInterface | ||
*/ | ||
public function findManager($manager = null) | ||
{ | ||
if (null !== $manager) { | ||
if (isset($this->managers[$manager])) { | ||
return $this->managers[$manager]; | ||
} | ||
|
||
throw new RuntimeException(sprintf('The asset manager "%s" doesn\'t exist', $manager)); | ||
} | ||
|
||
return $this->findAvailableManager(); | ||
} | ||
|
||
/** | ||
* Find the available asset manager. | ||
* | ||
* @throws RuntimeException When no asset manager is found | ||
* | ||
* @return AssetManagerInterface | ||
*/ | ||
private function findAvailableManager() | ||
{ | ||
// find asset manager by lockfile | ||
foreach ($this->managers as $manager) { | ||
if ($manager->hasLockFile()) { | ||
return $manager; | ||
} | ||
} | ||
|
||
// find asset manager by availability | ||
foreach ($this->managers as $manager) { | ||
if ($manager->isAvailable()) { | ||
return $manager; | ||
} | ||
} | ||
|
||
throw new RuntimeException('No asset manager is found'); | ||
} | ||
} |
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
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,133 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Foxy package. | ||
* | ||
* (c) François Pluchino <francois.pluchino@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Foxy\Tests\Asset; | ||
|
||
use Foxy\Asset\AssetManagerFinder; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* Asset manager finder tests. | ||
* | ||
* @author François Pluchino <francois.pluchino@gmail.com> | ||
* | ||
* @internal | ||
*/ | ||
final class AssetManagerFinderTest extends TestCase | ||
{ | ||
public function testFindManagerWithValidManager() | ||
{ | ||
$am = $this->getMockBuilder('Foxy\Asset\AssetManagerInterface')->getMock(); | ||
|
||
$am->expects(static::once()) | ||
->method('getName') | ||
->willReturn('foo') | ||
; | ||
|
||
$amf = new AssetManagerFinder(array($am)); | ||
$res = $amf->findManager('foo'); | ||
|
||
static::assertSame($am, $res); | ||
} | ||
|
||
/** | ||
* @expectedException \Foxy\Exception\RuntimeException | ||
* @expectedExceptionMessage The asset manager "bar" doesn't exist | ||
*/ | ||
public function testFindManagerWithInvalidManager() | ||
{ | ||
$am = $this->getMockBuilder('Foxy\Asset\AssetManagerInterface')->getMock(); | ||
|
||
$am->expects(static::once()) | ||
->method('getName') | ||
->willReturn('foo') | ||
; | ||
|
||
$amf = new AssetManagerFinder(array($am)); | ||
$amf->findManager('bar'); | ||
} | ||
|
||
public function testFindManagerWithAutoManagerAndAvailableManagerByLockFile() | ||
{ | ||
$am = $this->getMockBuilder('Foxy\Asset\AssetManagerInterface')->getMock(); | ||
|
||
$am->expects(static::once()) | ||
->method('getName') | ||
->willReturn('foo') | ||
; | ||
|
||
$am->expects(static::once()) | ||
->method('hasLockFile') | ||
->willReturn(true) | ||
; | ||
|
||
$am->expects(static::never()) | ||
->method('isAvailable') | ||
; | ||
|
||
$amf = new AssetManagerFinder(array($am)); | ||
$res = $amf->findManager(null); | ||
|
||
static::assertSame($am, $res); | ||
} | ||
|
||
public function testFindManagerWithAutoManagerAndAvailableManagerByAvailability() | ||
{ | ||
$am = $this->getMockBuilder('Foxy\Asset\AssetManagerInterface')->getMock(); | ||
|
||
$am->expects(static::once()) | ||
->method('getName') | ||
->willReturn('foo') | ||
; | ||
|
||
$am->expects(static::once()) | ||
->method('hasLockFile') | ||
->willReturn(false) | ||
; | ||
|
||
$am->expects(static::once()) | ||
->method('isAvailable') | ||
->willReturn(true) | ||
; | ||
|
||
$amf = new AssetManagerFinder(array($am)); | ||
$res = $amf->findManager(null); | ||
|
||
static::assertSame($am, $res); | ||
} | ||
|
||
/** | ||
* @expectedException \Foxy\Exception\RuntimeException | ||
* @expectedExceptionMessage No asset manager is found | ||
*/ | ||
public function testFindManagerWithAutoManagerAndNoAvailableManager() | ||
{ | ||
$am = $this->getMockBuilder('Foxy\Asset\AssetManagerInterface')->getMock(); | ||
|
||
$am->expects(static::atLeastOnce()) | ||
->method('getName') | ||
->willReturn('foo') | ||
; | ||
|
||
$am->expects(static::once()) | ||
->method('hasLockFile') | ||
->willReturn(false) | ||
; | ||
|
||
$am->expects(static::once()) | ||
->method('isAvailable') | ||
->willReturn(false) | ||
; | ||
|
||
$amf = new AssetManagerFinder(array($am)); | ||
$amf->findManager(null); | ||
} | ||
} |