Generates a controllers, models, and views using database tables.
Combustor is a utility package for Codeigniter 3 that generates controllers, models, and views based on the provided database tables. It uses the Describe package for getting columns from a database table and as the basis for code generation.
Codeigniter 3 framework;Combustor will do the rest.Extract the contents of the latest Codeigniter 3 project first:
$ wget https://github.com/bcit-ci/CodeIgniter/archive/3.1.13.zip
$ unzip 3.1.13.zip -d ciacmeThen configure the project's database connectivity settings:
$ cd ciacme
$ nano application/config/database.php// ciacme/application/config/database.php
// ...
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => '',
'password' => '',
'database' => '',
'dbdriver' => 'mysqli',
// ...
);Next is to proceed in installing Combustor via Composer:
$ composer require rougin/combustor --dev// ciacme/composer.json
{
// ...
"require-dev":
{
"mikey179/vfsstream": "1.6.*",
"phpunit/phpunit": "4.* || 5.* || 9.*",
"rougin/combustor": "~1.0"
}
}Lastly, install the ORM wrappers like Wildfire or Doctrine:
$ vendor/bin/combustor install:wildfire
$ vendor/bin/combustor install:doctrine[!NOTE] Using the
install:wildfirecommand installs the Wildfire package while theinstall:doctrineinstalls the Credo package.
Prior in executing any commands, kindly ensure that the database tables are defined properly (foreign keys, indexes, relationships, normalizations) in order to minimize the modifications after the code structure has been generated.
Also, please proceed first in generating models, views, or controllers to database tables that are having no relationship with other tables in the database.
[!TIP]
Combustorwill generate controllers, models, or views based on the specified database schema. If there's something wrong in the specified database schema,Combustorwill generate a bad codebase.
create:layoutCreate a new header and footer file.
Options
--bootstrap - adds styling based on Bootstrap--force - generates file/s even they already existsExample
$ vendor/bin/combustor create-layout --bootstrapcreate:controllerCreate a new HTTP controller.
Arguments
table - name of the database tableOptions
--doctrine - generates a Doctrine-based controller--wildfire - generates a Wildfire-based controller--empty - generates an empty HTTP controller--force - generates file/s even they already exists[!NOTE] If either
WildfireorDoctrineis installed, no need to specify it as option for executing a specified command (e.g.--wildfire). However if both are installed, a command must have a--wildfireor--doctrineoption added.
Example
$ vendor/bin/combustor create:controller users --wildfirecreate:modelCreate a new model.
Arguments
table - name of the database tableOptions
--doctrine - generates a Doctrine-based model--wildfire - generates a Wildfire-based model--empty - generates an empty model--force - generates file/s even they already existsExample
$ vendor/bin/combustor create:model users --wildfirecreate:repositoryCreate a new entity repository.
Arguments
table - name of the database tableOptions
--force - generates file/s even they already existsExample
$ vendor/bin/combustor create:repository users[!NOTE] This command is only applicable to a Doctrine implementation.
create:viewCreate view templates.
Arguments
table - name of the database tableOptions
--bootstrap - adds styling based on Bootstrap--doctrine - generates Doctrine-based views--wildfire - generates Wildfire-based views--force - generates file/s even they already existsExample
$ vendor/bin/combustor create:view users --bootstrapcreate:scaffoldCreate a new HTTP controller, model, and view templates.
Arguments
table - name of the database tableOptions
--bootstrap - adds styling based on Bootstrap--doctrine - generates a Doctrine-based controller, model, and views--wildfire - generates a Wildfire-based controller, model, and views--force - generates file/s even they already existsExample
$ vendor/bin/combustor create:scaffold users --bootstrap --wildfire[!NOTE] If
--doctrineis selected, the command will also execute thecreate:repositorycommand.
install:doctrineInstall the Doctrine package.
Example
$ vendor/bin/combustor install:doctrine[!NOTE]
- This command will be available if
Doctrineis not installed in the project.- It also adds a
Loader.phpin thecoredirectory. The said file is used for loading custom repositories extended toEntityRepository.
install:wildfireInstall the Wildfire package.
Example
$ vendor/bin/combustor install:wildfire[!NOTE] This command will be available if
Wildfireis not installed in the project.
remove:doctrineRemove the Doctrine package.
Example
$ vendor/bin/combustor remove:doctrine[!NOTE] This command will be available if
Doctrineis installed in the project.
remove:wildfireRemove the Wildfire package.
Example
$ vendor/bin/combustor remove:wildfire[!NOTE] This command will be available if
Wildfireis installed in the project.
combustor.ymlCombustor currently works out of the box after the configuration based on Installation. However, using a combustor.yml can be used for complex setups like specifying the new application path and excluding columns:
# combustor.yml
app_path: %%CURRENT_DIRECTORY%%/Sample
excluded_fields:
- created_at
- updated_at
- deleted_atTo create a combustor.yml, simply run the initialize command:
$ vendor/bin/combustor initialize
[PASS] "combustor.yml" added successfully!app_pathThis property specifies the application directory. It may updated to any directory (e.g., ciacme/application, ciacme/config, etc.) as long it can detect the config/config.php file from the defined directory:
# combustor.yml
app_path: %%CURRENT_DIRECTORY%%/Sample
# ...[!NOTE]
Combustorwill try to check the path specified inapp_pathif it is a validCodeigniter 3project. Then it will perform another check if theapplicationdirectory exists or if theconfigdirectory can be accessed directly from the directory defined inapp_path.
excluded_fieldsSpecified fields in this property are excluded from generation to the following templates:
controllersmodelsviews (only for create and edit templates)# combustor.yml
# ...
excluded_fields:
- created_at
- updated_at
- deleted_at[!NOTE] The timestamps are added by default when creating a
combustor.ymlfor the first time as they are usually populated automatically by installed ORMs such asWildfireorDoctrine.
custom_fieldsBy default, all of the fields generated by Combustor to create and edit pages will use the form_input helper:
<div class="mb-3">
<?= form_label('Email', '', ['class' => 'form-label mb-0']) ?>
<?= form_input('email', set_value('email'), 'class="form-control"') ?>
<?= form_error('email', '<div><span class="text-danger small">', '</span></div>') ?>
</div>However, some fields like email and boolean types may need to use other form helpers:
<div class="mb-3">
<?= form_label('Email', '', ['class' => 'form-label mb-0']) ?>
// Still using form_input, but the type is "email" instead
<?= form_input(['type' => 'email', 'name' => 'email', 'value' => set_value('email'), 'class' => 'form-control']) ?>
<?= form_error('email', '<div><span class="text-danger small">', '</span></div>') ?>
</div><div class="mb-3">
<?= form_label('Admin', '', ['class' => 'form-label mb-0']) ?>
// Use "form_checkbox" for boolean-based data types
<div>
<?= form_checkbox('admin', true, set_value('admin'), 'class="form-check-input"') ?>
</div>
<?= form_error('admin', '<div><span class="text-danger small">', '</span></div>') ?>
</div>To achieve this, Combustor provides a utility for handling specified field names or data types using custom_fields:
# combustor.yml
# ...
custom_fields:
- Rougin\Combustor\Template\Fields\BooleanFieldWhen adding a custom field, kindly create a class that extends to the Colfield class:
namespace Acme\Fields;
use Rougin\Combustor\Colfield;
class EmailField extends Colfield
{
protected $class = 'form-control';
/**
* If $name is specified, it will check if the current field
* name matches the in this $name field.
*/
protected $name = 'email';
public function getPlate()
{
$field = $this->accessor;
$class = $this->getClass();
/** @var string */
$name = $this->getName();
$html = '<?= form_input([\'type\' => \'email\', \'name\' => \'' . $name . '\', \'value\' => set_value(\'' . $name . '\')]) ?>';
if ($this->edit)
{
$html = str_replace('set_value(\'' . $name . '\')', 'set_value(\'' . $name . '\', ' . $field . ')', $html);
}
$html = str_replace(')]) ?>', '), \'class\' => \'' . $class . '\']) ?>', $html);
return array($html);
}
}Then after creating the custom field, simply add the class name to the combustor.yml:
# combustor.yml
# ...
custom_fields:
- Rougin\Combustor\Template\Fields\BooleanField
- Acme\Fields\EmailField