LoginSignup

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 5 years have passed since last update.

【Symfony2】yml定義の情報取得

Posted at

今回はymlファイルに定義されている情報を配列で一括取得する手順を紹介。

共通で利用する情報や業務で使われるサービス情報など、ymlファイルなど外部ファイルに定義するのが望ましいのでそこそこ使う頻度はある。以下の3つのステップを踏めば簡単にファイル内に定義されている定義情報を取得することが可能。

定義ファイルの作成

適当なバンドルのResources/config配下に定義ファイルを作成。

今回はservices_def.ymlという名前で定義する。

services_def.yml
series:
    - no: "10"
      service_series: 
    - no: "20"
      service_series:
          - type: "test21"
            code_list: ["100000"]
          - type: "test22"
            code_list: ["100001", "100002"]
          - type: "test23"
            code_list: ["100003", "100004", "100005", "100006"]
    - no: "30"
      service_series: 
          - type: "test31"
            code_list: ["100007"]
          - type: "test32"
            code_list: ["100008", "100009"]

上記定義を読み込めば配列内には[キー:series]で値が格納される。[キー:series]で取得された要素内には3つの配列が定義されている。

DependencyInjectionでファイル情報定義

作成したservices_def.ymlファイルを読み込むためコンテナに定義情報設定。

TestTestExtension.php
namespace Test\TestBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;

class TestTestExtension extends Extension
{
    /**
     * {@inheritdoc}
     */
    public function load(array $configs, ContainerBuilder $container)
    {
        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);

        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('services.yml');

        /**
         * サービス定義情報を定義
         */
        $serviceDefPath = __DIR__ . '/../Resources/config/services_def.yml';
        $container->setParameter('test_service_def', $serviceDefPath);
    }
}

定義情報を読み込みたい場合にはコンテナからtest_service_defの名前で利用することが可能。

定義ファイルを呼び出す

DependencyInjectionで設定したサービス情報をコントローラ内で呼び出す。

以下のロジックではfile_get_contentsで定義ファイルを呼び出し、仮に呼び出せなかった場合は警告メッセージが出力されてしまうので条件文でエラー判定を行う。

DefaultController.php
namespace Test\TestBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Yaml\Parser;

class DefaultController extends Controller
{
    public function executeAction()
    {
        /**
         * 定義ファイル読み込み
         */
        $path = $this->container->getParameter('test_service_def');
        $plainYaml = file_get_contents($path);
        if ($plainYaml === false) {
            //エラー処理
        }

        $yaml = new Parser();
        $serviceDef = $yaml->parse($plainYaml);

        /**
         * 後続処理
         */
    }

}

ファイル情報を読み込めたら、Parserクラスで定義情報をフォーマット。

0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up