Permalink
Browse files

generics refactored

  • Loading branch information...
juzna committed Aug 26, 2012
1 parent 09c18fa commit f4cd43ef7a2eecb3f06a030114a8fe6b55847924
Showing with 746 additions and 650 deletions.
  1. +23 −48 examples/03-generics/RunTest.php
  2. +18 −0 examples/03-generics/bootstrap.php
  3. 0 examples/03-generics/{tests → compile-tests}/00.phpt
  4. +68 −0 examples/03-generics/compile-tests/01.phpt
  5. +75 −0 examples/03-generics/compile-tests/02.phpt
  6. +33 −0 examples/03-generics/compile-tests/03.phpt
  7. +40 −0 examples/03-generics/compile-tests/04.phpt
  8. +2 −2 examples/03-generics/{tests → compile-tests}/05.phpt
  9. +33 −0 examples/03-generics/compile-tests/06.phpt
  10. +15 −0 examples/03-generics/compile-tests/07.phpt
  11. +12 −0 examples/03-generics/compile.php
  12. 0 examples/03-generics/{ → model}/GenericsExample/ORM/Entity/Article.php
  13. 0 examples/03-generics/{ → model}/GenericsExample/ORM/Entity/User.php
  14. 0 examples/03-generics/{ → model}/GenericsExample/ORM/Repository.php
  15. 0 examples/03-generics/{ → model}/GenericsExample/Other/ArrayHash.php
  16. +3 −11 examples/03-generics/run.php
  17. +17 −0 examples/03-generics/src/Enhancer/Generics/GenericType.php
  18. +18 −310 examples/03-generics/{ → src/Enhancer/Generics}/GenericsEnhancer.php
  19. +13 −0 examples/03-generics/src/Enhancer/Generics/Reflection/ClassType.php
  20. +13 −0 examples/03-generics/src/Enhancer/Generics/Reflection/Method.php
  21. +177 −0 examples/03-generics/src/Enhancer/Generics/Registry.php
  22. +69 −0 examples/03-generics/src/Enhancer/Generics/TypeArgument.php
  23. +24 −0 examples/03-generics/src/Enhancer/Generics/TypeValue.php
  24. +0 −68 examples/03-generics/tests/01.phpt
  25. +0 −75 examples/03-generics/tests/02.phpt
  26. +0 −33 examples/03-generics/tests/03.phpt
  27. +0 −40 examples/03-generics/tests/04.phpt
  28. +0 −33 examples/03-generics/tests/06.phpt
  29. +0 −15 examples/03-generics/tests/07.phpt
  30. +0 −12 examples/03-generics/tests/README.md
  31. 0 examples/03-generics/{GenericsExample/usage → tests}/test01.php
  32. 0 examples/03-generics/{GenericsExample/usage → tests}/test02.php
  33. 0 examples/03-generics/{GenericsExample/usage → tests}/test03.php
  34. 0 examples/03-generics/{GenericsExample/usage → tests}/test04.php
  35. 0 examples/03-generics/{GenericsExample/usage → tests}/test05.php
  36. 0 examples/03-generics/{GenericsExample/usage → tests}/test06.php
  37. +34 −3 examples/README.md
  38. +1 −0 examples/bootstrap.php
  39. +58 −0 src/Enhancer/CompileHelper.php
