Skip to content

Commit

Permalink
Merge branch 'release/v0.1.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
betterthanclay committed Nov 10, 2023
2 parents 8c05007 + 3b560b9 commit edc6980
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
## v0.1.1 (2023-11-10)
* Added object type dictionary

## v0.1.0 (2023-11-10)
* Built initial implementation
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@ $testObect = $slingshot->instantiate(Test::class, [
]);
```

Objects can be added to Slingshot by type for reference matching:

```php
$object = new Test(...);
$slingshot->addType($object);

$slingshot->invoke(function(Test $test) {
// ...
});
```

## Licensing

Slingshot is licensed under the MIT License. See [LICENSE](./LICENSE) for the full license text.
120 changes: 120 additions & 0 deletions src/Slingshot.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ class Slingshot
*/
protected array $parameters = [];

/**
* @var array<class-string<object>, object>
*/
protected array $types = [];

/**
* Init with container
*
Expand Down Expand Up @@ -168,6 +173,111 @@ public function removeParameter(
return $this;
}



/**
* Set types
*
* @template T of object
* @param array<int|class-string<T>, T> $types
* @return $this
*/
public function setTypes(
array $types
): static {
$this->types = [];
$this->addTypes($types);
return $this;
}

/**
* Add types
*
* @template T of object
* @param array<int|class-string<T>, T> $types
* @return $this
*/
public function addTypes(
array $types
): static {
foreach ($types as $key => $type) {
if (is_int($key)) {
$key = get_class($type);
}

$this->types[$key] = $type;
}

return $this;
}

/**
* Has types
*/
public function hasTypes(): bool
{
return !empty($this->types);
}

/**
* Get types
*
* @return array<class-string<object>, object>
*/
public function getTypes(): array
{
return $this->types;
}


/**
* Add type
*
* @template T of object
* @param T $type
* @param class-string<T> $interface
*/
public function addType(
object $type,
?string $interface = null
): static {
if ($interface !== null) {
$this->types[$interface] = $type;
}

$this->types[get_class($type)] = $type;
return $this;
}

/**
* Has type
*
* @param class-string<object> $type
*/
public function hasType(
string $type
): bool {
return isset($this->types[$type]);
}

/**
* Get type
*
* @template T of object
* @param class-string<T> $type
* @return T|null
*/
public function getType(
string $type
): ?object {
/** @var T|null $output */
$output = $this->types[$type] ?? null;
return $output;
}




/**
* Invoke method
*
Expand Down Expand Up @@ -230,6 +340,16 @@ public function invoke(
}


// Type
if (
$typeName !== null &&
isset($this->types[$typeName])
) {
$args[$name] = $this->types[$typeName];
continue;
}


// Container value
if (
$type instanceof ReflectionNamedType &&
Expand Down

0 comments on commit edc6980

Please sign in to comment.