Skip to content

Commit

Permalink
Add "custom_fields" in "combustor.yml"
Browse files Browse the repository at this point in the history
  • Loading branch information
rougin committed Oct 12, 2024
1 parent 26744fc commit 1e4113b
Show file tree
Hide file tree
Showing 14 changed files with 360 additions and 43 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ All notable changes to `Combustor` will be documented in this file.

### Added
- Command for creating `combustor.yml` configuration file
- Custom fields in templates using `custom_fields`
- Fields that can be excluded through `excluded_files`
- Specify customized `application` path using `app_path`
- `create:repository` for creating Entity Repositories
Expand Down
99 changes: 98 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,104 @@ excluded_fields:
```

> [!NOTE]
> By default, the timestamps are added when creating a `combustor.yml` for the first time as they are usually populated automatically by installed ORMs such as `Wildfire` or `Doctrine`.
> The timestamps are added by default when creating a `combustor.yml` for the first time as they are usually populated automatically by installed ORMs such as `Wildfire` or `Doctrine`.

### `custom_fields`

By default, all of the fields generated by `Combustor` to `create` and `edit` pages will use the `form_input` helper:

``` php
<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:

``` php
<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>
```

``` php
<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`:

``` yaml
# combustor.yml
# ...
custom_fields:
- Rougin\Combustor\Template\Fields\BooleanField
```

When adding a custom field, kindly create a class that extends to the `Colfield` class:

``` php
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`:

``` yaml
# combustor.yml
# ...
custom_fields:
- Rougin\Combustor\Template\Fields\BooleanField
- Acme\Fields\EmailField
```

## Changelog

Expand Down
64 changes: 54 additions & 10 deletions src/Colfield.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,44 @@ class Colfield
*/
protected $name = null;

/**
* @var boolean
*/
protected $styling = false;

/**
* @var string
*/
protected $tab = '';

/**
* @var string|null
*/
protected $type = null;

/**
* @param boolean $edit
* @param string $tab
*
* @return self
*/
public function __construct($edit = false, $tab = '')
public function asEdit($edit = true)
{
$this->edit = $edit;

$this->tab = $tab;
return $this;
}

/**
* @return string|null
*/
public function getClass()
{
if ($this->styling)
{
return $this->class;
}

return null;
}

/**
Expand All @@ -63,38 +87,58 @@ public function getPlate()
return array();
}

/**
* @return string|null
*/
public function getType()
{
return $this->type;
}

/**
* @param string $accessor
*
* @return self
*/
public function withAccessor($accessor)
public function setAccessor($accessor)
{
$this->accessor = $accessor;

return $this;
}

/**
* @param string $class
* @param string $name
*
* @return self
*/
public function withClass($class)
public function setName($name)
{
$this->class = $class;
$this->name = $name;

return $this;
}

/**
* @param string $name
* @param string $tab
*
* @return self
*/
public function withName($name)
public function setSpacing($tab = '')
{
$this->name = $name;
$this->tab = $tab;

return $this;
}

/**
* @param boolean $styling
*
* @return self
*/
public function useStyling($styling = true)
{
$this->styling = $styling;

return $this;
}
Expand Down
29 changes: 27 additions & 2 deletions src/Combustor.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ class Combustor
*/
protected $app = null;

/**
* @var \Rougin\Combustor\Colfield[]
*/
protected $customs = array();

/**
* @var \Rougin\Describe\Driver\DriverInterface|null
*/
Expand Down Expand Up @@ -63,11 +68,19 @@ public function getDriver()
/**
* @return string[]
*/
public function getExcluded()
public function getExcludedFields()
{
return $this->excluded;
}

/**
* @return \Rougin\Combustor\Colfield[]
*/
public function getCustomFields()
{
return $this->customs;
}

/**
* @return string
*/
Expand Down Expand Up @@ -100,12 +113,24 @@ public function setDriver(DriverInterface $driver)
return $this;
}

/**
* @param \Rougin\Combustor\Colfield[] $customs
*
* @return self
*/
public function setCustomFields($customs)
{
$this->customs = $customs;

return $this;
}

/**
* @param string[] $excluded
*
* @return self
*/
public function setExcluded($excluded)
public function setExcludedFields($excluded)
{
$this->excluded = $excluded;

Expand Down
9 changes: 8 additions & 1 deletion src/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ class Command extends Blueprint
*/
protected $excluded = array();

/**
* @var \Rougin\Combustor\Colfield[]
*/
protected $customs = array();

/**
* @var \Rougin\Classidy\Generator
*/
Expand All @@ -48,7 +53,9 @@ public function __construct(Combustor $combustor, Generator $maker)
{
$this->driver = $combustor->getDriver();

$this->excluded = $combustor->getExcluded();
$this->excluded = $combustor->getExcludedFields();

$this->customs = $combustor->getCustomFields();

$this->maker = $maker;

Expand Down
8 changes: 6 additions & 2 deletions src/Commands/CreateView.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ public function run()

$create->withBootstrap($bootstrap);

$create->withExcluded($this->excluded);
$create->withExcludedFields($this->excluded);

$create->withCustomFields($this->customs);

$file = $path . $name . '/create.php';

Expand All @@ -112,7 +114,9 @@ public function run()

$edit->withBootstrap($bootstrap);

$edit->withExcluded($this->excluded);
$edit->withExcludedFields($this->excluded);

$edit->withCustomFields($this->customs);

$file = $path . $name . '/edit.php';

Expand Down
43 changes: 40 additions & 3 deletions src/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Rougin\Combustor;

use ReflectionClass;
use Rougin\Blueprint\Blueprint;
use Rougin\Blueprint\Container;
use Rougin\Combustor\Packages\CombustorPackage;
Expand Down Expand Up @@ -77,10 +78,42 @@ public function getAppPath()
return realpath($path);
}

/**
* @return \Rougin\Combustor\Colfield[]
*/
public function getCustomFields()
{
$parsed = $this->getParsed();

$field = 'custom_fields';

if (! array_key_exists($field, $parsed))
{
return array();
}

/** @var class-string[] */
$items = $parsed[$field];

$customs = array();

foreach ($items as $item)
{
$ref = new ReflectionClass($item);

/** @var \Rougin\Combustor\Colfield */
$custom = $ref->newInstance();

$customs[] = $custom;
}

return $customs;
}

/**
* @return string[]
*/
public function getExcluded()
public function getExcludedFields()
{
$parsed = $this->getParsed();

Expand Down Expand Up @@ -139,9 +172,13 @@ protected function setPackages()
$container->addPackage($describe);

$combustor = new CombustorPackage($path);
$excluded = $this->getExcluded();

$combustor->setExcluded($excluded);
$customs = $this->getCustomFields();
$combustor->setCustomFields($customs);

$excluded = $this->getExcludedFields();
$combustor->setExcludedFields($excluded);

$container->addPackage($combustor);

$this->setContainer($container);
Expand Down
Loading

0 comments on commit 1e4113b

Please sign in to comment.