Skip to content

Commit

Permalink
Merge pull request #133 from moufmouf/missing_dependency_exception
Browse files Browse the repository at this point in the history
This PR fixes exception handling according to container-interop and incoming PSR-11 (Adds the Interop\Container\NotFoundException to the MoufInstanceNotFoundException) and does not throw it in case of nested missing dependencies.
  • Loading branch information
moufmouf committed Sep 27, 2016
2 parents 9f55705 + 565ebea commit f2b1973
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 29 deletions.
40 changes: 40 additions & 0 deletions src/Mouf/MissingDependencyException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/*
* This file is part of the Mouf core package.
*
* (c) 2012 David Negrier <david@mouf-php.com>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/
namespace Mouf;
use Interop\Container\Exception\ContainerException;

/**
* MissingDependencyException are thrown by the Mouf framework when the user
* request an instance that has a dependency on an instance that does not exist.
*
*/
class MissingDependencyException extends MoufException implements ContainerException {

/**
* The name of the instance that was not found.
*
* @var string
*/
private $instanceName;

public function __construct($msg, $code = null, $instanceName = null, \Exception $causeException = null) {
parent::__construct($msg, $code, $causeException);
$this->instanceName = $instanceName;
}

/**
* Returns the name of the instance that was not found.
*
* @return string
*/
public function getMissingInstanceName() {
return $this->instanceName;
}
}
18 changes: 18 additions & 0 deletions src/Mouf/MoufContainerException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
/*
* This file is part of the Mouf core package.
*
* (c) 2012 David Negrier <david@mouf-php.com>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/
namespace Mouf;

/**
* Mouf Container Exceptions are thrown by the Mouf framework if something goes wrong in the container.
*
*/
class MoufContainerException extends MoufException {

}
21 changes: 11 additions & 10 deletions src/Mouf/MoufInstanceNotFoundException.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
<?php
/*
* This file is part of the Mouf core package.
*
* (c) 2012 David Negrier <david@mouf-php.com>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/
/*
* This file is part of the Mouf core package.
*
* (c) 2012 David Negrier <david@mouf-php.com>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/
namespace Mouf;

use Interop\Container\Exception\NotFoundException;

/**
* MoufInstanceNotFoundException are thrown by the Mouf framework when the user
* request an instance that is not defined.
*
*/
class MoufInstanceNotFoundException extends MoufException {
class MoufInstanceNotFoundException extends MoufException implements NotFoundException {

/**
* The name of the instance that was not found.
Expand Down
38 changes: 19 additions & 19 deletions src/Mouf/MoufManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -668,11 +668,11 @@ private function instantiateComponent($instanceName) {
$closure = $closure->bindTo($object);
$object->$key = $closure($this->delegateLookupContainer);
} else {
throw new MoufException("Parse error in the callback of '$instanceName' property '$key': ".$closure);
throw new MoufContainerException("Parse error in the callback of '$instanceName' property '$key': ".$closure);
}
break;
default:
throw new MoufException("Invalid type '".$valueDef["type"]."' for object instance '$instanceName'.");
throw new MoufContainerException("Invalid type '".$valueDef["type"]."' for object instance '$instanceName'.");
}
}
}
Expand All @@ -699,12 +699,12 @@ private function instantiateComponent($instanceName) {
if ($closure instanceof \Closure) {
$closure = $closure->bindTo($object);
} else {
throw new MoufException("Parse error in the callback of '$instanceName' setter '$key': ".$closure);
throw new MoufContainerException("Parse error in the callback of '$instanceName' setter '$key': ".$closure);
}
$object->$key($closure($this->delegateLookupContainer));
break;
default:
throw new MoufException("Invalid type '".$valueDef["type"]."' for object instance '$instanceName'.");
throw new MoufContainerException("Invalid type '".$valueDef["type"]."' for object instance '$instanceName'.");
}
}
}
Expand Down Expand Up @@ -745,7 +745,7 @@ private function instantiateComponent($instanceName) {
}
}
} catch (MoufInstanceNotFoundException $e) {
throw new MoufInstanceNotFoundException("The object instance '".$instanceName."' could not be created because it depends on an object in error (".$e->getMissingInstanceName().")", 2, $instanceName, $e);
throw new MissingDependencyException("The object instance '".$instanceName."' could not be created because it depends on an object in error (".$e->getMissingInstanceName().")", 2, $instanceName, $e);
}
return $object;
}
Expand All @@ -761,7 +761,7 @@ private function instantiateComponent($instanceName) {
*/
public function setParameter($instanceName, $paramName, $paramValue, $type = "string", array $metadata = array()) {
if ($type != "string" && $type != "config" && $type != "request" && $type != "session" && $type != "php") {
throw new MoufException("Invalid type. Must be one of: string|config|request|session. Value passed: '".$type."'");
throw new MoufContainerException("Invalid type. Must be one of: string|config|request|session. Value passed: '".$type."'");
}

$this->declaredInstances[$instanceName]["fieldProperties"][$paramName]["value"] = $paramValue;
Expand All @@ -780,7 +780,7 @@ public function setParameter($instanceName, $paramName, $paramValue, $type = "st
*/
public function setParameterViaSetter($instanceName, $setterName, $paramValue, $type = "string", array $metadata = array()) {
if ($type != "string" && $type != "config" && $type != "request" && $type != "session" && $type != "php") {
throw new MoufException("Invalid type. Must be one of: string|config|request|session");
throw new MoufContainerException("Invalid type. Must be one of: string|config|request|session");
}

$this->declaredInstances[$instanceName]["setterProperties"][$setterName]["value"] = $paramValue;
Expand All @@ -800,7 +800,7 @@ public function setParameterViaSetter($instanceName, $setterName, $paramValue, $
*/
public function setParameterViaConstructor($instanceName, $index, $paramValue, $parameterType, $type = "string", array $metadata = array()) {
if ($type != "string" && $type != "config" && $type != "request" && $type != "session" && $type != "php") {
throw new MoufException("Invalid type. Must be one of: string|config|request|session");
throw new MoufContainerException("Invalid type. Must be one of: string|config|request|session");
}

$this->declaredInstances[$instanceName]['constructor'][$index]["value"] = $paramValue;
Expand Down Expand Up @@ -1200,7 +1200,7 @@ public function rewriteMouf() {
if ((file_exists(dirname(__FILE__)."/".$this->componentsFileName) && !is_writable(dirname(__FILE__)."/".$this->componentsFileName)) || (!file_exists(dirname(__FILE__)."/".$this->componentsFileName) && !is_writable(dirname(dirname(__FILE__)."/".$this->componentsFileName)))) {
$dirname = realpath(dirname(dirname(__FILE__)."/".$this->componentsFileName));
$filename = basename(dirname(__FILE__)."/".$this->componentsFileName);
throw new MoufException("Error, unable to write file ".$dirname."/".$filename);
throw new MoufContainerException("Error, unable to write file ".$dirname."/".$filename);
}

// Let's start by garbage collecting weak instances.
Expand Down Expand Up @@ -1370,7 +1370,7 @@ class ".$this->mainClassName." {
if (isset($classDesc['code'])) {
continue;
}
throw new MoufException("No class for instance '$name'");
throw new MoufContainerException("No class for instance '$name'");
}
if (isset($classDesc['anonymous']) && $classDesc['anonymous']) {
continue;
Expand Down Expand Up @@ -1598,17 +1598,17 @@ public function findInstanceName($instance) {
*/
public function duplicateInstance($srcInstanceName, $destInstanceName = null) {
if (!isset($this->declaredInstances[$srcInstanceName])) {
throw new MoufException("Error while duplicating instance: unable to find source instance ".$srcInstanceName);
throw new MoufContainerException("Error while duplicating instance: unable to find source instance ".$srcInstanceName);
}
if ($destInstanceName == null) {
if (!$this->isInstanceAnonymous($srcInstanceName)) {
throw new MoufException("Error while duplicating instance: you need to give a destination name.");
throw new MoufContainerException("Error while duplicating instance: you need to give a destination name.");
}
$destInstanceName = $this->getFreeAnonymousName();
}

if (isset($this->declaredInstances[$destInstanceName])) {
throw new MoufException("Error while duplicating instance: the dest instance already exists: ".$destInstanceName);
throw new MoufContainerException("Error while duplicating instance: the dest instance already exists: ".$destInstanceName);
}
$this->declaredInstances[$destInstanceName] = $this->declaredInstances[$srcInstanceName];

Expand Down Expand Up @@ -1939,7 +1939,7 @@ public function getInstanceDescriptor($name) {
$this->instanceDescriptors[$name] = new MoufInstanceDescriptor($this, $name);
return $this->instanceDescriptors[$name];
} else {
throw new MoufException("Instance '".$name."' does not exist.");
throw new MoufContainerException("Instance '".$name."' does not exist.");
}
}

Expand Down Expand Up @@ -2052,10 +2052,10 @@ public function createInstanceByCode() {
*/
public function setCode($instanceName, $code) {
if (!isset($this->declaredInstances[$instanceName])) {
throw new MoufException("Instance '$instanceName' does not exist.");
throw new MoufContainerException("Instance '$instanceName' does not exist.");
}
if (!isset($this->declaredInstances[$instanceName]["code"])) {
throw new MoufException("Instance '$instanceName' has not been created using `createInstanceByCode`. It cannot have a PHP code attached to it.");
throw new MoufContainerException("Instance '$instanceName' has not been created using `createInstanceByCode`. It cannot have a PHP code attached to it.");
}
$this->declaredInstances[$instanceName]["code"] = $code;
$this->findInstanceByCallbackType($instanceName);
Expand Down Expand Up @@ -2128,7 +2128,7 @@ public function setDelegateLookupContainer(ContainerInterface $delegateLookupCon

/**
* Check if there is no loop in constructor arguments
* @throws MoufException
* @throws MoufContainerException
*/
public function checkConstructorLoop() {
foreach ($this->declaredInstances as $instanceName => $descriptor) {
Expand All @@ -2142,13 +2142,13 @@ public function checkConstructorLoop() {
*
* @param string $instanceName
* @param array $path
* @throws MoufException
* @throws MoufContainerException
*/
private function walkConstructorLoop($instanceName, array $path) {
if(isset($path[$instanceName])) {
$instances = array_keys($path);
$instances = array_slice($instances, array_search($instanceName, $instances));
throw new MoufException('A loop was detected on constructor arguments '.implode(' -> ', $instances).' -> '.$instanceName);
throw new MoufContainerException('A loop was detected on constructor arguments '.implode(' -> ', $instances).' -> '.$instanceName);
}
$path[$instanceName] = true;
$descriptor = $this->declaredInstances[$instanceName];
Expand Down

0 comments on commit f2b1973

Please sign in to comment.