layla/cody

0.2.1 2014-04-16 11:59 UTC

This package is not auto-updated.

Last update: 2024-11-09 13:23:16 UTC


README

Cody is a code generator that generates objects and resources for different languages and frameworks. Cody utilises a very simple configuration format for defining your objects and resources.

The input format

The input format for the generator is as following (example is given in yaml, other formats are supported too)

The root

The root of the input contains the package name the resources that exist within in the package.

package: Vendor.Name
resources:
  __RESOURCES__

Resources

The resources are defined with the names as the key, and the configurations as the value

An example:

Models.User:
  __RESOURCE_CONFIGURATION__
Models.NewsItem
  __RESOURCE_CONFIGURATION__

Resource Configuration

The resource configuration may only contain 2 keys. The compilers key should always be present, it tells Cody what compiler(s) it should use to compile your resource. The second key can be one of the following:

The compiler expects the resource to ONLY contain the compilers property and on of the available types.

An example:

Models.User:
  class:
    __CLASS_CONFIGURATION__
  compilers:
    - php-laravel

Class configuration

The class configuration holds all the information we need to build a class. The available options are

base: MyApp.Foundation.Models.Base
properties:
  rules:
    __PROPERTY_CONFIGURATION__
methods:
  get.rules:
    __METHOD_CONFIGURATION__
  set.rules
    __METHOD_CONFIGURATION__

Model configuration

A model is an extension of the class, it allows you to specify relations and columns, and will automatically add the necesarry methods / properties for you, depending on the compiler.

base: MyApp.Foundation.Models.Base
properties:
  rules:
    __property_configuration
methods:
  get.rules:
    __METHOD_CONFIGURATION__
  set.rules
    __METHOD_CONFIGURATION__
relations:
  __RELATION_CONFIGURATION__
columns:
  __COLUMN_CONFIGURATION__

Method configuration

A method can be added to class resources, or subclasses thereof (models, controllers, etc.)

An example:

body:
  php-core: return $this->rules;
comment: Get the rules for this model
returnType: array
## Property configuration

A property can be added to class resources, or subclasses thereof (models, controllers, etc.)

An example:

value:
  name: required
  email: required|email
comment: The rules for this model
## Relation configuration

A relation can be added to a model resource, or subclasses thereof

An example:

type: hasMany
other: Models.TrailCategory
## Column configuration

A column can be added to a model resource, or subclasses thereof

An example:

type: string
max: 255
nullable: true

Generate from CLI

The generator can take your input file and spit out JSON, or save the files to their calculated destinations. The input can even be a folder, if that's the case, Cody will use the top 2 folders as the package name, and all the folders below indicate the namespace. The files found in the deepest folders represent the resource name, and the contents of the file represent the resource configuration. An example of this setup can be found in vendor/cody/example

./generator generate [--format="yml"] [--save] [--path="."] [--json] [--sync] [path]

Arguments

Options

Generate from PHP

  1. add this following line to the require section in your composer.json

"layla/cody": "dev-master"

  1. run composer update

  2. Register Cody's services by calling the following code

use Layla\Cody\CodyServiceProvider;
use Illuminate\Container\Container as Application;

$app = new Application;
$provider = new CodyServiceProvider($app);
$provider->register();

In case you already have a (compatible) container, you can pass that into the ServiceProvider.

  1. Profit!
$input = array(
  'package' => 'Example.Package',
  'resources' => array(
    'Models.News' => array(
      'model' => array(
        'relations' => array(
          'categories' => array(
            'other' => 'Models.Category'
          )
        )
      ),
      'compilers' => array(
        'laravel-php'
      )
    )
  )
);

$files = $app->make('cody')->compileInput($input);

// Or, if you like your input to be parsed, specify the name of the parser as the second argument (gotta love YAML :)

$input = "
package: Example.Package
resources:
  Models.News:
    model:
      relations:
        categories:
          other: Models.Category
    compilers:
      laravel-php
";

$files = $app->make('cody')->compileInput($input, 'yml');

// Or json if you like

$input = '
{
  "package": "Example.Package",
  "resources": {
    Models."News": {
      "model": {
        "relations": {
          "categories": {
            "other": "Models.Category"
          }
        }
      }
      "compilers": [
        "laravel-php"
      ]
    }
  }
}';

$files = $app->make('cody')->compileInput($input, 'json');

foreach($files as $filename => $content)
{
  // save it, echo it, do whatever you want to do with it
}