-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
\Phalcon\Di add alias? #13042
Comments
In the example I think i see the meaning: you want to add a definition with 2 names at once Imo this should extend the Phalcon/Di and override the setter and getter like
This would affect all setter and getter that are using the |
He just means to have option to add alias which will be alias of service added by |
Would be confusing to get aliases only on
So we would need an alias setter I think this should do it (its PHP. Would have to get that down to zep) class AliasException extends \Exception
{
}
class MyDi extends \Phalcon\Di
{
/**
* An array of key => value ($alias => $name).
*
* @var array
*/
protected $aliases = [];
/**
* Returns aliases optionally by given name.
*
* @param null|string $name
*
* @return array
*/
public function getAliases($name = null)
{
if ($name === null) {
return $this->aliases;
}
$keys = array_keys($this->aliases, (string)$name, true);
$aliases = [];
foreach ($keys as $key) {
$aliases[$key] = $this->aliases[$key];
}
return $aliases;
}
/**
* Sets one or more aliases to the given name.
*
* @param string $name
* @param string|array $aliases
*
* @return $this
* @throws AliasException
*/
public function setAlias($name, $aliases)
{
if (!is_string($name)) {
throw new \InvalidArgumentException("Parameter 'name' must be a string");
}
if (!is_array($aliases)) {
$aliases = [$aliases];
}
foreach ($aliases as $alias) {
if (!is_string($alias)) {
throw new \InvalidArgumentException("Parameter 'aliases' must contain strings");
}
if (parent::has($alias)) {
throw new AliasException("Alias '{$alias}' already in use by existing service.");
}
$this->aliases[$alias] = $name;
}
return $this;
}
/**
* {@inheritdoc}
*/
public function has($name)
{
$alias = $name;
if (isset($this->aliases[$alias])) {
if (parent::has($this->aliases[$alias])) {
return true;
}
}
return parent::has($name);
}
/**
* {@inheritdoc}
*/
public function get($name, $parameters = null)
{
$alias = $name;
if (isset($this->aliases[$alias])) {
try {
return parent::get($this->aliases[$alias], $parameters);
} catch (\Exception $e) {
// no action
}
}
return parent::get($name, $parameters);
}
/**
* {@inheritdoc}
*/
public function getShared($name, $parameters = null)
{
$alias = $name;
if (isset($this->aliases[$alias])) {
try {
return parent::getShared($this->aliases[$alias], $parameters);
} catch (\Exception $e) {
// no action
}
}
return parent::getShared($name, $parameters);
}
/**
* {@inheritdoc}
*/
public function getService($name)
{
$alias = $name;
if (isset($this->aliases[$alias])) {
try {
return parent::getService($this->aliases[$alias]);
} catch (\Exception $e) {
// no action
}
}
return parent::getService($name);
}
/**
* {@inheritdoc}
*/
public function getRaw($name)
{
$alias = $name;
if (isset($this->aliases[$alias])) {
try {
return parent::getRaw($this->aliases[$alias]);
} catch (\Exception $e) {
// no action
}
}
return parent::getRaw($name);
}
/**
* {@inheritdoc}
*/
public function remove($name)
{
$alias = $name;
$returnValue = parent::remove($name);
if (isset($this->aliases[$alias])) {
unset($this->aliases[$alias]);
}
return $returnValue;
}
/**
* {@inheritdoc}
*/
public function offsetExists($name)
{
return $this->has($name);
}
/**
* {@inheritdoc}
*/
public function offsetGet($name)
{
return $this->getShared($name);
}
/**
* {@inheritdoc}
*/
public function offsetUnset($name)
{
return $this->remove($name);
}
}
// define definition
$definition = function () {
return new \Phalcon\Http\Response();
};
// test with definition set
$di = new MyDi();
$di->set(\Phalcon\Http\ResponseInterface::class, $definition);
$di->setAlias(
\Phalcon\Http\ResponseInterface::class,
[
'response',
'http_response',
'http_response_interface'
]);
echo "Test not shared ...";
if ((new SimpleTests())->testNotShared($di, $definition)) {
echo " OK\r\n";
}
// test with definition set shared
$di = new MyDi();
$di->setShared(\Phalcon\Http\ResponseInterface::class, $definition);
$di->setAlias(
\Phalcon\Http\ResponseInterface::class,
[
'response',
'http_response',
'http_response_interface'
]);
echo "Test shared ...";
if ((new SimpleTests())->testShared($di, $definition)) {
echo " OK\r\n";
}
class SimpleTests
{
public function testNotShared(MyDi $di, $definition)
{
foreach (
[
\Phalcon\Http\ResponseInterface::class,
'response',
'http_response',
'http_response_interface',
] as $name
) {
$this->testHas($di, $name);
$this->testGet($di, $name);
$this->testGetShared($di, $name);
$this->testGetNewInstance($di, $name, true);
$this->testGetService($di, $name);
$this->testGetRaw($di, $name, $definition);
}
$this->testSetAliasAliasException($di);
$this->testSetAliasInvalidArgumentException($di);
$this->testSetAliasArrayInvalidArgumentException($di);
return true;
}
function testShared(MyDi $di, $definition)
{
foreach (
[
\Phalcon\Http\ResponseInterface::class,
'response',
'http_response',
'http_response_interface',
] as $name
) {
$this->testHas($di, $name);
$this->testGet($di, $name);
$this->testGetShared($di, $name);
$this->testGetNewInstance($di, $name, false);
$this->testGetService($di, $name);
$this->testGetRaw($di, $name, $definition);
}
$this->testSetAliasAliasException($di);
$this->testSetAliasInvalidArgumentException($di);
$this->testSetAliasArrayInvalidArgumentException($di);
return true;
}
protected function testHas(MyDi $di, $name)
{
if (!$di->has($name)) {
throw new \Exception("Error has('{$name}'): failure.");
}
return $this;
}
protected function testGet(MyDi $di, $name)
{
$response = $di->get($name);
if (!$response instanceof \Phalcon\Http\Response) {
throw new \Exception("Error get('{$name}'): is not instance of \Phalcon\Http\Response.");
}
return $this;
}
protected function testGetShared(MyDi $di, $name)
{
$response = $di->get($name);
$response->test = true;
$shared = $di->getShared($name);
if (!$shared instanceof \Phalcon\Http\Response) {
throw new \Exception("Error getShared('{$name}'): is not instance of \Phalcon\Http\Response.");
}
if (!isset($response->test) or $response->test !== true) {
throw new \Exception("Error getShared('{$name}'): is not shared instance.");
}
return $this;
}
protected function testGetNewInstance(MyDi $di, $name, $expectNewInstance)
{
$response = $di->get($name);
$response->test = true;
$new = $di->get($name);
if ($expectNewInstance) {
if (isset($new->test)) {
throw new \Exception("Error get('{$name}'): is not new instance.");
}
} else {
if (!isset($new->test) or $new->test !== true) {
throw new \Exception("Error get('{$name}'): is not shared instance.");
}
}
$response = $di->getShared($name);
$response->test = true;
$new = $di->get($name);
if ($expectNewInstance) {
if (isset($new->test)) {
throw new \Exception("Error get('{$name}'): is not new instance.");
}
} else {
if (!isset($new->test) or $new->test !== true) {
throw new \Exception("Error get('{$name}'): is not shared instance.");
}
}
}
protected function testGetService(MyDi $di, $name)
{
$serviceName = $di->getService($name)->getName();
if ($serviceName !== \Phalcon\Http\ResponseInterface::class) {
throw new \Exception("Error getService('{$name}')->getName(): is not expected name.");
}
return $this;
}
protected function testGetRaw(MyDi $di, $name, $definition)
{
$raw = $di->getRaw($name);
if ($raw !== $definition) {
throw new \Exception("Error getRaw('{$name}'): is not expected definition.");
}
return $this;
}
protected function testSetAliasInvalidArgumentException(MyDi $di)
{
$exception = null;
try {
$di->setAlias(
\Phalcon\Http\ResponseInterface::class,
1
);
} catch (InvalidArgumentException $e) {
$exception = $e;
} catch (\Exception $e) {
$exception = $e;
}
if (!$exception instanceof InvalidArgumentException) {
throw new \Exception("Error setAlias(\$name, \$alias): is not expected InvalidArgumentException.");
}
return $this;
}
protected function testSetAliasArrayInvalidArgumentException(MyDi $di)
{
$exception = null;
try {
$di->setAlias(
\Phalcon\Http\ResponseInterface::class,
['foo', 2]
);
} catch (InvalidArgumentException $e) {
$exception = $e;
} catch (\Exception $e) {
$exception = $e;
}
if (!$exception instanceof InvalidArgumentException) {
throw new \Exception("Error setAlias(\$name, \$alias): is not expected InvalidArgumentException.");
}
return $this;
}
protected function testSetAliasAliasException(MyDi $di)
{
$exception = null;
try {
$di->setAlias(
\Phalcon\Http\ResponseInterface::class,
\Phalcon\Http\ResponseInterface::class
);
} catch (AliasException $e) {
$exception = $e;
} catch (\Exception $e) {
$exception = $e;
}
if (!$exception instanceof AliasException) {
throw new \Exception("Error setAlias(\$name, \$alias): is not expected AliasException.");
}
return $this;
}
} |
Why can't you just do PR with zephir code? |
Because im not that much into zep code and it would take me much longer than this php script. |
Closing in favor of #13855. Will revisit if the community votes for it, or in later versions. |
The text was updated successfully, but these errors were encountered: