Skip to content

Commit

Permalink
chore(app): fixing some internal inconsistencies with dependencies an…
Browse files Browse the repository at this point in the history
…d attributes lifecycles
  • Loading branch information
razshare committed Dec 22, 2023
1 parent 2876ad1 commit 17bc5ec
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 46 deletions.
2 changes: 1 addition & 1 deletion .php-cs-fixer.cache
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"php":"8.2.12","version":"3.40.2:v3.40.2#4344562a516b76afe8f2d64b2e52214c30d64ed8","indent":" ","lineEnding":"\n","rules":{"align_multiline_comment":{"comment_type":"all_multiline"},"array_indentation":true,"array_syntax":{"syntax":"short"},"binary_operator_spaces":{"default":"align_single_space_minimal"},"blank_line_after_namespace":true,"braces":{"allow_single_line_anonymous_class_with_empty_body":true,"allow_single_line_closure":true,"position_after_anonymous_constructs":"same","position_after_control_structures":"same","position_after_functions_and_oop_constructs":"same"},"cast_spaces":{"space":"none"},"class_definition":true,"clean_namespace":true,"compact_nullable_typehint":true,"concat_space":{"spacing":"none"},"constant_case":true,"control_structure_continuation_position":{"position":"same_line"},"function_declaration":{"closure_function_spacing":"none","trailing_comma_single_line":true},"function_typehint_space":true,"heredoc_indentation":true,"indentation_type":true,"list_syntax":true,"method_argument_space":{"after_heredoc":false,"keep_multiple_spaces_after_comma":false,"on_multiline":"ensure_fully_multiline"},"method_chaining_indentation":true,"native_function_type_declaration_casing":true,"no_spaces_after_function_name":true,"no_spaces_around_offset":true,"no_unneeded_curly_braces":true,"no_unused_imports":true,"ordered_imports":true,"phpdoc_align":{"align":"vertical"},"phpdoc_indent":true,"phpdoc_order":true,"phpdoc_scalar":true,"yoda_style":{"always_move_variable":true,"equal":true,"identical":true,"less_and_greater":false}},"hashes":{"src\/lib\/Attributes\/Arguments.php":"3b9b083ade37e452a6c48cbdc4337e49","src\/lib\/Interfaces\/AttributeInterface.php":"1334828525b6b9ea79306f0714ea2c8d","src\/lib\/Attributes\/Option.php":"932f4b8ab41c68f1d72d97266e229a95","src\/lib\/Traits\/CoreAttributeDefinition.php":"51b397fa3f631ac0678520a76bad8df3","src\/lib\/Container.php":"926d27ee978cc648bf964a9d87b4c17d","src\/lib\/DependenciesOptions.php":"571edbbb09f330d90f9dfbd610696eb3"}}
{"php":"8.2.13","version":"3.41.1:v3.41.1#8b6ae8dcbaf23f09680643ab832a4a3a260265f6","indent":" ","lineEnding":"\n","rules":{"align_multiline_comment":{"comment_type":"all_multiline"},"array_indentation":true,"array_syntax":{"syntax":"short"},"binary_operator_spaces":{"default":"align_single_space_minimal"},"blank_line_after_namespace":true,"braces":{"allow_single_line_anonymous_class_with_empty_body":true,"allow_single_line_closure":true,"position_after_anonymous_constructs":"same","position_after_control_structures":"same","position_after_functions_and_oop_constructs":"same"},"cast_spaces":{"space":"none"},"class_definition":true,"clean_namespace":true,"compact_nullable_typehint":true,"concat_space":{"spacing":"none"},"constant_case":true,"control_structure_continuation_position":{"position":"same_line"},"function_declaration":{"closure_function_spacing":"none","trailing_comma_single_line":true},"function_typehint_space":true,"heredoc_indentation":true,"indentation_type":true,"list_syntax":true,"method_argument_space":{"after_heredoc":false,"keep_multiple_spaces_after_comma":false,"on_multiline":"ensure_fully_multiline"},"method_chaining_indentation":true,"native_function_type_declaration_casing":true,"no_spaces_after_function_name":true,"no_spaces_around_offset":true,"no_unneeded_curly_braces":true,"no_unused_imports":true,"ordered_imports":true,"phpdoc_align":{"align":"vertical"},"phpdoc_indent":true,"phpdoc_order":true,"phpdoc_scalar":true,"yoda_style":{"always_move_variable":true,"equal":true,"identical":true,"less_and_greater":false}},"hashes":{"src\/lib\/Traits\/CoreAttributeDefinition.php":"47c01538c489131625f523bc33045775","src\/lib\/Interfaces\/AttributeInterface.php":"ab472b11b6437e4d083bd519f2d49665","src\/lib\/Attributes\/Option.php":"07175cfe6a780b0549d5ffdde4b1d7d2","src\/lib\/Attributes\/Arguments.php":"386d40864598d23d54dc06000c63a217"}}
61 changes: 33 additions & 28 deletions src/lib/Attributes/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use Attribute;
use CatPaw\Bootstrap;
use CatPaw\DependenciesOptions;
use CatPaw\Interfaces\AttributeInterface;
use CatPaw\ReflectionTypeManager;
use CatPaw\Traits\CoreAttributeDefinition;
Expand Down Expand Up @@ -41,19 +42,45 @@ public function __construct(
self::init();
}

