https://github.com/ornicar/foqtyperbundle
Generate PHP classes from YAML templates
https://github.com/ornicar/foqtyperbundle
Last synced: 2 months ago
JSON representation
Generate PHP classes from YAML templates
- Host: GitHub
- URL: https://github.com/ornicar/foqtyperbundle
- Owner: ornicar
- Created: 2011-03-26T01:55:05.000Z (about 14 years ago)
- Default Branch: master
- Last Pushed: 2011-06-21T17:24:47.000Z (almost 14 years ago)
- Last Synced: 2025-03-19T02:11:35.711Z (2 months ago)
- Language: PHP
- Size: 101 KB
- Stars: 19
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.markdown
Awesome Lists containing this project
README
Disclaimer: this bundle is about typing less. Your keyboard will thank you.
## Assumptions
1. Symfony2 is the glorification of Object Oriented Programming.
A popular pattern, called [Single responsibility](http://en.wikipedia.org/wiki/Single_responsibility_principle),
states that each class should have only one purpose.2. It also means that for every single purpose you must write a new class.
3. If you follow this pattern (you should!) you will end up with lots of classes.
4. Classes are verbose. They have a namespace, uses, extends, implements, properties, constructors, getters and setters.
Oh, all these things have phpDoc and comments.5. You are lazy and your fingers are tired.
## Installation
### Add TyperBundle to your src/ dir
$ git submodule add git://github.com/ornicar/TyperBundle.git src/FOQ/TyperBundle
### Add the FOQ namespace to your autoloader
// app/autoload.php
$loader->registerNamespaces(array(
'FOQ' => __DIR__.'/../src',
// your other namespaces
);### Add TyperBundle to your application kernel
// app/AppKernel.php
public function registerBundles()
{
return array(
// ...
new FOQ\TyperBundle\FOQTyperBundle(),
// ...
);
}## Code generation
TyperBundle works by converting a YAML template to PHP code.
### 1. Get a template
First, create a php file containing the yaml template:
$ app/console typer:template > src/Vendor/CookBundle/Toaster.php
src/Vendor/CookBundle/Toaster.php now contains
name:
comment:
extends:
implements: []
properties:
foo:
type:
visibility: protected
comment:
default: null
getter: true
setter: true
construct: true### 2. Fill the YAML template
Complete the template as if it was a form.
Only `name` is required, you can remove the rest if you don't need it.Example of simple template:
name: Vendor\CookBundle\Toaster
comment: Takes a donnut and returns it toastedExample of more complete template:
name: Vendor\CookBundle\Toaster
comment: Takes a donnut and returns it toasted
extends: Vendor\MachineBundle\Electrical
implements: [ Vendor\CookBundle\ToasterInterface, ArrayAccess ]
properties:
duration:
type: int
visibility: private
comment: Toasting duration in seconds
default: 20
getter: true
setter: true
construct: true
timer:
type: Vendor\MachineBundle\Timer
comment: Timer used to measure the toasting duration
construct: true### 3. Generate the PHP code
$ app/console typer:generate src/Vendor/CookBundle/Toaster.php
It will read the YAML configuration from the file, and replace it with PHP code.
The first example of simple template will result to:
duration = $duration;
$this->timer = $timer;
}/**
* Gets: Toasting duration in seconds
*
* @return int duration
*/
public function getDuration()
{
return $this->duration;
}/**
* Sets: Toasting duration in seconds
*
* @param int duration
*/
public function setDuration($duration)
{
$this->duration = $duration;
}}
## Custom templates
`app/console typer:template` gives you a default template.
You are encouraged to create yours, that match your projects needs.
Save the template anywhere, then load it to your file!