eclipsegc / classiphpy
一个用于从各种来源生成PHP类的PHP库。
dev-master
2015-02-08 16:22 UTC
Requires
- php: >=5.4.0
- grom358/pharborist: dev-master
Requires (Dev)
- phpunit/phpunit: ~4.4
This package is auto-updated.
Last update: 2024-08-29 03:31:26 UTC
README
Classiphpy是一个PHP库,旨在读取定义数据并生成相应的类结构。Classiphpy将此过程视为定义接口和处理器接口。定义接口描述了解析数据的预期,验证这些数据并提供一个定义接口对象的数组。处理器接口解释定义接口中生成的定义,并生成匹配的文件类数据。应将个别用例描述为1个或多个定义接口类,以便准备数据供处理。处理期间生成的文件定义可通过DefaultOutput类写入磁盘,或者您可以编写另一个Output类,以满足非文件写入用例的需求。
##示例 Classiphpy中包含的DefaultDefinition类期望输入如下
<?php $data = [ "defaults" => [ "namespace" => "EclipseGc" ], "classes" => [ "Person" => [ "properties" => [ "first" => [ 'type' => 'string', 'description' => 'The first name of the person.', ], "last" => [ 'type' => 'string', 'description' => 'the last name of the person.', ] ], "namespace" => "EclipseGc\\Person", ], "Animal" => [ "properties" => [ "kingdom" => [ 'type' => 'string', 'description' => 'The kingdom to which the animal belongs.' ], "phylum" => [ 'type' => 'string', 'description' => 'The phylum to which the animal belongs.' ], "genus" => [ 'type' => 'string', 'description' => 'The genus to which the animal belongs.' ], "species" => [ 'type' => 'string', 'description' => 'The species to which the animal belongs.' ], ], "namespace" => "EclipseGc\\Animal", ] ] ];
通过PSR4PhpProcessor类运行,此JSON将生成以下PHP。
<?php namespace EclipseGc\Animal; class Animal { /** * @var string species * The species to which the animal belongs. */ protected $species; /** * @var string genus * The genus to which the animal belongs. */ protected $genus; /** * @var string phylum * The phylum to which the animal belongs. */ protected $phylum; /** * @var string kingdom * The kingdom to which the animal belongs. */ protected $kingdom; /** * @param string kingdom * The kingdom to which the animal belongs. * * @param string phylum * The phylum to which the animal belongs. * * @param string genus * The genus to which the animal belongs. * * @param string species * The species to which the animal belongs. */ public function __construct($kingdom, $phylum, $genus, $species) { $this->kingdom = $kingdom; $this->phylum = $phylum; $this->genus = $genus; $this->species = $species; } /** * Gets the kingdom value. */ public function getKingdom() { return $this->kingdom; } /** * Gets the phylum value. */ public function getPhylum() { return $this->phylum; } /** * Gets the genus value. */ public function getGenus() { return $this->genus; } /** * Gets the species value. */ public function getSpecies() { return $this->species; } }
##入门示例 在上面的示例中,要实际启动此过程,我们只需通知Classiphpy类我们希望使用哪个定义(们),并给它传递我们的Output类的一个实例。然后,我们实例化我们选择的处理器(这些可以使用装饰器模式嵌套,以添加额外的文件,而不仅仅是定义接口类预期的文件)并Classiphpy生成文件结构和将它们写入磁盘。
<?php $output = new \Classiphpy\Output\DefaultOutput('/tmp/classiphpy'); $test = new Classiphpy(['\Classiphpy\Definition\DefaultDefinition'], $output); $processor = new \Classiphpy\Processor\PSR4PhpProcessor('src'); $test->build($data, $processor); ?>