public static function findByMethod(ReflectionMethod $reflectionMethod) {
public static function findByMethod(ReflectionMethod $reflectionMethod):self|false {
return false;
}

public static function findByClass(ReflectionClass $reflectionClass) {
public static function findByClass(ReflectionClass $reflectionClass):self|false {
return false;
}

public static function findByProperty(ReflectionProperty $reflectionProperty) {
public static function findByProperty(ReflectionProperty $reflectionProperty):self|false {
return false;
}

public function onRouteMount(ReflectionFunction $reflection, Closure &$value, mixed $context) {
public function onRouteMount(ReflectionFunction $reflection, Closure &$value, DependenciesOptions $options):void {
}

public function onClassMount(ReflectionClass $reflection, mixed &$value, mixed $context) {
public function onClassMount(ReflectionClass $reflection, mixed &$value, DependenciesOptions $options):void {
}


public function onParameterMount(ReflectionParameter $reflection, mixed &$value, DependenciesOptions $options):void {
if (isset(self::$cache[$this->name])) {
$value = self::$cache[$this->name];
return;
}


/** @var string|int|bool|float $value */

$wrapper = ReflectionTypeManager::wrap($reflection);

$value = $this->findValue(
className: $wrapper->getClassName(),
allowsNullValue: $wrapper->allowsNullValue(),
allowsDefaultValue: $wrapper->allowsDefaultValue(),
defaultValue: $wrapper->getDefaultValue(),
allowsBoolean: $wrapper->allowsBoolean(),
allowsTrue: $wrapper->allowsTrue(),
allowsFalse: $wrapper->allowsFalse(),
);
}

// public static function renderLinuxManual():string {
Expand All @@ -73,7 +100,7 @@ public static function exists(string $option) {
return self::$exists[$option] = in_array($option, self::$options);
}

public static function init() {
private static function init() {
global $argv;
$index = 0;
$listingOptions = false;
Expand Down Expand Up @@ -197,26 +224,4 @@ public function findValue(

return self::$cache[$this->name] = $value;
}

public function onParameterMount(ReflectionParameter $reflection, mixed &$value, mixed $context) {
if (isset(self::$cache[$this->name])) {
$value = self::$cache[$this->name];
return;
}


/** @var string|int|bool|float $value */
/** @var false $context */
$wrapper = ReflectionTypeManager::wrap($reflection);

$value = $this->findValue(
className: $wrapper->getClassName(),
allowsNullValue: $wrapper->allowsNullValue(),
allowsDefaultValue: $wrapper->allowsDefaultValue(),
defaultValue: $wrapper->getDefaultValue(),
allowsBoolean: $wrapper->allowsBoolean(),
allowsTrue: $wrapper->allowsTrue(),
allowsFalse: $wrapper->allowsFalse(),
);
}
}
4 changes: 2 additions & 2 deletions src/lib/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public static function dependencies(
false|DependenciesOptions $options = false,
):array {
if (!$options) {
$options = new DependenciesOptions(
$options = DependenciesOptions::create(
ids: [],
overwrites:[],
provides: [],
Expand Down Expand Up @@ -438,7 +438,7 @@ public static function create(
if ($singleton || $service) {
self::$singletons[$className] = $instance;
if ($service) {
$service->onClassMount($reflection, $instance, new DependenciesOptions(
$service->onClassMount($reflection, $instance, DependenciesOptions::create(
ids: [],
overwrites:[],
provides: [],
Expand Down
28 changes: 27 additions & 1 deletion src/lib/DependenciesOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,33 @@ class DependenciesOptions {
* @param array<mixed> $defaultArguments,
* @param mixed $context,
*/
public function __construct(
public static function create(
array $ids,
array $overwrites,
array $provides,
array $fallbacks,
array $defaultArguments,
mixed $context,
):self {
return new self(
ids: $ids,
overwrites: $overwrites,
provides: $provides,
fallbacks: $fallbacks,
defaultArguments: $defaultArguments,
context: $context,
);
}

/**
* @param array<string> $ids,
* @param array<callable> $overwrites,
* @param array<callable> $provides,
* @param array<callable> $fallbacks,
* @param array<mixed> $defaultArguments,
* @param mixed $context,
*/
private function __construct(
public array $ids,
public array $overwrites,
public array $provides,
Expand Down
16 changes: 8 additions & 8 deletions src/lib/Interfaces/AttributeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
use ReflectionProperty;

interface AttributeInterface {
public static function findAllByFunction(ReflectionFunction $reflectionFunction);
public static function findAllByFunction(ReflectionFunction $reflectionFunction): array|false ;

public static function findByFunction(ReflectionFunction $reflectionFunction);
public static function findByFunction(ReflectionFunction $reflectionFunction): self|false;

public static function findByMethod(ReflectionMethod $reflectionMethod);
public static function findByMethod(ReflectionMethod $reflectionMethod): self|false;

public static function findByClass(ReflectionClass $reflectionClass);
public static function findByClass(ReflectionClass $reflectionClass): self|false;

public static function findByProperty(ReflectionProperty $reflectionProperty);
public static function findByProperty(ReflectionProperty $reflectionProperty): self|false;

/**
* Invoked when this attribute is detected on a parameter.
Expand All @@ -28,7 +28,7 @@ public static function findByProperty(ReflectionProperty $reflectionProperty);
* @param DependenciesOptions $options options used to find dependencies.
* @return void
*/
public function onParameterMount(ReflectionParameter $reflection, mixed &$value, DependenciesOptions $options);
public function onParameterMount(ReflectionParameter $reflection, mixed &$value, DependenciesOptions $options):void;


/**
Expand All @@ -40,7 +40,7 @@ public function onParameterMount(ReflectionParameter $reflection, mixed &$value,
* @param DependenciesOptions $options options used to find dependencies.
* @return void
*/
public function onRouteMount(ReflectionFunction $reflection, Closure &$value, DependenciesOptions $options);
public function onRouteMount(ReflectionFunction $reflection, Closure &$value, DependenciesOptions $options):void;


/**
Expand All @@ -50,5 +50,5 @@ public function onRouteMount(ReflectionFunction $reflection, Closure &$value, De
* @param DependenciesOptions $options options used to find dependencies.
* @return void
*/
public function onClassMount(ReflectionClass $reflection, mixed &$value, DependenciesOptions $options);
public function onClassMount(ReflectionClass $reflection, mixed &$value, DependenciesOptions $options):void;
}
10 changes: 4 additions & 6 deletions src/lib/Traits/CoreAttributeDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use CatPaw\Attributes\AttributeResolver;
use CatPaw\Attributes\Entry;
use CatPaw\Container;
use CatPaw\DependenciesOptions;
use Closure;
use ReflectionClass;
use ReflectionFunction;
Expand Down Expand Up @@ -187,15 +188,12 @@ public static function findByParameter(ReflectionParameter $reflectionParameter)
return $instance;
}

public function onParameterMount(ReflectionParameter $reflection, mixed &$value, mixed $context) {
return;
public function onParameterMount(ReflectionParameter $reflection, mixed &$value, DependenciesOptions $options):void {
}

public function onRouteMount(ReflectionFunction $reflection, Closure &$value, mixed $context) {
return;
public function onRouteMount(ReflectionFunction $reflection, Closure &$value, DependenciesOptions $options):void {
}

public function onClassMount(ReflectionClass $reflection, mixed &$value, mixed $context) {
return;
public function onClassMount(ReflectionClass $reflection, mixed &$value, DependenciesOptions $options):void {
}
}

0 comments on commit 17bc5ec

Please sign in to comment.