Permalink
Browse files

Initial code.

  • Loading branch information...
0 parents commit 5ec87400877978ff669fede28535858555a88fdf @inxilpro committed Jan 10, 2012
Showing with 409 additions and 0 deletions.
  1. +6 −0 .buildpath
  2. +22 −0 .project
  3. +4 −0 .settings/org.eclipse.php.core.prefs
  4. +101 −0 Readme.markdown
  5. +86 −0 lib/Zit/Container.php
  6. +177 −0 tests/ContainerTests.php
  7. +13 −0 tests/TestObj.php
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<buildpath>
+ <buildpathentry kind="con" path="com.zend.php.phpunit.CONTAINER"/>
+ <buildpathentry kind="src" path=""/>
+ <buildpathentry kind="con" path="org.eclipse.php.core.LANGUAGE"/>
+</buildpath>
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>Zit</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>org.eclipse.wst.validation.validationbuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>org.eclipse.dltk.core.scriptbuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>org.eclipse.php.core.PHPNature</nature>
+ </natures>
+</projectDescription>
@@ -0,0 +1,4 @@
+#Tue Jan 10 15:19:08 EST 2012
+eclipse.preferences.version=1
+include_path=0;/Zit\u00055;com.zend.php.phpunit.CONTAINER
+use_asp_tags_as_php=false
@@ -0,0 +1,101 @@
+# Zit
+
+Zit is a simple dependency injector based heavily on Pimple. It aims to provide the
+same simplicity as Pimple while offering a slightly more robust object interface.
+
+## Usage
+
+Zit is simple to use. Just include it and create a new container:
+
+ require_once '/path/to/lib/Zit/Container.php';
+ $container = new \Zit\Container();
+
+## Defining Objects
+
+Like Pimple, objects are defined through anonymous functions that return an instance
+of the object:
+
+ $container->set('auth', function() {
+ return new Auth();
+ });
+
+All instantiation functions are passed the container as the first argument, making
+dependency injection possible:
+
+ $container->set('auth', function($container) {
+ return new Auth($container->get('db'));
+ });
+
+Zit also provides convenient magic methods for setting instantiation functions:
+
+ $container->setAuth(function() { // ... });
+ // Or, if you prefer underscores:
+ $container->set_auth(function() { // ... });
+
+## Getting Objects
+
+Getting objects are as simple as:
+
+ $container->get('auth');
+
+Or, if you prefer the shorthand:
+
+ $container->getAuth();
+ // Or:
+ $container->get_auth();
+
+## Getting Fresh Objects
+
+By default, all objects are shared in Zit. That is, once an object is created, that same
+exact object is returned for each additional get(). If you need a fresh object, you can
+do so with:
+
+ $container->fresh('auth'); // Or:
+ $container->freshAuth(); // Or:
+ $container->fresh_auth(); // Or:
+ $container->newAuth(); // Or:
+ $container->new_auth();
+
+Note the because the 'new' keyword is reserved, you can only use it if you're using
+the magic methods.
+
+## Constructor Parameters
+
+Sometimes you need to pass parameters to the constructor of an object, while still also
+injecting dependencies. Zit automatically passes all parameters on to your instantiation
+function:
+
+ $container->setUser(function($c, $id)) {
+ $user = new User($id);
+ $user->setDb($c->getDb());
+ return $user;
+ });
+
+ $user = $container->newUser(1);
+
+## Storing Non-Objects
+
+You can also use Zit to store non-objects (anything from a string to an anonymous function).
+Just use the setParam() method:
+
+ $container->setParam('api_key', 'abcd1234567890');
+ $key = $container->get('api_key');
+
+## Custom Container
+
+Most projects will benefit from a custom container that sets up its own injection rules. This
+is as simple as extending Zit:
+
+ namespace MyApp\Di;
+
+ class Container extends \Zit\Container
+ {
+ public function __construct()
+ {
+ $this->setAuth(function() { // ... });
+ $this->setUser(function() { // ... });
+ }
+ }
+
+
+
@@ -0,0 +1,86 @@
+<?php
+
+namespace Zit;
+
+class Container
+{
+ protected $_objects = array();
+ protected $_callbacks = array();
+
+ public function __call($name, $arguments = array())
+ {
+ // Parse function name
+ preg_match_all('/_?([A-Z][a-z0-9]*|[a-z0-9]+)/', $name, $parts);
+ $parts = $parts[1];
+
+ // Determine method
+ $method = array_shift($parts);
+ if ('new' == $method) {
+ $method = 'fresh';
+ }
+
+ // Determine object key
+ $key = strtolower(implode('_', $parts));
+ array_unshift($arguments, $key);
+
+ // Call method if exists
+ if (method_exists($this, $method)) {
+ return call_user_func_array(array($this, $method), $arguments);
+ }
+
+ // Throw exception on miss
+ throw new \InvalidArgumentException(sprintf('Methood "%s" does not exist.', $method));
+ }
+
+ public function set($name, \Closure $callable)
+ {
+ $this->_callbacks[$name] = $callable;
+ }
+
+ public function setParam($name, $param)
+ {
+ $this->set($name, function() use ($param) {
+ return $param;
+ });
+ }
+
+ public function has($name)
+ {
+ return isset($this->_callbacks[$name]);
+ }
+
+ public function get($name)
+ {
+ // Return object if it's already instantiated
+ if (isset($this->_objects[$name])) {
+ return $this->_objects[$name];
+ }
+
+ // Otherwise create a new one
+ return call_user_func_array(array($this, 'fresh'), func_get_args());
+ }
+
+ public function fresh($name)
+ {
+ if (!isset($this->_callbacks[$name])) {
+ throw new \InvalidArgumentException(sprintf('Callback for "%s" does not exist.', $name));
+ }
+
+ $arguments = func_get_args();
+ $arguments[0] = $this;
+ $this->_objects[$name] = call_user_func_array($this->_callbacks[$name], $arguments);
+ return $this->_objects[$name];
+ }
+
+ public function delete($name)
+ {
+ if (isset($this->_objects[$name])) {
+ unset($this->_objects[$name]);
+ return true;
+ }
+
+ return false;
+ }
+}
+
+
Oops, something went wrong.

0 comments on commit 5ec8740

Please sign in to comment.