Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move tokenizer rules to di #56

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"symfony/expression-language": "^4.2",
"symfony/filesystem": "^4.2",
"symfony/finder": "^4.2",
"symfony/yaml": "^4.2"
"symfony/yaml": "^4.2",
"symfony/options-resolver": "^4.2"
},
"autoload": {
"psr-4": {
Expand Down
56 changes: 55 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions src/V1/Actor/Template/Tokenizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
namespace Neighborhoods\Buphalo\V1\Actor\Template;

use Neighborhoods\Buphalo\V1\Actor;
use Neighborhoods\Buphalo\V1\Actor\Template\Tokenizer\Rule\Builder\RepositoryInterface;

class Tokenizer implements TokenizerInterface
{
use Actor\Template\Tokenizer\Rule\Builder\Repository\AwareTrait;
use Actor\Template\AwareTrait;
use Actor\Template\AnnotationTokenizer\AwareTrait;
use Actor\AwareTrait {
Expand All @@ -27,6 +29,18 @@ public function getTokenizedContents(): string
if ($this->TokenizedContents === null) {
$this->getActorTemplateAnnotationTokenizer()->tokenize();
$templateContents = $this->getActorTemplate()->getTokenizedContents();

$ruleBuilderRepository = $this->getV1ActorTemplateTokenizerRuleBuilderRepository();
$ruleBuilderMap = $ruleBuilderRepository->getMapByFileExtension(RepositoryInterface::FILE_TYPE_ALL);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this pass in the current file extension?

foreach ($ruleBuilderMap as $key => $ruleBuilder) {
$ruleBuilder->setTemplateContents($templateContents);
$ruleBuilder->setActor($this->getActor());
$ruleBuilder->setActorTemplate($this->getActorTemplate());
$rule = $ruleBuilder->build();
$tokenizedContents = $rule->getTokenizedContents();
continue;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no-op continue?

Suggested change
continue;

}

/** @noinspection NotOptimalRegularExpressionsInspection */
$tokenizedContents = preg_replace(
'/namespace(\s+)Neighborhoods\\\BuphaloTemplateTree\\\PrimaryActorName/',
Expand Down
2 changes: 2 additions & 0 deletions src/V1/Actor/Template/Tokenizer.service.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ services:
class: Neighborhoods\Buphalo\V1\Actor\Template\Tokenizer
public: false
shared: false
calls:
- [setV1ActorTemplateTokenizerRuleBuilderRepository, ['@Neighborhoods\Buphalo\V1\Actor\Template\Tokenizer\Rule\Builder\RepositoryInterface']]
57 changes: 57 additions & 0 deletions src/V1/Actor/Template/Tokenizer/Rule.buphalo.v1.fabrication.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
actors:
<PrimaryActorName>Interface.php:
template: PrimaryActorNameInterface.php
<PrimaryActorName>/AwareTrait.php:
template: PrimaryActorName/AwareTrait.php
<PrimaryActorName>/FactoryInterface.php:
template: PrimaryActorName/FactoryInterface.php
<PrimaryActorName>/Factory/AwareTrait.php:
template: PrimaryActorName/Factory/AwareTrait.php
<PrimaryActorName>/BuilderInterface.php:
template: PrimaryActorName/BuilderInterface.php
<PrimaryActorName>/Builder/AwareTrait.php:
template: PrimaryActorName/Builder/AwareTrait.php
<PrimaryActorName>/Builder/FactoryInterface.php:
template: PrimaryActorName/Builder/FactoryInterface.php
<PrimaryActorName>/Builder/Factory/AwareTrait.php:
template: PrimaryActorName/Builder/Factory/AwareTrait.php
<PrimaryActorName>/Repository.php:
template: PrimaryActorName/Repository.php
<PrimaryActorName>/Repository.service.yml:
template: PrimaryActorName/Repository.service.yml
<PrimaryActorName>/RepositoryInterface.php:
template: PrimaryActorName/RepositoryInterface.php
<PrimaryActorName>/Repository/AwareTrait.php:
template: PrimaryActorName/Repository/AwareTrait.php
<PrimaryActorName>/Map.php:
template: PrimaryActorName/Map.php
<PrimaryActorName>/Map.service.yml:
template: PrimaryActorName/Map.service.yml
<PrimaryActorName>/MapInterface.php:
template: PrimaryActorName/MapInterface.php
<PrimaryActorName>/Map/AwareTrait.php:
template: PrimaryActorName/Map/AwareTrait.php
<PrimaryActorName>/Map/Factory.php:
template: PrimaryActorName/Map/Factory.php
<PrimaryActorName>/Map/Factory.service.yml:
template: PrimaryActorName/Map/Factory.service.yml
<PrimaryActorName>/Map/FactoryInterface.php:
template: PrimaryActorName/Map/FactoryInterface.php
<PrimaryActorName>/Map/Factory/AwareTrait.php:
template: PrimaryActorName/Map/Factory/AwareTrait.php
<PrimaryActorName>/Map/Builder.php:
template: PrimaryActorName/Map/Builder.php
<PrimaryActorName>/Map/Builder.service.yml:
template: PrimaryActorName/Map/Builder.service.yml
<PrimaryActorName>/Map/BuilderInterface.php:
template: PrimaryActorName/Map/BuilderInterface.php
<PrimaryActorName>/Map/Builder/AwareTrait.php:
template: PrimaryActorName/Map/Builder/AwareTrait.php
<PrimaryActorName>/Map/Builder/Factory.php:
template: PrimaryActorName/Map/Builder/Factory.php
<PrimaryActorName>/Map/Builder/Factory.service.yml:
template: PrimaryActorName/Map/Builder/Factory.service.yml
<PrimaryActorName>/Map/Builder/FactoryInterface.php:
template: PrimaryActorName/Map/Builder/FactoryInterface.php
<PrimaryActorName>/Map/Builder/Factory/AwareTrait.php:
template: PrimaryActorName/Map/Builder/Factory/AwareTrait.php
73 changes: 73 additions & 0 deletions src/V1/Actor/Template/Tokenizer/Rule/Builder/Map.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note-to-self: We should probably move a bunch of stuff like this into a committed fab/ directory.

declare(strict_types=1);

namespace Neighborhoods\Buphalo\V1\Actor\Template\Tokenizer\Rule\Builder;

use Neighborhoods\Buphalo\V1\Actor\Template\Tokenizer\Rule\BuilderInterface;

class Map extends \ArrayIterator implements MapInterface
{
/** @param BuilderInterface ...$builders */
public function __construct(array $builders = array(), int $flags = 0)
{
if ($this->count() !== 0) {
throw new \LogicException('Map is not empty.');
}

if (!empty($builders)) {
$this->assertValidArrayType(...array_values($builders));
}

parent::__construct($builders, $flags);
}

public function offsetGet($index): BuilderInterface
{
return $this->assertValidArrayItemType(parent::offsetGet($index));
}

/** @param BuilderInterface $builder */
public function offsetSet($index, $builder)
{
parent::offsetSet($index, $this->assertValidArrayItemType($builder));
}

/** @param BuilderInterface $builder */
public function append($builder)
{
$this->assertValidArrayItemType($builder);
parent::append($builder);
}

public function current(): BuilderInterface
{
return parent::current();
}

protected function assertValidArrayItemType(BuilderInterface $builder)
{
return $builder;
}

protected function assertValidArrayType(BuilderInterface ...$builders): MapInterface
{
return $this;
}

public function getArrayCopy(): MapInterface
{
return new self(parent::getArrayCopy(), (int)$this->getFlags());
}

public function toArray(): array
{
return (array)$this;
}

public function hydrate(array $array): MapInterface
{
$this->__construct($array);

return $this;
}
}
5 changes: 5 additions & 0 deletions src/V1/Actor/Template/Tokenizer/Rule/Builder/Map.service.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
services:
Neighborhoods\Buphalo\V1\Actor\Template\Tokenizer\Rule\Builder\MapInterface:
class: Neighborhoods\Buphalo\V1\Actor\Template\Tokenizer\Rule\Builder\Map
public: false
shared: false
45 changes: 45 additions & 0 deletions src/V1/Actor/Template/Tokenizer/Rule/Builder/Map/AwareTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
declare(strict_types=1);

namespace Neighborhoods\Buphalo\V1\Actor\Template\Tokenizer\Rule\Builder\Map;

use Neighborhoods\Buphalo\V1\Actor\Template\Tokenizer\Rule\Builder\MapInterface;

trait AwareTrait
{
protected $NeighborhoodsBuphaloV1ActorTemplateTokenizerRuleBuilderMap;

public function setV1ActorTemplateTokenizerRuleBuilderMap(MapInterface $v1ActorTemplateTokenizerRuleBuilderMap
): self {
if ($this->hasV1ActorTemplateTokenizerRuleBuilderMap()) {
throw new \LogicException('NeighborhoodsBuphaloV1ActorTemplateTokenizerRuleBuilderMap is already set.');
}
$this->NeighborhoodsBuphaloV1ActorTemplateTokenizerRuleBuilderMap = $v1ActorTemplateTokenizerRuleBuilderMap;

return $this;
}

protected function getV1ActorTemplateTokenizerRuleBuilderMap(): MapInterface
{
if (!$this->hasV1ActorTemplateTokenizerRuleBuilderMap()) {
throw new \LogicException('NeighborhoodsBuphaloV1ActorTemplateTokenizerRuleBuilderMap is not set.');
}

return $this->NeighborhoodsBuphaloV1ActorTemplateTokenizerRuleBuilderMap;
}

protected function hasV1ActorTemplateTokenizerRuleBuilderMap(): bool
{
return isset($this->NeighborhoodsBuphaloV1ActorTemplateTokenizerRuleBuilderMap);
}

protected function unsetV1ActorTemplateTokenizerRuleBuilderMap(): self
{
if (!$this->hasV1ActorTemplateTokenizerRuleBuilderMap()) {
throw new \LogicException('NeighborhoodsBuphaloV1ActorTemplateTokenizerRuleBuilderMap is not set.');
}
unset($this->NeighborhoodsBuphaloV1ActorTemplateTokenizerRuleBuilderMap);

return $this;
}
}
16 changes: 16 additions & 0 deletions src/V1/Actor/Template/Tokenizer/Rule/Builder/Map/Factory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
declare(strict_types=1);

namespace Neighborhoods\Buphalo\V1\Actor\Template\Tokenizer\Rule\Builder\Map;

use Neighborhoods\Buphalo\V1\Actor\Template\Tokenizer\Rule\Builder\MapInterface;

class Factory implements FactoryInterface
{
use AwareTrait;

public function create(): MapInterface
{
return $this->getV1ActorTemplateTokenizerRuleBuilderMap()->getArrayCopy();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
services:
Neighborhoods\Buphalo\V1\Actor\Template\Tokenizer\Rule\Builder\Map\FactoryInterface:
class: Neighborhoods\Buphalo\V1\Actor\Template\Tokenizer\Rule\Builder\Map\Factory
public: false
shared: true
calls:
- [setV1ActorTemplateTokenizerRuleBuilderMap, ['@Neighborhoods\Buphalo\V1\Actor\Template\Tokenizer\Rule\Builder\MapInterface']]
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
declare(strict_types=1);

namespace Neighborhoods\Buphalo\V1\Actor\Template\Tokenizer\Rule\Builder\Map\Factory;

use Neighborhoods\Buphalo\V1\Actor\Template\Tokenizer\Rule\Builder\Map\FactoryInterface;

trait AwareTrait
{
protected $NeighborhoodsBuphaloV1ActorTemplateTokenizerRuleBuilderMapFactory;

public function setV1ActorTemplateTokenizerRuleBuilderMapFactory(
FactoryInterface $v1ActorTemplateTokenizerRuleBuilderMapFactory
): self {
if ($this->hasV1ActorTemplateTokenizerRuleBuilderMapFactory()) {
throw new \LogicException('NeighborhoodsBuphaloV1ActorTemplateTokenizerRuleBuilderMapFactory is already set.');
}
$this->NeighborhoodsBuphaloV1ActorTemplateTokenizerRuleBuilderMapFactory = $v1ActorTemplateTokenizerRuleBuilderMapFactory;

return $this;
}

protected function getV1ActorTemplateTokenizerRuleBuilderMapFactory(): FactoryInterface
{
if (!$this->hasV1ActorTemplateTokenizerRuleBuilderMapFactory()) {
throw new \LogicException('NeighborhoodsBuphaloV1ActorTemplateTokenizerRuleBuilderMapFactory is not set.');
}

return $this->NeighborhoodsBuphaloV1ActorTemplateTokenizerRuleBuilderMapFactory;
}

protected function hasV1ActorTemplateTokenizerRuleBuilderMapFactory(): bool
{
return isset($this->NeighborhoodsBuphaloV1ActorTemplateTokenizerRuleBuilderMapFactory);
}

protected function unsetV1ActorTemplateTokenizerRuleBuilderMapFactory(): self
{
if (!$this->hasV1ActorTemplateTokenizerRuleBuilderMapFactory()) {
throw new \LogicException('NeighborhoodsBuphaloV1ActorTemplateTokenizerRuleBuilderMapFactory is not set.');
}
unset($this->NeighborhoodsBuphaloV1ActorTemplateTokenizerRuleBuilderMapFactory);

return $this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
declare(strict_types=1);

namespace Neighborhoods\Buphalo\V1\Actor\Template\Tokenizer\Rule\Builder\Map;

use Neighborhoods\Buphalo\V1\Actor\Template\Tokenizer\Rule\Builder\MapInterface;

interface FactoryInterface
{
public function create(): MapInterface;
}
Loading