Skip to content
Permalink
Browse files

update: No longer throwing internal container exceptions

  • Loading branch information...
inhere committed Aug 2, 2019
1 parent 4ba117e commit 1e74893bf6e05fe954334e527e8b76eca2cef6e9
@@ -5,7 +5,6 @@
use ReflectionException;
use Swoft\Annotation\Exception\AnnotationException;
use Swoft\Bean\Contract\HandlerInterface;
use Swoft\Bean\Exception\ContainerException;
use Swoft\Stdlib\Reflections;
/**
@@ -19,8 +18,6 @@ final class BeanFactory
* Init
*
* @return void
* @throws ContainerException
* @throws ReflectionException
* @throws AnnotationException
*/
public static function init(): void
@@ -54,8 +51,6 @@ public static function getContainer(): Container
* @param string $name Bean name Or alias Or class name
*
* @return object|mixed
* @throws ContainerException
* @throws ReflectionException
*/
public static function getBean(string $name)
{
@@ -68,8 +63,6 @@ public static function getBean(string $name)
* @param string $className
*
* @return array
* @throws ContainerException
* @throws ReflectionException
*/
public static function getBeans(string $className): array
{
@@ -82,7 +75,6 @@ public static function getBeans(string $className): array
* @param string $name
*
* @return mixed
* @throws ContainerException
*/
public static function getSingleton(string $name)
{
@@ -129,8 +121,6 @@ public static function isSingleton(string $name): bool
* ]);
*
* @return object
* @throws ContainerException
* @throws ReflectionException
*/
public static function createBean(string $name, array $definition = [])
{
@@ -147,9 +137,7 @@ public static function createBean(string $name, array $definition = [])
* @param string $name
* @param string $id
*
* @return object
* @throws ContainerException
* @throws ReflectionException
* @return object|mixed
*/
public static function getRequestBean(string $name, string $id)
{
@@ -162,9 +150,7 @@ public static function getRequestBean(string $name, string $id)
* @param string $name
* @param string $sid
*
* @return object
* @throws ContainerException
* @throws ReflectionException
* @return object|mixed
*/
public static function getSessionBean(string $name, string $sid)
{
@@ -3,17 +3,9 @@
namespace Swoft\Bean;
use function array_keys;
use function class_exists;
use function count;
use function end;
use function is_array;
use function is_string;
use function method_exists;
use InvalidArgumentException;
use ReflectionClass;
use ReflectionException;
use function sprintf;
use function strpos;
use Swoft\Annotation\Exception\AnnotationException;
use Swoft\Bean\Annotation\Mapping\Bean;
use Swoft\Bean\Contract\ContainerInterface;
@@ -24,10 +16,18 @@
use Swoft\Bean\Definition\Parser\AnnotationObjParser;
use Swoft\Bean\Definition\Parser\DefinitionObjParser;
use Swoft\Bean\Definition\PropertyInjection;
use Swoft\Bean\Exception\ContainerException;
use Swoft\Stdlib\Helper\ArrayHelper;
use Swoft\Stdlib\Helper\ObjectHelper;
use Swoft\Stdlib\Reflections;
use function array_keys;
use function class_exists;
use function count;
use function end;
use function is_array;
use function is_string;
use function method_exists;
use function sprintf;
use function strpos;
use function ucfirst;
/**
@@ -292,8 +292,6 @@ public static function getInstance(): Container
/**
* Init
*
* @throws ContainerException
* @throws ReflectionException
* @throws AnnotationException
*/
public function init(): void
@@ -323,8 +321,6 @@ public function initializeRequest(int $rid): void
* @param string $id Usually is coroutine ID
*
* @return object
* @throws ContainerException
* @throws ReflectionException
*/
public function getRequest(string $name, string $id)
{
@@ -344,7 +340,7 @@ public function getRequest(string $name, string $id)
}
if (!isset($this->requestDefinitions[$name])) {
throw new ContainerException(sprintf('Request bean(%s) is not defined', $name));
throw new InvalidArgumentException(sprintf('Request bean(%s) is not defined', $name));
}
return $this->newBean($name, $id);
@@ -357,8 +353,7 @@ public function getRequest(string $name, string $id)
* @param string $sid
*
* @return object
* @throws ContainerException
* @throws ReflectionException
* @throws InvalidArgumentException
*/
public function getSession(string $name, string $sid)
{
@@ -378,7 +373,7 @@ public function getSession(string $name, string $sid)
}
if (!isset($this->sessionDefinitions[$name])) {
throw new ContainerException(sprintf('Session bean(%s) is not defined', $name));
throw new InvalidArgumentException(sprintf('Session bean(%s) is not defined', $name));
}
return $this->newBean($name, $sid);
@@ -392,8 +387,7 @@ public function getSession(string $name, string $sid)
* When class name will return all of instance for class name
*
* @return object
* @throws ContainerException
* @throws ReflectionException
* @throws InvalidArgumentException
*/
public function get($id)
{
@@ -422,7 +416,7 @@ public function get($id)
// Not defined
if (!isset($this->objectDefinitions[$id])) {
throw new ContainerException(sprintf('The bean of %s is not defined', $id));
throw new InvalidArgumentException(sprintf('The bean of %s is not defined', $id));
}
/* @var ObjectDefinition $objectDefinition */
@@ -438,8 +432,6 @@ public function get($id)
* @param string $className
*
* @return array
* @throws ContainerException
* @throws ReflectionException
*/
public function gets(string $className): array
{
@@ -463,7 +455,6 @@ public function gets(string $className): array
* @param string $name
*
* @return object|mixed
* @throws ContainerException
*/
public function getSingleton(string $name)
{
@@ -482,7 +473,7 @@ public function getSingleton(string $name)
return $this->singletonPool[$name];
}
throw new ContainerException(sprintf('The singleton bean "%s" is not defined', $name));
throw new InvalidArgumentException(sprintf('The singleton bean "%s" is not defined', $name));
}
/**
@@ -492,8 +483,7 @@ public function getSingleton(string $name)
* @param array $definition
*
* @return object
* @throws ContainerException
* @throws ReflectionException
* @throws InvalidArgumentException
* @example
* [
* 'class' => 'className',
@@ -516,7 +506,7 @@ public function getSingleton(string $name)
public function create(string $name, array $definition = [])
{
if ($this->has($name)) {
throw new ContainerException('Create ' . $name . ' bean by definition is exist!');
throw new InvalidArgumentException('Create ' . $name . ' bean by definition is exist!');
}
// Create bean only by class name
@@ -638,7 +628,6 @@ public function addParsers(array $annotationParsers): void
/**
* Parse annotations
*
* @throws ContainerException
* @throws AnnotationException
*/
private function parseAnnotations(): void
@@ -653,8 +642,6 @@ private function parseAnnotations(): void
/**
* Parse definitions
*
* @throws ContainerException
*/
private function parseDefinitions(): void
{
@@ -764,8 +751,7 @@ public function getSessionDefinitions(): array
/**
* Initialize beans
*
* @throws ContainerException
* @throws ReflectionException
* @throws InvalidArgumentException
*/
private function initializeBeans(): void
{
@@ -795,7 +781,7 @@ private function initializeBeans(): void
* @param string $beanName
*
* @return ObjectDefinition
* @throws ContainerException
* @throws InvalidArgumentException
*/
private function getNewObjectDefinition(string $beanName): ObjectDefinition
{
@@ -821,7 +807,7 @@ private function getNewObjectDefinition(string $beanName): ObjectDefinition
return $this->getNewObjectDefinition($this->aliases[$beanName]);
}
throw new ContainerException('Bean name of ' . $beanName . ' is not defined!');
throw new InvalidArgumentException('Bean name of ' . $beanName . ' is not defined!');
}
/**
@@ -861,8 +847,6 @@ private function setNewBean(string $beanName, string $scope, $object, string $id
* @param string $id
*
* @return object
* @throws ReflectionException
* @throws ContainerException
*/
private function newBean(string $beanName, string $id = '')
{
@@ -879,6 +863,7 @@ private function newBean(string $beanName, string $id = '')
$className = $objectDefinition->getClassName();
// Cache reflection class info
/** @noinspection PhpUnhandledExceptionInspection */
Reflections::cache($className);
// Before initialize bean
@@ -897,10 +882,12 @@ private function newBean(string $beanName, string $id = '')
$className = $this->handler->classProxy($className);
}
/** @noinspection PhpUnhandledExceptionInspection */
$reflectionClass = new ReflectionClass($className);
$reflectObject = $this->newInstance($reflectionClass, $constructArgs);
// Inject properties values
/** @noinspection PhpUnhandledExceptionInspection */
$this->newProperty($reflectObject, $reflectionClass, $propertyInjects, $id);
// Alias
@@ -923,14 +910,13 @@ private function newBean(string $beanName, string $id = '')
* @param string $id
*
* @return array
* @throws ContainerException
* @throws ReflectionException
* @throws InvalidArgumentException
*/
private function getConstructParams(MethodInjection $methodInjection, string $id = ''): array
{
$methodName = $methodInjection->getMethodName();
if ($methodName !== '__construct') {
throw new ContainerException('ConstructInjection method must be `__construct`');
throw new InvalidArgumentException('ConstructInjection method must be `__construct`');
}
$argInjects = $methodInjection->getParameters();
@@ -966,18 +952,17 @@ private function getConstructParams(MethodInjection $methodInjection, string $id
* @param array $args
*
* @return object
* @throws ContainerException
* @throws ReflectionException
*/
private function newInstance(ReflectionClass $reflectionClass, array $args)
{
if (empty($args) || !$reflectionClass->hasMethod('__construct')) {
return $reflectionClass->newInstance();
}
/** @noinspection PhpUnhandledExceptionInspection */
$reflectMethod = $reflectionClass->getMethod('__construct');
if ($reflectMethod->isPrivate() || $reflectMethod->isProtected()) {
throw new ContainerException('Construct function for bean must be public!');
throw new InvalidArgumentException('Construct function for bean must be public!');
}
return $reflectionClass->newInstanceArgs($args);
@@ -992,7 +977,6 @@ private function newInstance(ReflectionClass $reflectionClass, array $args)
* @param string $id
*
* @return void
* @throws ContainerException
* @throws ReflectionException
*/
private function newProperty(
@@ -1014,10 +998,11 @@ private function newProperty(
continue;
}
/** @noinspection PhpUnhandledExceptionInspection */
$reflectProperty = $reflectionClass->getProperty($propertyName);
if ($reflectProperty->isStatic()) {
throw new ContainerException(sprintf('Property %s for bean can not be `static` ', $propertyName));
throw new InvalidArgumentException(sprintf('Property %s for bean can not be `static` ', $propertyName));
}
// Parse property value
@@ -1089,8 +1074,7 @@ private function beforeInit(string $beanName, string $className, ObjectDefinitio
* @param string $id
*
* @return array
* @throws ContainerException
* @throws ReflectionException
* @throws InvalidArgumentException
*/
private function newPropertyArray(array $propertyValue, string $id = ''): array
{
@@ -1111,8 +1095,7 @@ private function newPropertyArray(array $propertyValue, string $id = ''): array
* @param string $id
*
* @return mixed
* @throws ContainerException
* @throws ReflectionException
* @throws InvalidArgumentException
*/
private function getRefValue($value, string $id = '')
{

0 comments on commit 1e74893

Please sign in to comment.
You can’t perform that action at this time.