thuata/intercessionbundle

帮助代码生成的Bundle

安装: 75

依赖者: 1

建议者: 0

安全性: 0

星标: 0

关注者: 2

分支: 0

开放问题: 0

类型:symfony-bundle

v1.1.3 2016-12-06 13:12 UTC

README

Build Status Codeship

干预原则

干预是程序修改其执行状态的能力。这意味着干预允许在程序执行过程中生成并执行一些代码。

干预Bundle旨在生成类定义,并将其写入文件。然后您可以自由地包含/要求该文件,以便在新生成的类可用。该Bundle还提供工具以添加phpdoc。

警告

请注意,代码生成如果不安全地使用,可能会非常危险并损害您的应用程序。您应该只使用您理解的代码进行干预。使用不安全的代码或不确定来源的代码(例如表单请求中的代码)应在特定环境中(例如容器或虚拟系统)进行。

意识到干预可以是一个非常强大的工具。

安装

步骤1:下载Bundle

在您的composer.json文件中添加以下行

{
    "require": {
      // ...
        "thuata/intercessionbundle": "^1",
      // ...
    }
}

或直接使用Composer在命令行中

composer require thuata/intercessionbundle

此命令需要您已全局安装Composer,如Composer文档中的安装章节中所述。

步骤2:启用Bundle

然后,通过将其添加到项目中app/AppKernel.php文件中注册的Bundle列表中来启用该Bundle

<?php
// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...

            new Thuata\IntercessionBundle\ThuataIntercessionBundle,
        );

        // ...
    }

    // ...
}

使用方法

干预Bundle提供了一些类来准备类、方法和属性定义,并提供了一个用于生成定义并将其/写入文件的服务。

以下是一个简单的示例,它在一个命名空间中定义了一个类。该类继承自另一个类,实现了某些接口并使用了某些特性。它有一个描述和作者(用于phpdoc)

        $class = new IntercessionClass(); // instanciates the intercession class

        $class->setName('Foo'); // sets the class name ...
        $class->setNamespace('Bar/Baz'); // ... and the namespace
        $class->addAuthor('Anthony Maudry', 'anthony.maudry@thuata.com'); // the author (appears in phpdoc)
        $class->setDescription('The marvelous Foo class !'); // and a description (php doc too)
        $class->addInterface('\Bar\FooBarInterface'); // First interface to implement
        $class->addInterface('\Bar\BarFooInterface'); // and the second interface
        $class->addTrait('\Bar\FooBarTrait'); // one trait
        $class->addTrait('\Bar\BarFooTrait'); // another trait
        $class->setExtends('\Bar\AbstractFooBar'); // Foo class now extends another class
        
        /** @var GeneratorService $generator */
        $generator = $container->get('thuata_intercession.generator'); // get the generator from the conatainer
        // get the definition as string ...
        $definition = $generator->renderClass($class);
        // or write it directly in a file
        $fileName = '/some/path/to/a/file.php';
        $generator->createClassDefinitionFile($class, $fileName);
        
        // you can now include that file :
        include_once($fileName);
        // and instanciate your class :
        $foo = new \Bar\Baz\Foo();

更完整的文档即将推出。

下一步

这个版本是1.0。它仅提供定义生成。

下一步将提供

  • 完整的文档
  • 即使实例化后也能实时编辑类