forked from Kdyby/Autowired
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
0 parents
commit a8fafba
Showing
16 changed files
with
733 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.gitattributes export-ignore | ||
.gitignore export-ignore | ||
.travis.yml export-ignore | ||
docs export-ignore | ||
tests export-ignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
vendor | ||
composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
language: php | ||
|
||
php: | ||
- 5.3.3 | ||
- 5.4 | ||
|
||
before_script: | ||
- composer self-update | ||
- composer install --no-interaction --prefer-dist --quiet --dev | ||
|
||
script: php tests/lint.php src/Kdyby/ tests/KdybyTests/ && VERBOSE=true ./tests/run-tests.sh -s tests/KdybyTests/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
Kdyby/Autowired [](http://travis-ci.org/Kdyby/Autowired) | ||
=========================== | ||
|
||
|
||
Requirements | ||
------------ | ||
|
||
Kdyby/Autowired requires PHP 5.3.2 or higher. | ||
|
||
- [Nette Framework 2.0.x](https://github.com/nette/nette) | ||
|
||
|
||
Installation | ||
------------ | ||
|
||
The best way to install Kdyby/Autowired is using [Composer](http://getcomposer.org/): | ||
|
||
```sh | ||
$ composer require kdyby/autowired:@dev | ||
``` | ||
|
||
|
||
----- | ||
|
||
Homepage [http://www.kdyby.org](http://www.kdyby.org) and repository [http://github.com/kdyby/autowired](http://github.com/kdyby/autowired). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{ | ||
"name": "kdyby/autowired", | ||
"type": "library", | ||
"description": "DI Extension for Nette Framework", | ||
"keywords": ["nette", "di", "autowire", "kdyby"], | ||
"homepage": "http://kdyby.org", | ||
"license": ["BSD-3-Clause", "GPL-2.0", "GPL-3.0"], | ||
"authors": [ | ||
{ | ||
"name": "Filip Procházka", | ||
"homepage": "http://filip-prochazka.com" | ||
} | ||
], | ||
"support": { | ||
"email": "[email protected]", | ||
"issues": "https://github.com/kdyby/autowired/issues" | ||
}, | ||
"require": { | ||
"nette/nette": "@dev" | ||
}, | ||
"require-dev": { | ||
"nette/tester": "@dev" | ||
}, | ||
"autoload": { | ||
"psr-0": { | ||
"Kdyby\\Autowired": "src/" | ||
}, | ||
"classmap": [ | ||
"src/Kdyby/Autowired/exceptions.php" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
Licenses | ||
======== | ||
|
||
Good news! You may use Kdyby Framework under the terms of either | ||
the New BSD License or the GNU General Public License (GPL) version 2 or 3. | ||
|
||
The BSD License is recommended for most projects. It is easy to understand and it | ||
places almost no restrictions on what you can do with the framework. If the GPL | ||
fits better to your project, you can use the framework under this license. | ||
|
||
You don't have to notify anyone which license you are using. You can freely | ||
use Kdyby Framework in commercial projects as long as the copyright header | ||
remains intact. | ||
|
||
|
||
|
||
New BSD License | ||
--------------- | ||
|
||
Copyright (c) 2008 Filip Procházka (http://filip-prochazka.com) | ||
All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without modification, | ||
are permitted provided that the following conditions are met: | ||
|
||
* Redistributions of source code must retain the above copyright notice, | ||
this list of conditions and the following disclaimer. | ||
|
||
* Redistributions in binary form must reproduce the above copyright notice, | ||
this list of conditions and the following disclaimer in the documentation | ||
and/or other materials provided with the distribution. | ||
|
||
* Neither the name of "Kdyby Framework" nor the names of its contributors | ||
may be used to endorse or promote products derived from this software | ||
without specific prior written permission. | ||
|
||
This software is provided by the copyright holders and contributors "as is" and | ||
any express or implied warranties, including, but not limited to, the implied | ||
warranties of merchantability and fitness for a particular purpose are | ||
disclaimed. In no event shall the copyright owner or contributors be liable for | ||
any direct, indirect, incidental, special, exemplary, or consequential damages | ||
(including, but not limited to, procurement of substitute goods or services; | ||
loss of use, data, or profits; or business interruption) however caused and on | ||
any theory of liability, whether in contract, strict liability, or tort | ||
(including negligence or otherwise) arising in any way out of the use of this | ||
software, even if advised of the possibility of such damage. | ||
|
||
|
||
|
||
GNU General Public License | ||
-------------------------- | ||
|
||
GPL licenses are very very long, so instead of including them here we offer | ||
you URLs with full text: | ||
|
||
- [GPL version 2](http://www.gnu.org/licenses/gpl-2.0.html) | ||
- [GPL version 3](http://www.gnu.org/licenses/gpl-3.0.html) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the Kdyby (http://www.kdyby.org) | ||
* | ||
* Copyright (c) 2008 Filip Procházka ([email protected]) | ||
* | ||
* For the full copyright and license information, please view the file license.txt that was distributed with this source code. | ||
*/ | ||
|
||
namespace Kdyby\Autowired; | ||
|
||
use Nette; | ||
use Nette\Reflection\Method; | ||
use Nette\Reflection\Property; | ||
use Nette\Reflection\ClassType; | ||
|
||
|
||
|
||
/** | ||
* @author Filip Procházka <[email protected]> | ||
*/ | ||
trait AutowireProperties | ||
{ | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $autowire = array(); | ||
|
||
/** | ||
* @var Nette\DI\Container | ||
*/ | ||
private $autowirePropertiesLocator; | ||
|
||
|
||
|
||
/** | ||
* @param \Nette\DI\Container $dic | ||
* @throws MemberAccessException | ||
* @throws MissingServiceException | ||
* @throws InvalidStateException | ||
* @throws UnexpectedValueException | ||
*/ | ||
public function injectProperties(Nette\DI\Container $dic) | ||
{ | ||
if (!$this instanceof Nette\Application\UI\PresenterComponent) { | ||
throw new MemberAccessException('Trait ' . __TRAIT__ . ' can be used only in descendants of PresenterComponent.'); | ||
} | ||
|
||
$this->autowirePropertiesLocator = $dic; | ||
$cache = new Nette\Caching\Cache($dic->getByType('Nette\Caching\IStorage'), 'Kdyby.Autowired.PresenterComponent'); | ||
if (($this->autowire = $cache->load($presenterClass = get_class($this))) === NULL) { | ||
$this->autowire = array(); | ||
|
||
$rc = ClassType::from($this); | ||
$ignore = class_parents('Nette\Application\UI\Presenter') + array('ui' => 'Nette\Application\UI\Presenter'); | ||
foreach ($rc->getProperties(Property::IS_PUBLIC | Property::IS_PROTECTED) as $prop) { | ||
/** @var Property $prop */ | ||
if (in_array($prop->getDeclaringClass()->getName(), $ignore) || !$prop->hasAnnotation('autowire')) { | ||
continue; | ||
} | ||
|
||
$this->resolveProperty($prop); | ||
} | ||
|
||
$files = array_map(function ($class) { | ||
return ClassType::from($class)->getFileName(); | ||
}, array_diff(array_values(class_parents($presenterClass) + array('me' => $presenterClass)), $ignore)); | ||
|
||
$cache->save($presenterClass, $this->autowire, array( | ||
$cache::FILES => $files, | ||
)); | ||
|
||
} else { | ||
foreach ($this->autowire as $propName => $tmp) { | ||
unset($this->{$propName}); | ||
} | ||
} | ||
} | ||
|
||
|
||
|
||
/** | ||
* @param Property $prop | ||
* @throws MissingServiceException | ||
* @throws UnexpectedValueException | ||
*/ | ||
private function resolveProperty(Property $prop) | ||
{ | ||
$type = $this->resolveAnnotationClass($prop, $prop->getAnnotation('var'), 'var'); | ||
$metadata = array( | ||
'value' => NULL, | ||
'type' => $type, | ||
); | ||
|
||
if (($args = (array) $prop->getAnnotation('autowire')) && !empty($args['factory'])) { | ||
$factoryType = $this->resolveAnnotationClass($prop, $args['factory'], 'autowire'); | ||
|
||
if (empty($this->autowirePropertiesLocator->classes[strtolower($factoryType)])) { | ||
throw new MissingServiceException("Factory of type \"$factoryType\" not found for $prop in annotation @autowire."); | ||
} | ||
|
||
$factoryMethod = Method::from($factoryType, 'create'); | ||
$createsType = $this->resolveAnnotationClass($factoryMethod, $factoryMethod->getAnnotation('return'), 'return'); | ||
if ($createsType !== $type) { | ||
throw new UnexpectedValueException("The property $prop requires $type, but factory of type $factoryType, that creates $createsType was provided."); | ||
} | ||
|
||
unset($args['factory']); | ||
$metadata['arguments'] = array_values($args); | ||
$metadata['factory'] = $this->autowirePropertiesLocator->classes[strtolower($factoryType)]; | ||
|
||
} else { | ||
if (empty($this->autowirePropertiesLocator->classes[strtolower($type)])) { | ||
throw new MissingServiceException("Service of type \"$type\" not found for $prop in annotation @var."); | ||
} | ||
} | ||
|
||
// unset property to pass control to __set() and __get() | ||
unset($this->{$prop->getName()}); | ||
$this->autowire[$prop->getName()] = $metadata; | ||
} | ||
|
||
|
||
|
||
private function resolveAnnotationClass(\Reflector $prop, $annotationValue, $annotationName) | ||
{ | ||
/** @var Property|Method $prop */ | ||
|
||
if (!$type = ltrim($annotationValue, '\\')) { | ||
throw new InvalidStateException("Missing annotation @{$annotationName} with typehint on {$prop}."); | ||
} | ||
|
||
if (!class_exists($type) && !interface_exists($type)) { | ||
if (substr(func_get_arg(1), 0, 1) === '\\') { | ||
throw new MissingClassException("Class \"$type\" was not found, please check the typehint on {$prop} in annotation @{$annotationName}"); | ||
} | ||
|
||
if (!class_exists($type = $prop->getDeclaringClass()->getNamespaceName() . '\\' . $type) && !interface_exists($type)) { | ||
throw new MissingClassException("Neither class \"" . func_get_arg(1) . "\" or \"{$type}\" was found, please check the typehint on {$prop} in annotation @{$annotationName}"); | ||
} | ||
} | ||
|
||
return ClassType::from($type)->getName(); | ||
} | ||
|
||
|
||
|
||
/** | ||
* @param string $name | ||
* @param mixed $value | ||
* @throws MemberAccessException | ||
* @return mixed | ||
*/ | ||
public function __set($name, $value) | ||
{ | ||
if (!isset($this->autowire[$name])) { | ||
return parent::__set($name, $value); | ||
|
||
} elseif ($this->autowire[$name]['value']) { | ||
throw new MemberAccessException("Property \$$name has already been set."); | ||
|
||
} elseif (!$value instanceof $this->autowire[$name]['type']) { | ||
throw new MemberAccessException("Property \$$name must be an instance of " . $this->autowire[$name]['type'] . "."); | ||
} | ||
|
||
return $this->autowire[$name]['value'] = $value; | ||
} | ||
|
||
|
||
|
||
/** | ||
* @param $name | ||
* @throws MemberAccessException | ||
* @return mixed | ||
*/ | ||
public function &__get($name) | ||
{ | ||
if (!isset($this->autowire[$name])) { | ||
return parent::__get($name); | ||
} | ||
|
||
if (empty($this->autowire[$name]['value'])) { | ||
if (!empty($this->autowire[$name]['factory'])) { | ||
$factory = callback($this->autowirePropertiesLocator->getService($this->autowire[$name]['factory']), 'create'); | ||
$this->autowire[$name]['value'] = $factory->invokeArgs($this->autowire[$name]['arguments']); | ||
|
||
} else { | ||
$this->autowire[$name]['value'] = $this->autowirePropertiesLocator->getByType($this->autowire[$name]['type']); | ||
} | ||
} | ||
|
||
return $this->autowire[$name]['value']; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the Kdyby (http://www.kdyby.org) | ||
* | ||
* Copyright (c) 2008 Filip Procházka ([email protected]) | ||
* | ||
* For the full copyright and license information, please view the file license.txt that was distributed with this source code. | ||
*/ | ||
|
||
namespace Kdyby\Autowired; | ||
|
||
|
||
interface Exception | ||
{ | ||
} | ||
|
||
|
||
|
||
/** | ||
* The exception that is thrown when a method call is invalid for the object's | ||
* current state, method has been invoked at an illegal or inappropriate time. | ||
*/ | ||
class InvalidStateException extends \RuntimeException implements Exception | ||
{ | ||
} | ||
|
||
|
||
|
||
/** | ||
* The exception that is thrown when an argument does not match with the expected value. | ||
*/ | ||
class InvalidArgumentException extends \InvalidArgumentException implements Exception | ||
{ | ||
} | ||
|
||
|
||
|
||
/** | ||
* The exception that is thrown when a value (typically returned by function) does not match with the expected value. | ||
*/ | ||
class UnexpectedValueException extends \UnexpectedValueException implements Exception | ||
{ | ||
} | ||
|
||
|
||
class MemberAccessException extends \Nette\MemberAccessException implements Exception | ||
{ | ||
|
||
} | ||
|
||
|
||
|
||
class MissingServiceException extends \Nette\DI\MissingServiceException implements Exception | ||
{ | ||
} | ||
|
||
|
||
class MissingClassException extends InvalidStateException | ||
{ | ||
|
||
} |
Oops, something went wrong.