@@ -2,12 +2,11 @@
namespace GenericsExample;
+use Enhancer\Generics\GenericsEnhancer;
-if (!defined('NETTE')) {
- require_once __DIR__ . '/../bootstrap.php';
-}
-require_once __DIR__ . '/GenericsEnhancer.php';
+require_once __DIR__ . '/bootstrap.php';
+
/**
* @author Filip Procházka <filip.prochazka@kdyby.org>
@@ -19,19 +18,21 @@ class RunTest extends \Tests\TestCase
public function setUp()
{
- \Enhancer\EnhancerStream::$enhancer = new \GenericsEnhancer();
+// \Enhancer\EnhancerStream::$enhancer = new \GenericsEnhancer();
}
+ /***************** compile-tests *****************j*d*/
+
/**
* @return array
*/
public function dataCompiledOutput_testsDirectory()
{
$allCases = array();
- $tests = \Nette\Utils\Finder::findFiles("*.phpt")->from(__DIR__ . '/tests');
+ $tests = \Nette\Utils\Finder::findFiles("*.phpt")->from(__DIR__ . '/compile-tests');
foreach ($tests as $file) {
/** @var \SplFileInfo $file */
@@ -53,25 +54,28 @@ public function dataCompiledOutput_testsDirectory()
*/
public function testCompiledOutput_testsDirectory($input, $expectedOutput)
{
- $enhancer = new \GenericsEnhancer();
+ $enhancer = new GenericsEnhancer();
$output = $enhancer->enhance($input);
$this->assertSame($expectedOutput, $output);
}
+ /***************** semantic tests *****************j*d*/
+
/**
* @return array
*/
public function dataRunUsages_Compiled()
{
- \Enhancer\EnhancerStream::$enhancer = new \GenericsEnhancer();
+// \Enhancer\EnhancerStream::$enhancer = new GenericsEnhancer();
if ($errors = $this->compileUsages()) {
- return $errors;
+ // return $errors;
+ // TODO: throw or run at least those tests which compiled sucessfully?
}
$tests = array();
- foreach (glob(__DIR__ . '/output/GenericsExample/usage/*.php') as $test) {
+ foreach (glob(__DIR__ . '/output/tests/*.php') as $test) {
$tests[basename($test)] = array($test);
}
@@ -104,10 +108,10 @@ public function testRunUsages_Compiled($usageCase)
*/
public function dataRunUsages_Live()
{
- \Enhancer\EnhancerStream::$enhancer = new \GenericsEnhancer();
+ \Enhancer\EnhancerStream::$enhancer = new GenericsEnhancer();
$tests = array();
- foreach (glob(__DIR__ . '/GenericsExample/usage/*.php') as $test) {
+ foreach (glob(__DIR__ . '/tests/*.php') as $test) {
$tests[basename($test)] = array($test);
}
@@ -138,42 +142,13 @@ public function testRunUsages_Live($usageCase)
*/
private static function compileUsages()
{
- $errors = array();
-
- $usages = \Nette\Utils\Finder::findFiles("*.php")->from(__DIR__ . '/GenericsExample');
- foreach ($usages as $file) {
- /** @var \SplFileInfo $file */
-
- $outputPath = __DIR__ . '/output/' . str_replace(__DIR__ . '/', '', $file->getRealPath());
- if (self::isFresh($file, $outputPath)) {
- continue; // if newer, not compile again
- }
-
- @mkdir(dirname($outputPath), 0777, true);
- file_put_contents($outputPath, file_get_contents('enhance://' . $file->getRealPath()));
-
- if ($e = \Enhancer\Utils\PhpSyntax::check($outputPath)) {
- $errors[] = array($e);
- }
- }
-
- return $errors;
- }
-
-
-
- /**
- * @param \SplFileInfo $file
- * @param string $outputPath
- *
- * @return bool
- */
- private static function isFresh(\SplFileInfo $file, $outputPath)
- {
- $enhancerRefl = new \ReflectionClass('GenericsEnhancer');
- return file_exists($outputPath)
- && filemtime($outputPath) > filemtime($file->getRealPath())
- && filemtime($outputPath) > filemtime($enhancerRefl->getFileName());
+ return \Enhancer\CompileHelper::compileDirs(
+ \Enhancer\EnhancerStream::$enhancer,
+ array(
+ __DIR__ . '/model',
+ __DIR__ . '/tests'
+ )
+ );
}
}
@@ -0,0 +1,18 @@
+<?php
+require_once __DIR__ . '/../bootstrap.php';
+
+// Setup enhancer
+$loader = new \Composer\Autoload\ClassLoader;
+$loader->register();
+$loader->add('Enhancer\Generics', __DIR__ . '/src/');
+
+Enhancer\EnhancerStream::$enhancer = new Enhancer\Generics\GenericsEnhancer;
+
+
+
+// Register autoloader
+$loader = new \Enhancer\Loaders\ClassLoader;
+$loader->register(true);
+$loader->add('GenericsExample', __DIR__ . '/model');
+
+
@@ -0,0 +1,68 @@
+simple generic class, oneline
+<?php#e
+class Collection<E> { }
+?>
+<?php#c
+\Enhancer\Generics\Registry::registerClass('Collection', array('E'));class Collection implements \Enhancer\Generics\GenericType {public function getParametrizedType($parameterName) { return \Enhancer\Generics\Registry::getParametrizedTypesForObject($this, $parameterName); } }
+?>
+------------------------------------------------j-d--
+
+generic class with a method
+<?php#e
+class Collection<E>
+{
+ function foo() { }
+}
+?>
+<?php#c
+\Enhancer\Generics\Registry::registerClass('Collection', array('E'));class Collection implements \Enhancer\Generics\GenericType
+{public function getParametrizedType($parameterName) { return \Enhancer\Generics\Registry::getParametrizedTypesForObject($this, $parameterName); }
+ function foo() { }
+}
+?>
+------------------------------------------------j-d--
+
+generic class implementing interface
+<?php#e
+class Collection<E> implements IteratorAggregate
+{
+ function foo() { }
+}
+?>
+<?php#c
+\Enhancer\Generics\Registry::registerClass('Collection', array('E'));class Collection implements IteratorAggregate, \Enhancer\Generics\GenericType
+{public function getParametrizedType($parameterName) { return \Enhancer\Generics\Registry::getParametrizedTypesForObject($this, $parameterName); }
+ function foo() { }
+}
+?>
+------------------------------------------------j-d--
+
+generic class extending super class
+<?php#e
+class Collection<E> extends \Nette\Object
+{
+ function foo() { }
+}
+?>
+<?php#c
+\Enhancer\Generics\Registry::registerClass('Collection', array('E'));class Collection extends \Nette\Object implements \Enhancer\Generics\GenericType
+{public function getParametrizedType($parameterName) { return \Enhancer\Generics\Registry::getParametrizedTypesForObject($this, $parameterName); }
+ function foo() { }
+}
+?>
+------------------------------------------------j-d--
+
+generic class extending super class and implementing interface
+<?php#e
+class Collection<E> extends \Nette\Object implements IteratorAggregate
+{
+ function foo() { }
+}
+?>
+<?php#c
+\Enhancer\Generics\Registry::registerClass('Collection', array('E'));class Collection extends \Nette\Object implements IteratorAggregate, \Enhancer\Generics\GenericType
+{public function getParametrizedType($parameterName) { return \Enhancer\Generics\Registry::getParametrizedTypesForObject($this, $parameterName); }
+ function foo() { }
+}
+?>
+------------------------------------------------j-d--
@@ -0,0 +1,75 @@
+new instance - simple
+<?php#e
+use MyNS\Foo;
+$f = new Foo;
+?>
+<?php#c
+use MyNS\Foo;
+$f = \Enhancer\Generics\Registry::newInstance('MyNS\Foo', NULL);
+?>
+------------------------------------------------j-d--
+
+
+new instance - generics
+<?php#e
+use MyNS\Foo;
+$f = new Factory<Foo>();
+?>
+<?php#c
+use MyNS\Foo;
+$f = \Enhancer\Generics\Registry::newInstance('Factory', array('MyNS\Foo'));
+?>
+------------------------------------------------j-d--
+
+
+new instance - generics + arguments
+<?php#e
+$f = new Factory<Foo>($em, 2, foo(3));
+?>
+<?php#c
+$f = \Enhancer\Generics\Registry::newInstance('Factory', array('Foo'), $em, 2, foo(3));
+?>
+------------------------------------------------j-d--
+
+
+new instance of type-argument
+<?php#e
+class Factory<E>
+{
+ function create($id)
+ {
+ return new E($id);
+ }
+}
+?>
+<?php#c
+\Enhancer\Generics\Registry::registerClass('Factory', array('E'));class Factory implements \Enhancer\Generics\GenericType
+{public function getParametrizedType($parameterName) { return \Enhancer\Generics\Registry::getParametrizedTypesForObject($this, $parameterName); }
+ function create($id)
+ {
+ return \Enhancer\Generics\Registry::newInstance(\Enhancer\Generics\Registry::resolveTypeArgument($this, 'E'), NULL, $id);
+ }
+}
+?>
+------------------------------------------------j-d--
+
+
+new instance of generic with type-argument
+<?php#e
+use ORM\Common\Collection;
+class Factory<E>
+{
+ public function createCollection(array $ids) {
+ return new Collection<E>($ids[0], $ids[1]);
+ }
+}
+?>
+<?php#c
+use ORM\Common\Collection;
+\Enhancer\Generics\Registry::registerClass('Factory', array('E'));class Factory implements \Enhancer\Generics\GenericType
+{public function getParametrizedType($parameterName) { return \Enhancer\Generics\Registry::getParametrizedTypesForObject($this, $parameterName); }
+ public function createCollection(array $ids) {
+ return \Enhancer\Generics\Registry::newInstance('ORM\Common\Collection', array(\Enhancer\Generics\Registry::resolveTypeArgument($this, 'E')), $ids[0], $ids[1]);
+ }
+}
+?>
@@ -0,0 +1,33 @@
+typehint validation
+<?php#e
+use ORM\Common\Collection;
+
+class Repository<E> extends BaseRepository
+{
+ function saveOne(E $item)
+ {
+ return true;
+ }
+
+ function saveMultiple(Collection<E> $list)
+ {
+ return true;
+ }
+}
+?>
+<?php#c
+use ORM\Common\Collection;
+
+\Enhancer\Generics\Registry::registerClass('Repository', array('E'));class Repository extends BaseRepository implements \Enhancer\Generics\GenericType
+{public function getParametrizedType($parameterName) { return \Enhancer\Generics\Registry::getParametrizedTypesForObject($this, $parameterName); }
+ function saveOne($item)
+ {\Enhancer\Generics\Registry::ensureInstance($item, \Enhancer\Generics\Registry::resolveTypeArgument($this, 'E'), array());
+ return true;
+ }
+
+ function saveMultiple(Collection $list)
+ {\Enhancer\Generics\Registry::ensureInstance($list, 'ORM\Common\Collection', array(\Enhancer\Generics\Registry::resolveTypeArgument($this, 'E')));
+ return true;
+ }
+}
+?>
@@ -0,0 +1,40 @@
+instanceof - simple
+<?php#e
+dump($foo instanceof Foo);
+?>
+<?php#c
+dump($foo instanceof Foo);
+?>
+------------------------------------------------j-d--
+
+
+instanceof - generic
+<?php#e
+dump($foo instanceof Foo<User>);
+?>
+<?php#c
+dump($foo instanceof Foo && \Enhancer\Generics\Registry::checkInstance($foo, 'Foo', array('User')));
+?>
+------------------------------------------------j-d--
+
+
+instanceof - within class
+<?php#e
+class Repository<E>
+{
+ function foo($foo)
+ {
+ return $foo instanceof Foo<E>;
+ }
+}
+?>
+<?php#c
+\Enhancer\Generics\Registry::registerClass('Repository', array('E'));class Repository implements \Enhancer\Generics\GenericType
+{public function getParametrizedType($parameterName) { return \Enhancer\Generics\Registry::getParametrizedTypesForObject($this, $parameterName); }
+ function foo($foo)
+ {
+ return $foo instanceof Foo && \Enhancer\Generics\Registry::checkInstance($foo, 'Foo', array(\Enhancer\Generics\Registry::resolveTypeArgument($this, 'E')));
+ }
+}
+?>
+------------------------------------------------j-d--
@@ -30,8 +30,8 @@ class Foo<E>
}
?>
<?php#c
-\GenericsRegistry::registerClass('Foo', array('E'));class Foo implements \GenericType
-{public function getParametrizedType($parameterName) { return \GenericsRegistry::getParametrizedTypesForObject($this); }
+\Enhancer\Generics\Registry::registerClass('Foo', array('E'));class Foo implements \Enhancer\Generics\GenericType
+{public function getParametrizedType($parameterName) { return \Enhancer\Generics\Registry::getParametrizedTypesForObject($this, $parameterName); }
public function getEntity()
{
// dummy
Oops, something went wrong.

0 comments on commit f4cd43e

Please sign in to comment.