Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

initial import

  • Loading branch information...
commit bc38d36508aceb4d90102fb38bdadb6ce8ced1b5 0 parents
@gonzalo123 authored
3  .gitignore
@@ -0,0 +1,3 @@
+vendor/*
+bin/*
+composer.lock
75 README.md
@@ -0,0 +1,75 @@
+Example of standalone usage of dependency injection container from Symfony
+
+(remember to install vendors first with "composer install")
+
+```php
+<?php
+include __DIR__ . "/../vendor/autoload.php";
+
+use Symfony\Component\DependencyInjection\ContainerBuilder;
+use Symfony\Component\DependencyInjection\Reference;
+use Symfony\Component\Config\FileLocator;
+use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
+
+$container = new ContainerBuilder();
+$loader = new YamlFileLoader($container, new FileLocator(__DIR__));
+$loader->load('services.yml');
+
+echo $container->get('app')->hello();
+```
+
+Using a yaml file defining services:
+```
+services:
+ app:
+ class: App
+ arguments: [@Proxy]
+ proxy:
+ class: Proxy
+ arguments: [@Curl]
+ curl:
+ class: Curl
+```
+
+Whith those three classes:
+
+```php
+<?php
+class App
+{
+ private $proxy;
+
+ public function __construct(Proxy $proxy)
+ {
+ $this->proxy = $proxy;
+ }
+
+ public function hello()
+ {
+ return $this->proxy->hello();
+ }
+}
+
+class Curl
+{
+ public function doGet()
+ {
+ return "Hello";
+ }
+}
+
+class Proxy
+{
+ private $curl;
+
+ public function __construct(Curl $curl)
+ {
+ $this->curl = $curl;
+ }
+
+ public function hello()
+ {
+ return $this->curl->doGet();
+ }
+}
+```
19 composer.json
@@ -0,0 +1,19 @@
+{
+ "authors":[
+ {
+ "name":"Gonzalo Ayuso",
+ "email":"gonzalo123@gmail.com",
+ "homepage":"http://gonzalo123.wordpress.com/"
+ }
+ ],
+ "require": {
+ "symfony/dependency-injection": "dev-master",
+ "symfony/yaml": "dev-master",
+ "symfony/config": "dev-master"
+ },
+ "autoload":{
+ "psr-0":{
+ "":"lib/"
+ }
+ }
+}
13 example/example1.php
@@ -0,0 +1,13 @@
+<?php
+include __DIR__ . "/../vendor/autoload.php";
+
+use Symfony\Component\DependencyInjection\ContainerBuilder;
+use Symfony\Component\DependencyInjection\Reference;
+use Symfony\Component\Config\FileLocator;
+use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
+
+$container = new ContainerBuilder();
+$loader = new YamlFileLoader($container, new FileLocator(__DIR__));
+$loader->load('services.yml');
+
+echo $container->get('app')->hello();
9 example/services.yml
@@ -0,0 +1,9 @@
+services:
+ app:
+ class: App
+ arguments: [@Proxy]
+ proxy:
+ class: Proxy
+ arguments: [@Curl]
+ curl:
+ class: Curl
15 lib/App.php
@@ -0,0 +1,15 @@
+<?php
+class App
+{
+ private $proxy;
+
+ public function __construct(Proxy $proxy)
+ {
+ $this->proxy = $proxy;
+ }
+
+ public function hello()
+ {
+ return $this->proxy->hello();
+ }
+}
8 lib/Curl.php
@@ -0,0 +1,8 @@
+<?php
+class Curl
+{
+ public function doGet()
+ {
+ return "Hello";
+ }
+}
15 lib/Proxy.php
@@ -0,0 +1,15 @@
+<?php
+class Proxy
+{
+ private $curl;
+
+ public function __construct(Curl $curl)
+ {
+ $this->curl = $curl;
+ }
+
+ public function hello()
+ {
+ return $this->curl->doGet();
+ }
+}
35 phpunit.xml
@@ -0,0 +1,35 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!-- http://www.phpunit.de/manual/current/en/appendixes.configuration.html -->
+<phpunit
+ backupGlobals = "false"
+ backupStaticAttributes = "false"
+ colors = "true"
+ convertErrorsToExceptions = "true"
+ convertNoticesToExceptions = "true"
+ convertWarningsToExceptions = "true"
+ processIsolation = "false"
+ stopOnError = "false"
+ stopOnFailure = "false"
+ stopOnIncomplete = "false"
+ stopOnSkipped = "false"
+ syntaxCheck = "true"
+ strict = "false"
+ verbose = "true"
+ debug = "true"
+ bootstrap = "vendor/autoload.php" >
+
+ <testsuites>
+ <testsuite name="tests">
+ <directory>tests/</directory>
+ </testsuite>
+ </testsuites>
+
+ <filter>
+ <whitelist>
+ <directory>tests/</directory>
+ <directory>lib/</directory>
+ </whitelist>
+ </filter>
+
+</phpunit>
35 tests/AllTest.php
@@ -0,0 +1,35 @@
+<?php
+
+use Symfony\Component\DependencyInjection\ContainerBuilder;
+use Symfony\Component\DependencyInjection\Reference;
+use Symfony\Component\Config\FileLocator;
+use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
+
+class AllTest extends \PHPUnit_Framework_TestCase
+{
+ private $container;
+
+ public function setUp()
+ {
+ $container = new ContainerBuilder();
+ $loader = new YamlFileLoader($container, new FileLocator(__DIR__));
+ $loader->load('fixtures/services.yml');
+
+ $this->container = $container;
+ }
+
+ public function testApp()
+ {
+ $this->assertEquals("Hello", $this->container->get('app')->hello());
+ }
+
+ public function testCurl()
+ {
+ $this->assertEquals("Hello", $this->container->get('curl')->doGet());
+ }
+
+ public function testProxy()
+ {
+ $this->assertEquals("Hello", $this->container->get('proxy')->hello());
+ }
+}
9 tests/fixtures/services.yml
@@ -0,0 +1,9 @@
+services:
+ app:
+ class: App
+ arguments: [@Proxy]
+ proxy:
+ class: Proxy
+ arguments: [@Curl]
+ curl:
+ class: Curl
Please sign in to comment.
Something went wrong with that request. Please try again.