Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

merged branch igorw/invokable-factory (PR #47)

Commits
-------

a419262 Treat invokable objects as factories too (just like closures)

Discussion
----------

Treat invokable objects as factories too (just like closures)

Note: instances of Closure are also invokable.

---------------------------------------------------------------------------

by fabpot at 2012-09-10T07:00:31Z

Can you squash your commits before I merge? Thanks.

---------------------------------------------------------------------------

by igorw at 2012-09-10T10:57:53Z

Done.
  • Loading branch information...
commit b9f27b8dc18c08f00627dec02359b46a24791dc3 2 parents db836b8 + a419262
@fabpot authored
View
4 lib/Pimple.php
@@ -78,7 +78,9 @@ public function offsetGet($id)
throw new InvalidArgumentException(sprintf('Identifier "%s" is not defined.', $id));
}
- return $this->values[$id] instanceof Closure ? $this->values[$id]($this) : $this->values[$id];
+ $isFactory = is_object($this->values[$id]) && method_exists($this->values[$id], '__invoke');
+
+ return $isFactory ? $this->values[$id]($this) : $this->values[$id];
}
/**
View
35 tests/Pimple/Tests/Invokable.php
@@ -0,0 +1,35 @@
+<?php
+
+/*
+ * This file is part of Pimple.
+ *
+ * Copyright (c) 2009 Fabien Potencier
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is furnished
+ * to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+namespace Pimple\Tests;
+
+class Invokable
+{
+ public function __invoke()
+ {
+ return 'I was invoked';
+ }
+}
View
34 tests/Pimple/Tests/NonInvokable.php
@@ -0,0 +1,34 @@
+<?php
+
+/*
+ * This file is part of Pimple.
+ *
+ * Copyright (c) 2009 Fabien Potencier
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is furnished
+ * to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+namespace Pimple\Tests;
+
+class NonInvokable
+{
+ public function __call($a, $b)
+ {
+ }
+}
View
18 tests/Pimple/Tests/PimpleTest.php
@@ -249,4 +249,22 @@ public function testKeys()
$this->assertEquals(array('foo', 'bar'), $pimple->keys());
}
+
+ /** @test */
+ public function settingAnInvokableObjectShouldTreatItAsFactory()
+ {
+ $pimple = new Pimple();
+ $pimple['invokable'] = new Invokable();
+
+ $this->assertEquals('I was invoked', $pimple['invokable']);
+ }
+
+ /** @test */
+ public function settingNonInvokableObjectShouldTreatItAsParameter()
+ {
+ $pimple = new Pimple();
+ $pimple['non_invokable'] = new NonInvokable();
+
+ $this->assertInstanceOf('Pimple\Tests\NonInvokable', $pimple['non_invokable']);
+ }
}
View
2  tests/Pimple/Tests/Service.php
@@ -26,8 +26,6 @@
namespace Pimple\Tests;
-use Pimple;
-
/**
* Pimple Test Service
*
View
2  tests/bootstrap.php
@@ -11,3 +11,5 @@
require_once __DIR__.'/../lib/Pimple.php';
require_once __DIR__.'/Pimple/Tests/Service.php';
+require_once __DIR__.'/Pimple/Tests/Invokable.php';
+require_once __DIR__.'/Pimple/Tests/NonInvokable.php';
Please sign in to comment.
Something went wrong with that request. Please try again.