Skip to content

Commit

Permalink
main
Browse files Browse the repository at this point in the history
  • Loading branch information
6562680 committed May 27, 2024
1 parent 5e26a91 commit 4251d37
Show file tree
Hide file tree
Showing 11 changed files with 243 additions and 188 deletions.
5 changes: 1 addition & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,5 @@
"optimize-autoloader": true,

"sort-packages": false
},
"require-dev": {
"symfony/var-dumper": "^5.4"
}
}
}
30 changes: 11 additions & 19 deletions helpers/di.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/**
* @return DiInterface
*/
function _di(DiInterface $di = null)
function _di(DiInterface $di = null) // : DiInterface
{
static $instance;

Expand All @@ -29,53 +29,45 @@ function _di(DiInterface $di = null)
/**
* @param string $id
*/
function _di_has_bound($id, Id &$result = null) : bool
function _di_has($id, Id &$result = null) : bool
{
return _di()->has($id, $result);
}

/**
* @param string $id
*/
function _di_has_item($id, Id &$result = null) : bool
{
return _di()->hasItem($id, $result);
}


function _di_bind($id, $mixed = null, bool $isSingleton = null)
function _di_bind($id, $mixed = null, bool $isSingleton = null) // : static
{
_di()->bind($id, $mixed, $isSingleton);
}

function _di_bind_singleton($id, $mixed = null)
function _di_bind_singleton($id, $mixed = null) // : static
{
_di()->bindSingleton($id, $mixed);
}


function _di_bind_alias($id, $aliasId, bool $isSingleton = null)
function _di_bind_alias($id, $aliasId, bool $isSingleton = null) // : static
{
return _di()->bindAlias($id, $aliasId, $isSingleton);
}

/**
* @param class-string $structId
* @param class-string $classId
*/
function _di_bind_struct($id, $structId, bool $isSingleton = null)
function _di_bind_class($id, $classId, bool $isSingleton = null) // : static
{
return _di()->bindStruct($id, $structId, $isSingleton);
return _di()->bindClass($id, $classId, $isSingleton);
}

/**
* @param callable $fnFactory
*/
function _di_bind_factory($id, $fnFactory, bool $isSingleton = null)
function _di_bind_factory($id, $fnFactory, bool $isSingleton = null) // : static
{
return _di()->bindFactory($id, $fnFactory, $isSingleton);
}

function _di_bind_instance($id, object $instance, bool $isSingleton = null)
function _di_bind_instance($id, object $instance, bool $isSingleton = null) // : static
{
return _di()->bindInstance($id, $instance, $isSingleton);
}
Expand All @@ -84,7 +76,7 @@ function _di_bind_instance($id, object $instance, bool $isSingleton = null)
/**
* @param callable $fnExtend
*/
function _di_extend($id, $fnExtend)
function _di_extend($id, $fnExtend) // : static
{
return _di()->extend($id, $fnExtend);
}
Expand Down
6 changes: 6 additions & 0 deletions src/Demo/MyClassFive.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ class MyClassFive
public $four;


public function __construct()
{
// > gzhegow, пустой конструктор, чтобы протестировать, что для пустых функций аргументы равны пустому массиву
}


public function __autowire(MyClassFour $four)
{
$this->four = $four;
Expand Down
12 changes: 10 additions & 2 deletions src/Demo/MyClassTwo.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,20 @@ class MyClassTwo implements MyClassTwoInterface
protected $hello;


public function __construct(MyClassOneInterface $a, string $hello = null)
// public function __construct(MyClassOneInterface $a, string $hello = null)
// {
// // > gzhegow, long init example
// sleep(3);
//
// $this->a = $a;
// $this->hello = $hello;
// }

public function __construct(string $hello = null)
{
// > gzhegow, long init example
sleep(3);

$this->a = $a;
$this->hello = $hello;
}

Expand Down
22 changes: 11 additions & 11 deletions src/Di.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public function bind($id, $mixed = null, bool $isSingleton = null) // : static

$id = Id::from($id);

$this->injector->bindItem($id, $mixed, $isSingleton);
$this->injector->bindItemAuto($id, $mixed, $isSingleton);

return $this;
}
Expand All @@ -152,16 +152,16 @@ public function bindAlias($id, $aliasId, bool $isSingleton = null) // : static
}

/**
* @param class-string $structId
* @param class-string $classId
*/
public function bindStruct($id, $structId, bool $isSingleton = null) // : static
public function bindClass($id, $classId, bool $isSingleton = null) // : static
{
$isSingleton = $isSingleton ?? false;

$id = Id::from($id);
$structId = Id::from($structId);
$classId = Id::from($classId);

$this->injector->bindItemStruct($id, $structId, $isSingleton);
$this->injector->bindItemClass($id, $classId, $isSingleton);

return $this;
}
Expand Down Expand Up @@ -254,15 +254,15 @@ public function get($id, string $contractT = null, bool $forceInstanceOf = null,
*
* @return T
*/
public function take($id, array $parametersWhenNew = null, string $contractT = null, bool $forceInstanceOf = null) // : object
public function make($id, array $parameters = null, string $contractT = null, bool $forceInstanceOf = null) // : object
{
$parametersWhenNew = $parametersWhenNew ?? [];
$parameters = $parameters ?? [];
$contractT = $contractT ?? '';
$forceInstanceOf = $forceInstanceOf ?? false;

$id = Id::from($id);

$instance = $this->injector->takeItem($id, $parametersWhenNew, $contractT, $forceInstanceOf);
$instance = $this->injector->makeItem($id, $parameters, $contractT, $forceInstanceOf);

return $instance;
}
Expand All @@ -274,15 +274,15 @@ public function take($id, array $parametersWhenNew = null, string $contractT = n
*
* @return T
*/
public function make($id, array $parameters = null, string $contractT = null, bool $forceInstanceOf = null) // : object
public function take($id, array $parametersWhenNew = null, string $contractT = null, bool $forceInstanceOf = null) // : object
{
$parameters = $parameters ?? [];
$parametersWhenNew = $parametersWhenNew ?? [];
$contractT = $contractT ?? '';
$forceInstanceOf = $forceInstanceOf ?? false;

$id = Id::from($id);

$instance = $this->injector->makeItem($id, $parameters, $contractT, $forceInstanceOf);
$instance = $this->injector->takeItem($id, $parametersWhenNew, $contractT, $forceInstanceOf);

return $instance;
}
Expand Down
8 changes: 4 additions & 4 deletions src/DiInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ public function bindSingleton($id, $mixed = null);
public function bindAlias($id, $aliasId, bool $isSingleton = null);

/**
* @param class-string $structId
* @param class-string $classId
*/
public function bindStruct($id, $structId, bool $isSingleton = null);
public function bindClass($id, $classId, bool $isSingleton = null);

/**
* @param callable $fnFactory
Expand Down Expand Up @@ -100,7 +100,7 @@ public function get($id, string $contractT = null, bool $forceInstanceOf = null,
*
* @return T
*/
public function take($id, array $parametersWhenNew = null, string $contractT = null, bool $forceInstanceOf = null);
public function make($id, array $parameters = null, string $contractT = null, bool $forceInstanceOf = null);

/**
* @template-covariant T
Expand All @@ -109,7 +109,7 @@ public function take($id, array $parametersWhenNew = null, string $contractT = n
*
* @return T
*/
public function make($id, array $parameters = null, string $contractT = null, bool $forceInstanceOf = null);
public function take($id, array $parametersWhenNew = null, string $contractT = null, bool $forceInstanceOf = null);


/**
Expand Down
Loading

0 comments on commit 4251d37

Please sign in to comment.