Skip to content
shama edited this page Aug 17, 2012 · 22 revisions

Note This wiki page reflect my thoughts and don't indicate changes that will actually happen in CakePHP.

Cake Bake

The baker is one the of things that originally attracted me to CakePHP. As useful as the tool is, I use it surprisingly very little; mostly for baking fixtures. I find it more work to remove the unwanted code baked from the core templates; so I build from scratch. I could build custom templates but I find that process to be a bit cumbersome and not mutable.

Problems

  • Bake templates are cumbersome
  • Baking cannot be done programmatically
  • You can only bake once... cannot rebake
  • Bake CLI is exhaustive
  • cough cough tests!

Ideas

Represent Code with Arrays

Why arrays? They're flexible and CakePHP is already good at working with them. They can easily be created from other formats: JSON, YAML, DataSources, etc.

An example array to create a controller could be:

$controller = array(
  'class' => array(
    'name' => 'PagesController',
    'extends' => 'AppController',
    'docblock' => array('Pages Controller', '', '@package CakePHP'),
  ),
  'properties' => array(
    'paginate' => array(
      'access' => 'public',
      'value' => array(
        'limit' => 25,
        'order' => array('Post.title' => 'asc'),
      ),
    ),
  ),
  'methods' => array(
    'admin_index' => array(
      'access' => 'public',
      'parameters' => array(
        array('name' => 'id', 'default' => null, 'type' => 'integer'),
      ),
      'value' => "$this->set('data', $this->paginate());",
    ),
  ),
);

Now the template can be manipulated and tested programmatically.

Rebaking

If code is represented as an array then it can be manipulated as an array. Such as with Hash::merge(). This affords re-baking. This allows a user to bake a class, modify that class then bake again without overwriting the changes. New functionality can be baked into old code. A single class can be built by merging multiple templates.

Skeleton Templates

App skeletons could also be represented with arrays:

$app = array(
  'Config' => array(
    'Schema',
  ),
  'Controller' => array(
    'Component',
    'PagesController.php' => array(/* Code Array Here? */),
  ),
  'Model' => array(
    'Behavior',
    'Page.php' => array(/* Code Array Here? */),
  ),
);

Testable

Using the above methods enables bake templates and skeletons to become testable.

Bake API

Using a programatic method over flat file templates and folder structures allows bake to become an API. Users could then utilize and extend it.

Baking Traits

As @lorenzo pointed out in IRC: baking PHP 5.4 traits would be ideal. Traits would be a much cleaner method than relying solely on arrays. Using arrays to bake traits would still be useful as the traits could be rebaked and more easily tested.

An example array to bake a trait could be:

$trait = array(
  'trait' => array(
    'name' => 'PageTrait',
    'docblock' => array('Page Trait', '', '@package CakePHP'),
  ),
  'properties' => array(
    'paginate' => array(
      'access' => 'public',
      'value' => array(
        'limit' => 25,
        'order' => array('Post.title' => 'asc'),
      ),
    ),
  ),
  'methods' => array(
    'admin_index' => array(
      'access' => 'public',
      'parameters' => array(
        array('name' => 'id', 'default' => null, 'type' => 'integer'),
      ),
      'value' => "$this->set('data', $this->paginate());",
    ),
  ),
);

and the controller would become:

$controller = array(
  'class' => array(
    'name' => 'PagesController',
    'extends' => 'AppController',
    'docblock' => array('Pages Controller', '', '@package CakePHP'),
  ),
  'use' => array('PageTrait'),
);

Config File Alternative to CLI

Using arrays would also allow a user to create a config file outlining how they would like their app baked. Then issue a single command like: cake bake Config/Bake/template.json As opposed to responding to the interactive questions or know the exhaustive list of commands to run.

Clone this wiki locally