Skip to content

Commit

Permalink
Merge pull request #280 from wagnert/master
Browse files Browse the repository at this point in the history
Performance optimizations by refactoring DI provider + switch to latests...
  • Loading branch information
wagnert committed Dec 3, 2014
2 parents 4370e2d + 6ec5e86 commit de3ef3d
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 58 deletions.
25 changes: 3 additions & 22 deletions src/AppserverIo/Appserver/AspectContainer/AspectManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,28 +185,9 @@ public function injectWebappPath($webappPath)
*
* @return \AppserverIo\Lang\Reflection\ReflectionClass The reflection instance
*/
public function newReflectionClass($className)
public function getReflectionClass($className)
{
// initialize the array with the annotations we want to ignore
$annotationsToIgnore = array(
'author',
'package',
'license',
'copyright',
'param',
'return',
'throws',
'see',
'link'
);

// initialize the array with the aliases for the aspect annotation
$annotationAliases = array(
Aspect::ANNOTATION => Aspect::__getClass()
);

// return the reflection class instance
return new ReflectionClass($className, $annotationsToIgnore, $annotationAliases);
return $this->getApplication()->search('ProviderInterface')->getReflectionClass($className);
}

/**
Expand Down Expand Up @@ -244,7 +225,7 @@ protected function registerAspects(ApplicationInterface $application)
$className = substr($pregResult, 0, -4);

// we need a reflection class to read the annotations
$reflectionClass = $this->newReflectionClass($className);
$reflectionClass = $this->getReflectionClass($className);

// if we found an aspect we have to register it using our aspect register class
if ($reflectionClass->hasAnnotation(Aspect::ANNOTATION)) {
Expand Down
8 changes: 4 additions & 4 deletions src/AppserverIo/Appserver/Core/DgClassLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,9 @@ public function loadClass($className)
{

// Might the class be a omitted one? If so we can require the original.
if ($this->getConfig()->hasValue('autoloader/omit')) {
if ($this->config->hasValue('autoloader/omit')) {

$omittedNamespaces = $this->getConfig()->getValue('autoloader/omit');
$omittedNamespaces = $this->config->getValue('autoloader/omit');
foreach ($omittedNamespaces as $omitted) {

// If our class name begins with the omitted part e.g. it's namespace
Expand All @@ -287,9 +287,9 @@ public function loadClass($className)
}

// Do we have the file in our cache dir? If we are in development mode we have to ignore this.
if ($this->getConfig()->getValue('environment') !== 'development') {
if ($this->config->getValue('environment') !== 'development') {

$cachePath = $this->getConfig()->getValue('cache/dir') . DIRECTORY_SEPARATOR . str_replace('\\', '_', $className) . '.php';
$cachePath = $this->config->getValue('cache/dir') . DIRECTORY_SEPARATOR . str_replace('\\', '_', $className) . '.php';

if (is_readable($cachePath)) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,12 @@ interface ProviderInterface extends ManagerInterface
/**
* Injects the dependencies of the passed instance.
*
* @param object $instance The instance to inject the dependencies for
* @param \AppserverIo\Lang\Reflection\ClassInterface|null $reflectionClass The reflection class for the passed instance
* @param string|null $sessionId The session-ID, necessary to inject stateful session beans (SFBs)
* @param object $instance The instance to inject the dependencies for
* @param string|null $sessionId The session-ID, necessary to inject stateful session beans (SFBs)
*
* @return void
*/
public function injectDependencies($instance, ClassInterface $reflectionClass = null, $sessionId = null);
public function injectDependencies($instance, $sessionId = null);

/**
* Returns a new instance of the passed class name.
Expand Down Expand Up @@ -113,11 +112,32 @@ public function getLookupName(AnnotationInterface $annotation);
public function resolveAlias($lookupName);

/**
* Returns a reflection class intance for the passed class name.
* Returns a new reflection class intance for the passed class name.
*
* @param string $className The class name to return the reflection instance for
* @param string $className The class name to return the reflection class instance for
*
* @return \AppserverIo\Lang\Reflection\ReflectionClass The reflection instance
*/
public function newReflectionClass($className);

