jeffpacks / cody
使用PHP代码创建PHP代码的库
1.0.0-alpha.3
2023-10-16 19:14 UTC
Requires
- php: ^7.4|^8.0|^8.1|^8.2
- ext-json: *
Requires (Dev)
- phpunit/phpunit: ^9
This package is auto-updated.
Last update: 2024-09-22 02:21:50 UTC
README
Cody是一个用于生成PHP文件的PHP包。
要求
此库需要PHP >= 7.4。
安装
Composer
- 在您的shell中运行
composer require jeffpacks/cody
下载
如果您已手动下载或接收了jeffpacks/cody
包,您可以将它放在文件系统的任何位置,并在代码中调用require_once('/path/to/cody/autoload.php')
以开始使用它。
概念
Cody允许您创建一个代码"项目",并用PHP类、接口和特性填充它。一旦您的代码创建了项目,项目就可以作为代码库导出到文件系统。
在Cody中,命名空间是创建类、接口、特性和子命名空间的开端。
示例
jeffpacks\cody\Cody::createProject()
方法创建并返回一个新的Project
实例。
<?php require_once('vendor/autoload.php'); # Using Composer to load Cody use jeffpacks\cody\Cody; # Create project $project = Cody::createProject('', 'acme\\webshop'); # Gotta escape them backslashes $projectNamespace = $project->getNamespace(); # This is the project namespace, "acme\webshop" $interfaces = $projectNamespace->createNamespace('interfaces'); # We'll put our interfaces in the "acme\webshop\interfaces" namespace $customer = $interfaces->createInterface('Customer'); $customer->setDescription('Represents a web-shop customer'); $customer->createMethod('getName')->setDescription('Provides the full name of the customer')->setReturnTypes('string'); $customer->createMethod('getAddress')->setDescription('Provides the postal address of the customer')->setReturnTypes('string'); $user = $interfaces->createInterface('User'); $user->setDescription('Represents a web-shop user'); $user->createMethod('getUsername')->setDescription('Provides the username of the user')->setReturnTypes('string'); $user->createMethod('getPasswordHash')->setDescription('Provides the hashed password of the user')->setReturnTypes('string'); $client = $projectNamespace->createClass('Client')->setDescription('Represents a web-shop client'); $client->implement($customer); $client->implement($user); $client->createVariable('name', 'string|null'); # pipe style $client->createVariable('address', '?string'); # nullable style $client->createVariable('username', ['string', 'null']); # array style $client->createVariable('passwordHash', '?string'); $client->getMethod('getName')->setBody('return $this->name;') $client->getMethod('getAddress')->setBody('return $this->address;') $client->getMethod('getUsername')->setBody('return $this->username;') $client->getMethod('getPasswordHash')->setBody('return $this->passwordHash;') $project->export()->toDirectory('/tmp/')->run();
作者
- Johan Fredrik Varen – github.com/jeffpacks
许可
Cody是Johan Fredrik Varen的专有作品和财产。版权所有:Johan Fredrik Varen 2023。