-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add first version of transformers sequence
- Loading branch information
1 parent
2b9a634
commit ac5d95a
Showing
7 changed files
with
105 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace AmpProject\Optimizer\Exception; | ||
|
||
use DomainException; | ||
|
||
/** | ||
* Exception thrown when an invalid transformer sequence is provided | ||
* or missed transformer which required by another transformer. | ||
* | ||
* @package ampproject/amp-toolbox | ||
*/ | ||
final class InvalidTransformersSequence extends DomainException implements AmpOptimizerException | ||
{ | ||
|
||
/** | ||
* @param string $transformerClass | ||
* @param array $missingTags | ||
* @return \AmpProject\Optimizer\Exception\InvalidTransformersSequence | ||
*/ | ||
public static function forTransformer($transformerClass, array $missingTags) | ||
{ | ||
$tagList = implode(',', $missingTags); | ||
$message = "Transformer '{$transformerClass}' must be after transformers that provide '{$tagList}' tags."; | ||
|
||
return new self($message); | ||
} | ||
} |
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
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,14 @@ | ||
<?php | ||
|
||
namespace AmpProject\Optimizer; | ||
|
||
interface TransformerProvides | ||
{ | ||
|
||
/** | ||
* Return the collection of dependencies that this transformer provides. | ||
* | ||
* @return string[] | ||
*/ | ||
public static function provides(); | ||
} |
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,14 @@ | ||
<?php | ||
|
||
namespace AmpProject\Optimizer; | ||
|
||
interface TransformerRequires | ||
{ | ||
|
||
/** | ||
* Return the collection of dependencies that this transformer requires. | ||
* | ||
* @return string[] | ||
*/ | ||
public static function requires(); | ||
} |
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,29 @@ | ||
<?php | ||
|
||
namespace AmpProject\Optimizer; | ||
|
||
use AmpProject\Optimizer\Exception\InvalidTransformersSequence; | ||
|
||
class TransformersSequenceValidator | ||
{ | ||
|
||
/** | ||
* @param string[] $transformers | ||
*/ | ||
public static function validate(array $transformers) | ||
{ | ||
$providedTags = []; | ||
foreach (array_values($transformers) as $transformerClass) { | ||
if ($transformerClass instanceof TransformerRequires) { | ||
$missingTags = array_diff($transformerClass::requires(), $providedTags); | ||
if ($missingTags) { | ||
throw InvalidTransformersSequence::forTransformer($transformerClass, $missingTags); | ||
} | ||
} | ||
|
||
if ($transformerClass instanceof TransformerProvides) { | ||
$providedTags = array_merge($providedTags, $transformerClass::provides()); | ||
} | ||
} | ||
} | ||
} |