/**
* Returns a reflection class intance for the passed class name.
*
* @param string $className The class name to return the reflection class instance for
*
* @return \AppserverIo\Lang\Reflection\ReflectionClass The reflection instance
* @see \DependencyInjectionContainer\Interfaces\ProviderInterface::getReflectionClass()
*/
public function getReflectionClass($className);

/**
* Returns a reflection class intance for the passed class name.
*
* @param object $instance The instance to return the reflection class instance for
*
* @return \AppserverIo\Lang\Reflection\ReflectionClass The reflection instance
* @see \DependencyInjectionContainer\Interfaces\ProviderInterface::newReflectionClass()
* @see \DependencyInjectionContainer\Interfaces\ProviderInterface::getReflectionClass()
*/
public function getReflectionClassForObject($instance);
}
73 changes: 54 additions & 19 deletions src/AppserverIo/Appserver/DependencyInjectionContainer/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,26 +255,63 @@ public function newReflectionClass($className)
}

/**
* Injects the dependencies of the passed instance.
* Returns a new reflection class intance for the passed class name.
*
* @param string $className The class name to return the reflection class instance for
*
* @return \AppserverIo\Lang\Reflection\ReflectionClass The reflection instance
*/
public function getReflectionClass($className)
{

// check if we've already initialized the reflection class
if (isset($this->reflectionClasses[$className]) === false) {
$this->reflectionClasses[$className] = $this->newReflectionClass($className);
}

// return the reflection class instance
return $this->reflectionClasses[$className];
}

/**
* Returns a reflection class intance for the passed class name.
*
* @param object $instance The instance to return the reflection class instance for
*
* @return \AppserverIo\Lang\Reflection\ReflectionClass The reflection instance
* @see \DependencyInjectionContainer\Interfaces\ProviderInterface::newReflectionClass()
* @see \DependencyInjectionContainer\Interfaces\ProviderInterface::getReflectionClass()
*/
public function getReflectionClassForObject($instance)
{
return $this->getReflectionClass(get_class($instance));
}

/**
* Adds the passe reflection class instance to the DI provider.
*
* @param object $instance The instance to inject the dependencies for
* @param \AppserverIo\Lang\Reflection\ClassInterface|null $reflectionClass The reflection class for the passed instance
* @param string|null $sessionId The session-ID, necessary to inject stateful session beans (SFBs)
* @param \AppserverIo\Lang\Reflection\ClassInterface $reflectionClass The reflection class instance to add
*
* @return void
*/
public function injectDependencies($instance, ClassInterface $reflectionClass = null, $sessionId = null)
public function setReflectionClass(ClassInterface $reflectionClass)
{
$this->reflectionClasses[$reflectionClass->getName()] = $reflectionClass;
}

// the class name we want to inject the dependencies
$className = get_class($instance);
/**
* Injects the dependencies of the passed instance.
*
* @param object $instance The instance to inject the dependencies for
* @param string|null $sessionId The session-ID, necessary to inject stateful session beans (SFBs)
*
* @return void
*/
public function injectDependencies($instance, $sessionId = null)
{

// check if a reflection class instance has been passed or is already available
if (isset($this->reflectionClasses[$className])) {
$reflectionClass = $this->reflectionClasses[$className];
} elseif (isset($this->reflectionClasses[$className]) === false && $reflectionClass == null) {
$reflectionClass = $this->newReflectionClass($instance);
}
$reflectionClass = $this->getReflectionClassForObject($instance);

// we've to check for DI property annotations
foreach ($reflectionClass->getProperties() as $reflectionProperty) {
Expand Down Expand Up @@ -332,10 +369,8 @@ public function injectDependencies($instance, ClassInterface $reflectionClass =
}
}

// add the reflection class name to the array
if (isset($this->reflectionClasses[$className]) === false) {
$this->reflectionClasses[$className] = $reflectionClass;
}
// add the reflection class name to the array (because it could have been updated)
$this->setReflectionClass($reflectionClass);
}

