Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
tux-rampage committed Apr 28, 2016
1 parent 10d5275 commit 8b9478d
Show file tree
Hide file tree
Showing 9 changed files with 328 additions and 144 deletions.
14 changes: 14 additions & 0 deletions config/module.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* This file provides the distributed module configuration for Zend\Di
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

return [
// TODO: Module configuration
];
119 changes: 117 additions & 2 deletions src/Definition/Introspection/DefaultStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,132 @@

namespace Zend\Di\Definition\Introspection;

use ReflectionClass;
use ReflectionMethod;
use ReflectionParameter;

use Zend\Di\Definition\Annotation;

use Zend\Code\Annotation\AnnotationManager;
use Zend\Code\Annotation\Parser\GenericAnnotationParser;


/**
* The default introspection strategy for PHP7 and up
*/
class DefaultStrategy implements StrategyInterface
{
/**
* Method inclusion pattern
*/
const METHOD_PATTERN = '/^set[A-Z]{1}\w*/';

/**
* Interface inclusion pattern
*/
const INTERFACE_PATTERN = '/\w*Aware\w*/';


/**
* The annotation manager instance for this strategy
*
* @var AnnotationManager
*/
protected $annotationManager = null;

/**
* Annotation usage flag
*
* @var bool
*/
protected $useAnnotations = true;

/**
* Constructor
*
* @param null|AnnotationManager $annotationManager
*/
public function __construct(AnnotationManager $annotationManager = null)
{
$this->annotationManager = $annotationManager?: $this->createDefaultAnnotationManager();
}

/**
* Creates the default annotation manager
*
* @return AnnotationManager
*/
protected function createDefaultAnnotationManager()
{
$annotationManager = new AnnotationManager();
$parser = new GenericAnnotationParser();

$parser->registerAnnotation(new Annotation\Inject());
$parser->registerAnnotation(new Annotation\Instantiator());

$annotationManager->attach($parser);

return $annotationManager;
}

/**
* {@inheritDoc}
* @see \Zend\Di\Definition\Introspection\StrategyInterface::getAnnotationManager()
*/
public function getAnnotationManager()
{
return $this->annotationManager;
}

/**
* {@inheritDoc}
* @see \Zend\Di\Definition\Introspection\StrategyInterface::getUseAnnotations()
*/
public function getUseAnnotations()
{
return $this->useAnnotations;
}

/**
* Specify annotation usage
*
* @param bool $flag Whether to enable/disable annotation usage
* @return self
*/
public function setUseAnnotations($flag)
{
$this->useAnnotations = (bool)$flag;
return $this;
}

/**
* {@inheritDoc}
* @see \Zend\Di\Definition\Introspection\StrategyInterface::includeInterfaceMethods()
*/
public function includeInterfaceMethods(ReflectionClass $reflectedInterface)
{
return (bool)preg_match(self::INTERFACE_PATTERN, $reflectedInterface->getName());
}

/**
* {@inheritDoc}
* @see \Zend\Di\Definition\Introspection\StrategyInterface::includeMethod()
*/
public function includeMethod(ReflectionMethod $reflectedMethod)
{
return (bool)preg_match(self::METHOD_PATTERN, $reflectedMethod->getName());
}

/**
* {@inheritDoc}
* @see \Zend\Di\Definition\Introspection\StrategyInterface::reflectParameterType()
*/
public function reflectParameterType(\ReflectionParameter $parameter)
public function reflectParameterType(ReflectionParameter $parameter)
{
// TODO Auto-generated method stub
if (!$parameter->hasType()) {
return null;
}

return (string)$parameter->getType();
}
}
14 changes: 11 additions & 3 deletions src/Definition/Introspection/Php5Strategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,25 @@

namespace Zend\Di\Definition\Introspection;

use ReflectionParameter;

/**
* Introspection strategy for PHP5
*/
class Php5Strategy implements StrategyInterface
class Php5Strategy extends DefaultStrategy implements StrategyInterface
{
/**
* {@inheritDoc}
* @see \Zend\Di\Definition\Introspection\StrategyInterface::reflectParameterType()
*/
public function reflectParameterType(\ReflectionParameter $parameter)
public function reflectParameterType(ReflectionParameter $parameter)
{
// TODO Auto-generated method stub
$class = $parameter->getClass();

if (!$class) {
return null;
}

return $class->getName();
}
}
6 changes: 5 additions & 1 deletion src/Definition/Introspection/StrategyInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ interface StrategyInterface
/**
* Reflect the parameter type
*
* This should return the required type name. This might
* either be a FQCN or a buildin type name (i.e. string or int)
*
* @param \ReflectionParameter $parameter
* @return string|null
*/
public function reflectParameterType(ReflectionParameter $parameter);

Expand Down Expand Up @@ -56,4 +60,4 @@ public function includeMethod(ReflectionMethod $reflectedMethod);
* @return bool
*/
public function includeInterfaceMethods(ReflectionClass $reflectedInterface);
}
}
135 changes: 0 additions & 135 deletions src/Definition/IntrospectionStrategy.php

This file was deleted.

30 changes: 27 additions & 3 deletions src/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,33 @@

namespace Zend\Di;

use Zend\ModuleManager\Feature\ConfigProviderInterface;


/**
* @todo ZF Module definition
* Provides Module functionality for Zend Framework 3 applications
*
* To add the DI integration to your application, add it to the ZF modules list:
*
* ```php
* // application.config.php
* return [
* // ...
* 'modules' => [
* 'Zend\\Di',
* // ...
* ]
* ];
* ```
*/
class Module
class Module implements ConfigProviderInterface
{
}
/**
* {@inheritDoc}
* @see \Zend\ModuleManager\Feature\ConfigProviderInterface::getConfig()
*/
public function getConfig()
{
return require __DIR__ . '/../config/module.dist.php';
}
}
Loading

0 comments on commit 8b9478d

Please sign in to comment.