Skip to content
This repository has been archived by the owner on Aug 13, 2023. It is now read-only.

Commit

Permalink
di init
Browse files Browse the repository at this point in the history
  • Loading branch information
rez1dent3 committed Dec 23, 2016
1 parent 76ac6f5 commit b8ad574
Show file tree
Hide file tree
Showing 7 changed files with 550 additions and 4 deletions.
6 changes: 2 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
composer.phar
composer.lock
/vendor/

# Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
# composer.lock
.idea/
18 changes: 18 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "deimos/di",
"license": "MIT",
"authors": [
{
"name": "rez1dent3",
"email": "[email protected]"
}
],
"require": {
"php": "^5.5 || ^7.0"
},
"autoload": {
"psr-4": {
"": "src/"
}
}
}
85 changes: 85 additions & 0 deletions demo/math.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

include_once __DIR__ . '/../vendor/autoload.php';

class GCDMath
{

/**
* @param int $a
* @param int $b
*
* @return int
*/
public function call($a, $b)
{
if (!$a)
{
return $b;
}
if (!$b)
{
return $a;
}

return $this->call($a, $a % $b);
}

}

class LCMMath
{
/**
* @var GCDMath
*/
protected $gcdMath;

public function __construct(GCDMath $gcd)
{
$this->gcdMath = $gcd;
}

/**
* @param int $a
* @param int $b
*
* @return int
*/
public function call($a, $b)
{
return $a * ($b / $this->gcdMath->call($a, $b));
}
}

class Obj extends Deimos\DI\DI
{
protected function configure()
{
$this->value('a', 13);
$this->value('b', 16);

$this->group('math', function ()
{
$this->instance('gcdClass', GCDMath::class, []);
$this->instance('lcmClass', LCMMath::class, ['@math.gcdClass']);
});

$this->addBuildCallback('gcd', function ()
{
return $this->call('math.gcdClass.call', [$this->a(), $this->b()]);
});

$this->addBuildCallback('lcm', function ()
{
return $this->call('math.lcmClass.call', [$this->a(), $this->b()]);
});
}
}

$obj = new Obj();

var_dump($obj->get('math.lcmClass')->call($obj->a(), $obj->b()));
var_dump($obj->get('math.gcdClass')->call($obj->a(), $obj->b()));

var_dump($obj->lcm());
var_dump($obj->gcd());
192 changes: 192 additions & 0 deletions src/Deimos/DI/Container.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
<?php

namespace Deimos\DI;

abstract class Container
{

/**
* @var mixed[]
*/
protected $values = [];

/**
* @var callable[]
*/
protected $callbacks = [];

/**
* @var callable[]
*/
protected $buildCallbacks = [];

/**
* @param string $key
* @param mixed $value
*/
protected function addValue($key, $value)
{
$this->values[$key] = $value;
}

/**
* @param string $key
* @param callable $callback
*/
protected function addCallback($key, callable $callback)
{
$this->callbacks[$key] = $callback;
}

/**
* @param string $key
* @param callable $callback
*/
protected function addBuildCallback($key, callable $callback)
{
$this->buildCallbacks[$key] = $callback;
}

/**
* @param $name
*
* @return callable|mixed
*
* @throws \InvalidArgumentException
*/
protected function processGet($name)
{
return $this->getBySplitPath($this->splitPath($name));
}

/**
* @param $name
* @param $params
*
* @return callable|mixed
*
* @throws \InvalidArgumentException
*/
protected function processCall($name, $params)
{
return $this->getBySplitPath($this->splitPath($name), true, $params);
}

/**
* @param $name
*
* @return array
*/
protected function splitPath($name)
{
return explode('.', $name);
}

/**
* @param array $path
* @param bool $isCall
* @param array $params
*
* @return callable|mixed
*
* @throws \InvalidArgumentException
*/
protected function getBySplitPath(array $path, $isCall = false, array $params = [])
{
$value = array_shift($path);

if (empty($path))
{
return $this->getValue($value, $isCall, $params);
}

$value = $this->getValue($value);

if ($value instanceof self)
{
return $value->getBySplitPath($path, $isCall, $params);
}

$last = array_pop($path);
$this->steps($value, $path);

if ($isCall)
{
return call_user_func_array([$value, $last], $params);
}

return $value->$last();
}

/**
* @param $value
* @param array $path
*/
protected function steps(&$value, array $path)
{
foreach ($path as $step)
{
$value = $value->$step();
}
}

/**
* @param $name
*
* @return mixed
*/
protected function buildCallback($name)
{
$this->values[$name] = call_user_func($this->buildCallbacks[$name]);
unset($this->buildCallbacks[$name]);

return $this->values[$name];
}

/**
* @param string $name
* @param bool $isCallable
* @param null $callParams
*
* @return callable|mixed
*
* @throws \InvalidArgumentException
*/
protected function getValue($name, $isCallable = false, $callParams = null)
{
if (!array_key_exists($name, $this->values))
{
if (isset($this->buildCallbacks[$name]))
{
return $this->buildCallback($name);
}

if (isset($this->callbacks[$name]))
{
if ($isCallable)
{
return call_user_func_array($this->callbacks[$name], $callParams);
}

return $this->callbacks[$name];
}

$selfPath = $this->selfPath($name);

throw new \InvalidArgumentException("'$selfPath' is not defined");
}

return $this->values[$name];
}

/**
* @param string $name
*
* @return string
*/
protected function selfPath($name)
{
return $name;
}

}
Loading

0 comments on commit b8ad574

Please sign in to comment.