/**
Expand All @@ -350,8 +385,8 @@ public function injectDependencies($instance, ClassInterface $reflectionClass =
public function newInstance($className, $sessionId = null, array $args = array())
{

// create and return a new instance
$reflectionClass = $this->newReflectionClass($className);
// load/create and return a new instance
$reflectionClass = $this->getReflectionClass($className);

// check if we've a constructor
if ($reflectionClass->hasMethod('__construct')) {
Expand All @@ -361,7 +396,7 @@ public function newInstance($className, $sessionId = null, array $args = array()
}

// inject the dependencies
$this->injectDependencies($instance, $reflectionClass, $sessionId);
$this->injectDependencies($instance, $sessionId);

// return the instance here
return $instance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function lookup(BeanManager $beanManager, $className, $sessionId = null,
{

// get the reflection class for the passed class name
$reflectionClass = $beanManager->newReflectionClass($className);
$reflectionClass = $beanManager->getReflectionClass($className);

// @Stateful
if ($reflectionClass->hasAnnotation(Stateful::ANNOTATION)) {
Expand Down
37 changes: 32 additions & 5 deletions src/AppserverIo/Appserver/PersistenceContainer/BeanManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ protected function registerBeans(ApplicationInterface $application)
$className = substr($pregResult, 0, -4);

// we need a reflection class to read the annotations
$reflectionClass = $this->newReflectionClass($className);
$reflectionClass = $this->getReflectionClass($className);

// register the bean instance
$this->registerBean($reflectionClass);
Expand Down Expand Up @@ -512,7 +512,7 @@ public function destroyBeanInstance($instance)
{

// we need a reflection object
$reflectionObject = new ReflectionObject($instance);
$reflectionObject = $this->getReflectionClassForObject($instance);

// we've to check for a @PreDestroy annotation
foreach ($reflectionObject->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflectionMethod) {
Expand All @@ -537,7 +537,7 @@ public function attach($instance, $sessionId = null)
{

// we need a reflection object to read the annotations
$reflectionObject = new ReflectionObject($instance);
$reflectionObject = $this->getReflectionClassForObject($instance);

// @Singleton
if ($reflectionObject->hasAnnotation(Singleton::ANNOTATION)) {
Expand Down Expand Up @@ -614,9 +614,9 @@ public function getAttribute($key)
}

/**
* Returns a reflection class intance for the passed class name.
* Returns a new reflection class intance for the passed class name.
*
* @param string $className The class name to return the reflection instance for
* @param string $className The class name to return the reflection class instance for
*
* @return \AppserverIo\Lang\Reflection\ReflectionClass The reflection instance
*/
Expand All @@ -625,6 +625,33 @@ public function newReflectionClass($className)
return $this->getApplication()->search('ProviderInterface')->newReflectionClass($className);
}

/**
* Returns a reflection class intance for the passed class name.
*
* @param string $className The class name to return the reflection class instance for
*
* @return \AppserverIo\Lang\Reflection\ReflectionClass The reflection instance
* @see \DependencyInjectionContainer\Interfaces\ProviderInterface::getReflectionClass()
*/
public function getReflectionClass($className)
{
return $this->getApplication()->search('ProviderInterface')->getReflectionClass($className);
}

/**
* Returns a reflection class intance for the passed class name.
*
* @param object $instance The instance to return the reflection class instance for
*
* @return \AppserverIo\Lang\Reflection\ReflectionClass The reflection instance
* @see \DependencyInjectionContainer\Interfaces\ProviderInterface::newReflectionClass()
* @see \DependencyInjectionContainer\Interfaces\ProviderInterface::getReflectionClass()
*/
public function getReflectionClassForObject($instance)
{
return $this->getApplication()->search('ProviderInterface')->getReflectionClassForObject($instance);
}

/**
* Returns a new instance of the passed class name.
*
Expand Down
2 changes: 1 addition & 1 deletion src/AppserverIo/Appserver/ServletEngine/ServletManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ public function lookup($servletPath, $sessionId = null, array $args = array())

// inject the dependencies
$dependencyInjectionContainer = $this->getApplication()->search('ProviderInterface');
$dependencyInjectionContainer->injectDependencies($instance, null, $sessionId);
$dependencyInjectionContainer->injectDependencies($instance, $sessionId);

// return the instance
return $instance;
Expand Down

0 comments on commit de3ef3d

Please sign in to comment.