jalis/service-generator

Symfony2 Bundle 用于通过命令行创建服务

dev-master 2013-02-28 12:03 UTC

This package is not auto-updated.

Last update: 2024-09-22 03:59:52 UTC


README

此包包含一个可以轻松高效地从命令行创建基本服务的Bundle。

此包增加了一个新的命令行命令 app/console generate:service,用于创建基本服务的所有配置和代码。

  • 修改services.xml(仅适用于xml语言)
  • 创建库类
  • 注入依赖(实体管理器)

安装

步骤 1: 安装依赖

安装依赖于您的Symfony版本

Symfony 2.1.x: Composer

Composer 是PHP项目的依赖管理器。您必须在composer.json文件中列出您的依赖。

{
    "require-dev": {
        "jalis/service-generator": "dev-master"
    }
}

要实际安装Service Generator到您的项目中,下载composer二进制文件并运行它

wget https://getcomposer.org.cn/composer.phar
# or
curl -O https://getcomposer.org.cn/composer.phar

php composer.phar install

Symfony 2.0.x: bin/vendors.php 方法

如果您使用bin/vendors.php方法来管理您的供应商库,请将以下条目添加到项目根目录中的deps

[JalisServiceGeneratorBundle]
    git=https://github.com/javijalis/JalisServiceGeneratorBundle.git
    target=/bundles/Jalis/Bundle/ServiceGeneratorBundle

接下来,运行以下命令来更新您的供应商

$ ./bin/vendors

最后,将以下条目添加到您的自动加载器中

<?php
// app/autoload.php

$loader->registerNamespaces(array(
    // ...
  
    'Jalis'        => __DIR__.'/../vendor/bundles',
));

步骤 2: 启用Bundle

最后,在kernel中启用Bundle

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new Jalis\Bundle\ServiceGeneratorBundle\JalisServiceGeneratorBundle(), 
    );
}

如何使用

执行命令

$ app/console generate:service

按照说明操作

Your service code must be written in Manager directory. This command helps
you generate them easily.

Each service is hosted under a namespace (like Acme/Bundle/BlogBundle).
(which must have Bundle as a suffix).

Bundle namespace: myFolder/Bundle/myBundle

Your service must have a name for call it

Service Name: example

Your service need EntityManager?

Do you need entity Manager in your service [no]? yes

之后,您只需在任何地方使用该服务即可

$my_service = $this->get('exampleManager');

用于服务的代码中的类位于BundleGiven/Manager/exampleManager.php文件夹中

<?php
namespace BundleGiven\Manager;                                                                                                                                                                                                                                             
                                                                                
use Doctrine\ORM\EntityManager;                                                 
                                                                                                                                                                                                                                          
class exampleManager                                                               
{                                                                               
    protected $em;                                                              
                                                                                
    public function __construct(EntityManager $em){                             
                                                                                
        $this->em = $em;                                              
    }                                                                  
                                                                                                                                                            
    public function getInfo() {                                                 
                                                                      
        return "name: exampleManager";                                                                                                                          
    }                                                                           
    
    //... your code
                                                                                
}             

待办事项

  • 创建Twig扩展的选项
  • 添加更多选项以注入更多原生服务(monolog,mailer)和自定义服务
  • 重构Command类代码
  • yml中的服务怎么办?