Symfony2 バンドルの作成

2011.11.06 21:20
 Symfony2の肝の部分がバンドルという概念です。バンドルは、いわゆるモジュールでありプラグインでありという1つのプログラムの塊です。様々な機能をもたせようとプログラムを組む際に概念的に最も多きな単位となるのがこのバンドルというものです。まず最初にバンドルを作成しないと何もできないということになります。
  バンドルは名前空間ごとに分けられていて、それらの機能はincludeやrequireなどを気にせずとも自動的に読み込んでくれます。作成した機能そのそのものはphpの機能としてしかるべき呼び出し方とアクセスをすれば動いてくれるというという優れものです。
 公式ページでも詳しいドキュメントがあります。
 http://docs.symfony.gr.jp/sf2-blog-tutorial/03-create-bundle.html
 
 

バンドルを作成する

  バンドルの作成は、Symfony2のフレームワークにすでに用意されているコマンドを使うと一発で作成可能です。端末、あるいはSSHから接続して以下のコマンドを実行します。
 
$ cd /home/user/public_html/Symfony/
$  php app/console generate:bundle --namespace=TEST/HelloBundle --format=yml

 まずSymfonyのあるディレクトリに移動します。Symfonyのあるディレクトリのトップに移動しないと正しくバンドルが作成できない場合があります。使用するコマンドそのものが、app/consoleにありますのでパスを合わせる必要があるからです。
 実行するコマンドは、
php app/console generate:bundle --namespace=<任意の名前空間(*1)>/<バンドル名(*2)>Bundle --format=<設定ファイルのフォーマット形式(*3)>
となっています。
 コマンドを実行すると対話形式になっていて幾つか質問されます。今回は細かい設定をしないでSymfonyのデフォルトの設定に任せるのですべての項目でEnterを押して最後まで進んでください。

$ php app/console generate:bundle --namespace=TEST/HelloBundle --format=yml

                                          
  Welcome to the Symfony2 bundle generator
                                          


Your application code must be written in bundles. This command helps
you generate them easily.

Each bundle is hosted under a namespace (like Acme/Bundle/BlogBundle).
The namespace should begin with a "vendor" name like your company name, your
project name, or your client name, followed by one or more optional category
sub-namespaces, and it should end with the bundle name itself
(which must have Bundle as a suffix).

Use / instead of \ for the namespace delimiter to avoid any problem.

Bundle namespace [TEST/HelloBundle]:[構わずEnter]

In your code, a bundle is often referenced by its name. It can be the
concatenation of all namespace parts but it's really up to you to come
up with a unique name (a good practice is to start with the vendor name).
Based on the namespace, we suggest TESTHelloBundle.

Bundle name [TESTHelloBundle]:[構わずEnter]

The bundle can be generated anywhere. The suggested default directory uses
the standard conventions.

Target directory [/home/omnioo/www/Symfony/src]:[構わずEnter]

Determine the format to use for the generated configuration.

Configuration format (yml, xml, php, or annotation) [yml]:[構わずEnter]

To help you getting started faster, the command can generate some
code snippets for you.

Do you want to generate the whole directory structure [no]?[構わずEnter]

                           
  Summary before generation
                           

You are going to generate a "TEST\HelloBundle\TESTHelloBundle" bundle
in "/home/omnioo/www/Symfony/src/" using the "yml" format.

Do you confirm generation [yes]?[構わずEnter]

                   
  Bundle generation
                   

Generating the bundle code: OK
Checking that the bundle is autoloaded: OK
Confirm automatic update of your Kernel [yes]?[構わずEnter]
Enabling the bundle inside the Kernel: OK
Confirm automatic update of the Routing [yes]?[構わずEnter]
Importing the bundle routing resource: OK

                                             
  You can now start using the generated code! 


任意の名前空間(*1)

 ./src/以下にバンドル格納専用のディレクトリが作成されます。この名前空間はそのディレクトリ名とその階層と同様の意味を持っています。要はSymfony2のフレームワークではバンドルに関しては名前空間とディレクトリ構造を全く同じにするという規則があるというわけです。任意の名前空間を適当なディレクトリ階層にばらまくことはできますが、それはちょっと不合理ということでここではきちんとまとめてあるのです。ですので、ここで設定された名前空間は、物理的なパス./src/<任意の名前空間(*1)>というディレクリに相当することになります。

バンドル名(*2)

 バンドル名は自身が作成する機能に関して類推できる名称がよいです。上記の名前空間で仕切られていることからほぼ自由に命名できることになります。このバンドル名は、付けた名前にBundleという文字を付け加えないといけないルールになっています。これは実によいアイディアで、ファイル名、ディレクトリ名を勝手気ままに付けてしますチーム内の人間に強制的にルールを敷くことができるわけです。Bundleという名称でもってそれがバンドルであると判断できるからです。また物理的なパスも決まっています。
./src/<任意の名前空間(*1)>/<バンドル名(*2)>Bundle
という風になっているので、ここを探せばバンドルがあるともうすでにルールで決まっているのです。さすがフレームワークだ!

設定ファイルのフォーマット形式(*3)

 あなたがプログラムを作成する際になんらかの設定ファイルを作成したことはあるでしょう。私はたくさんあります。.csv .txt .cgi .yml .php .xml 様々なテキストフォーマットの形式があり、それぞれに便利です。しかし設定ファイル形式はいずれにしろ統一しないと管理が大変になってしまいます。ですのでSymfony2では.yml .xml .phpに限定しています。このいずれかの設定ファイルフォーマットを選択できます。ymlは最近では様々なプログラムで使われています。平文のテキストでperlやRubyでも好んで使われています。xmlは皆様知っての通り。phpはphpなのでphpでのみ使えるファイル形式です。特に拡張性がないという場合にはphpファイルでの設定でもよろしいかと思います。
 ここではymlを設定ファイルに選択しています。

$  php app/console generate:bundle --namespace=TEST/HelloBundle --format=yml
を実行すると、
./src/TEST
└── HelloBundle
    ├── Controller
    │   └── DefaultController.php
    ├── DependencyInjection
    │   ├── Configuration.php
    │   └── TESTHelloExtension.php
    ├── Resources
    │   ├── config
    │   │   ├── routing.yml
    │   │   └── services.yml
    │   └── views
    │       └── Default
    │           └── index.html.twig
    ├── TESTHelloBundle.php
    └── Tests
        └── Controller
            └── DefaultControllerTest.php
           
というバンドルのファイル群が自動的に生成されます。そしてこれがバンドルの実体であり実装となるわけです。


 





プロフィール



  • Name :: 山上オサム ♂(39)
  • Hobby :: 武術
  • Work :: Web Designer