-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from atournayre/feature/new_architecture
New architecture
- Loading branch information
Showing
88 changed files
with
2,151 additions
and
1,046 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,69 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Atournayre\PHPArkitect\Builder; | ||
|
||
use Arkitect\Rules\DSL\ArchRule; | ||
use Arkitect\Rules\Rule; | ||
use Atournayre\PHPArkitect\Contracts\AndThatInterface; | ||
use Atournayre\PHPArkitect\Contracts\ExceptInterface; | ||
use Atournayre\PHPArkitect\Contracts\RulesInterface; | ||
use Webmozart\Assert\Assert; | ||
|
||
final class RuleBuilder | ||
{ | ||
private array $rules = []; | ||
|
||
private function __construct() | ||
{ | ||
} | ||
|
||
public static function create(): self | ||
{ | ||
return new self(); | ||
} | ||
|
||
public function add(RulesInterface|ArchRule $rule): self | ||
{ | ||
$this->rules[] = $rule instanceof ArchRule ? $rule : $this->buildArchRule($rule); | ||
return $this; | ||
} | ||
|
||
public function set(array $rules): self | ||
{ | ||
Assert::allIsInstanceOf($rules, RulesInterface::class); | ||
|
||
foreach ($rules as $rule) { | ||
$this->add($rule); | ||
} | ||
return $this; | ||
} | ||
|
||
public function rules(): array | ||
{ | ||
return $this->rules; | ||
} | ||
|
||
private function buildArchRule(RulesInterface|ExceptInterface|AndThatInterface $rule): ArchRule | ||
{ | ||
Assert::isInstanceOf($rule, RulesInterface::class); | ||
|
||
$allClasses = Rule::allClasses(); | ||
|
||
if ($rule instanceof ExceptInterface) { | ||
$allClasses = $allClasses->except(...$rule->except()); | ||
} | ||
|
||
$allClasses = $allClasses->that($rule->that()); | ||
|
||
if ($rule instanceof AndThatInterface) { | ||
foreach ($rule->andThat() as $andThatItem) { | ||
$allClasses = $allClasses->andThat($andThatItem); | ||
} | ||
} | ||
|
||
return $allClasses | ||
->should($rule->should()) | ||
->because($rule->because()); | ||
} | ||
} |
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 | ||
declare(strict_types=1); | ||
|
||
namespace Atournayre\PHPArkitect\Contracts; | ||
|
||
use Arkitect\Expression\Expression; | ||
|
||
interface AndThatInterface | ||
{ | ||
/** | ||
* @return array|Expression[] | ||
*/ | ||
public function andThat(): array; | ||
} |
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,9 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Atournayre\PHPArkitect\Contracts; | ||
|
||
interface ExceptInterface | ||
{ | ||
public function except(): array; | ||
} |
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,15 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Atournayre\PHPArkitect\Contracts; | ||
|
||
use Arkitect\Expression\Expression; | ||
|
||
interface RulesInterface | ||
{ | ||
public function that(): Expression; | ||
|
||
public function should(): Expression; | ||
|
||
public function because(): string; | ||
} |
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,53 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Atournayre\PHPArkitect\Expression\ForClasses; | ||
|
||
use Arkitect\Analyzer\ClassDescription; | ||
use Arkitect\Exceptions\FailOnFirstViolationException; | ||
use Arkitect\Expression\Description; | ||
use Arkitect\Expression\Expression; | ||
use Arkitect\Rules\Violation; | ||
use Arkitect\Rules\ViolationMessage; | ||
use Arkitect\Rules\Violations; | ||
|
||
class DependsOnTheseNamespace implements Expression | ||
{ | ||
private string $namespace; | ||
|
||
public function __construct(string $namespace) | ||
{ | ||
$this->namespace = $namespace; | ||
} | ||
|
||
public function describe(ClassDescription $theClass, string $because): Description | ||
{ | ||
return new Description("should depend on class in these namespace: $this->namespace", $because); | ||
} | ||
|
||
/** | ||
* @throws FailOnFirstViolationException | ||
*/ | ||
public function evaluate(ClassDescription $theClass, Violations $violations, string $because): void | ||
{ | ||
$dependencies = $theClass->getDependencies(); | ||
|
||
$filteredDependencies = array_filter($dependencies, function ($dependency) { | ||
return $dependency->matches($this->namespace); | ||
}); | ||
|
||
if (count($filteredDependencies) > 0) { | ||
return; | ||
} | ||
|
||
$violation = Violation::create( | ||
$theClass->getFQCN(), | ||
ViolationMessage::withDescription( | ||
$this->describe($theClass, $because), | ||
"depends on {$this->namespace}" | ||
) | ||
); | ||
|
||
$violations->add($violation); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.