facebook twitter hatena line email

「Php/pimple」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(サンプル)
(サンプル)
 
(同じ利用者による、間の6版が非表示)
行13: 行13:
 
$ php composer.phar install
 
$ php composer.phar install
  
==サンプル==
+
==DLされたPimple V3系==
 +
vendor/pimple/pimple/src/
 +
└── Pimple
 +
    ├── Container.php
 +
    ├── Exception
 +
    │   ├── ExpectedInvokableException.php
 +
    │   ├── FrozenServiceException.php
 +
    │   ├── InvalidServiceIdentifierException.php
 +
    │   └── UnknownIdentifierException.php
 +
    ├── Psr11
 +
    │   ├── Container.php
 +
    │   └── ServiceLocator.php
 +
    ├── ServiceIterator.php
 +
    ├── ServiceProviderInterface.php
 +
    └── Tests
 +
        ├── Fixtures
 +
        │   ├── Invokable.php
 +
        │   ├── NonInvokable.php
 +
        │   ├── PimpleServiceProvider.php
 +
        │   └── Service.php
 +
        ├── PimpleServiceProviderInterfaceTest.php
 +
        ├── PimpleTest.php
 +
        ├── Psr11
 +
        │   ├── ContainerTest.php
 +
        │   └── ServiceLocatorTest.php
 +
        └── ServiceIteratorTest.php
 +
 
 +
==DIサンプル==
 
$ vi ServiceInterface.php
 
$ vi ServiceInterface.php
 
  namespace App\Services;
 
  namespace App\Services;
行29: 行56:
 
             return "hoge";
 
             return "hoge";
 
         }
 
         }
 +
}
 +
 +
$ vi Client.php
 +
namespace App\Services;
 +
class Client
 +
{
 +
    private $service;
 +
    public function __construct(ServiceInterface $service)
 +
    {
 +
        $this->service = $service;
 +
    }
 +
    public function doSomething()
 +
    {
 +
        return $this->service->doSomething();
 +
    }
 
  }
 
  }
  
行36: 行78:
 
  class PimpleexaController extends Controller
 
  class PimpleexaController extends Controller
 
  {
 
  {
        public function index()
+
    public function index()
        {
+
    {
            $container = new Container();
+
        $container = new Container();
            $container['service'] = function ($container) {
+
        $container['service'] = function ($container) {
                $service = new Service();
+
            $service = new Service();
                return $service;
+
            return $service;
            };
+
        };
             $service = $container['service'];
+
        $container['client'] = function ($container) {
             echo $service->doSomething(); // hoge
+
             $client = new Client($container['service']);
        }
+
             return $client;
 +
        };
 +
        $client = $container['client'];
 +
        echo $client->doSomething(); // hoge
 +
    }
 
  }
 
  }
 +
 +
==サービスロケータはアンチパターン==
 +
$container変数をconstructの引数に渡す組み込み方法をサービスロケータというが
 +
これはアンチパターン
 +
 +
==参考==
 +
http://blog.a-way-out.net/blog/2015/08/31/your-dependency-injection-is-wrong-as-I-expected/

2017年9月9日 (土) 23:04時点における最新版

pimpleとは

DIコンテナのphpライブラリ

composerによるインストール

$ vi composer.json

{
   "require": {
       "pimple/pimple": "v3.*.*"
   }
}

>Installing pimple/pimple (v3.2.2)

$ php composer.phar install

DLされたPimple V3系

vendor/pimple/pimple/src/
└── Pimple
   ├── Container.php
   ├── Exception
   │   ├── ExpectedInvokableException.php
   │   ├── FrozenServiceException.php
   │   ├── InvalidServiceIdentifierException.php
   │   └── UnknownIdentifierException.php
   ├── Psr11
   │   ├── Container.php
   │   └── ServiceLocator.php
   ├── ServiceIterator.php
   ├── ServiceProviderInterface.php
   └── Tests
       ├── Fixtures
       │   ├── Invokable.php
       │   ├── NonInvokable.php
       │   ├── PimpleServiceProvider.php
       │   └── Service.php
       ├── PimpleServiceProviderInterfaceTest.php
       ├── PimpleTest.php
       ├── Psr11
       │   ├── ContainerTest.php
       │   └── ServiceLocatorTest.php
       └── ServiceIteratorTest.php

DIサンプル

$ vi ServiceInterface.php

namespace App\Services;
interface ServiceInterface
{
    public function doSomething();
}

$ vi Service.php

namespace App\Services;
class Service implements ServiceInterface
{
       public function doSomething()
       {
            return "hoge";
       }
}

$ vi Client.php

namespace App\Services;
class Client
{
    private $service;
    public function __construct(ServiceInterface $service)
    {
        $this->service = $service;
    }
    public function doSomething()
    {
        return $this->service->doSomething();
    }
}

$ vi PimpleexaController.php

use Pimple\Container;
use App\Services\Service;
class PimpleexaController extends Controller
{
    public function index()
    {
        $container = new Container();
        $container['service'] = function ($container) {
            $service = new Service();
            return $service;
        };
        $container['client'] = function ($container) {
            $client = new Client($container['service']);
            return $client;
        };
        $client = $container['client'];
        echo $client->doSomething(); // hoge
    }
}

サービスロケータはアンチパターン

$container変数をconstructの引数に渡す組み込み方法をサービスロケータというが これはアンチパターン

参考

http://blog.a-way-out.net/blog/2015/08/31/your-dependency-injection-is-wrong-as-I-expected/