Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Giiant config generator #301

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ public function bootstrap($app)
$app->getModule('gii')->generators['giiant-test'] = 'schmunk42\giiant\generators\test\Generator';
}

if (!isset($app->getModule('gii')->generators['giiant-config'])) {
$app->getModule('gii')->generators['giiant-config'] = 'schmunk42\giiant\generators\config\Generator';
}

if ($app instanceof \yii\console\Application) {
$app->controllerMap['giiant-batch'] = 'schmunk42\giiant\commands\BatchController';
}
Expand Down
166 changes: 166 additions & 0 deletions src/generators/config/Generator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
<?php

namespace schmunk42\giiant\generators\config;

use Yii;
use yii\gii\CodeFile;

class Generator extends \yii\gii\Generator
{

/**
* Namespace in which the config should be generated
*/
public $ns = 'project\modules\crud';

/**
* Path where the config file should be generated
*/
public $savePath = '/app/project/modules/crud/config';

/**
* Path where the config file should be generated
*/
public $filename = 'giiant.php';

/**
* @inheritdoc
*/
public function getName()
{
return 'Giiant Config';
}

/**
* @inheritdoc
*/
public function getDescription()
{
return 'This generator generates a basic configuration file.';
}

/**
* @inheritdoc
*/
public function generate()
{
return [
new CodeFile($this->getFilePath(), $this->render('config.php', [
'levels' => $this->getBaseConfigParentDirectoryCount()
]))
];
}

/**
* @inheritdoc
*/
public function rules()
{
$rules = parent::rules();
$rules[] = [
'ns',
'filter',
'filter' => static function ($value) {
return trim($value, ' \\');
},
'skipOnArray' => true,
'skipOnEmpty' => true
];
$rules[] = [
'savePath',
'filter',
'filter' => static function ($value) {
return DIRECTORY_SEPARATOR . trim($value, ' ' . DIRECTORY_SEPARATOR);
},
'skipOnArray' => true,
'skipOnEmpty' => true
];
$rules[] = [
'messageCategory',
'filter',
'filter' => static function ($value) {
return trim($value);
},
'skipOnArray' => true,
'skipOnEmpty' => true
];
$rules[] = [
['ns', 'savePath', 'filename'],
'required'
];
$rules[] = [
'ns',
'validateNamespace'
];
$rules[] = [
'filename',
'match',
'pattern' => '/\.php$/',
'message' => 'Only files with the ending .php are allowed.',
];
return $rules;
}

/**
* @inheritdoc
*/
public function attributeLabels()
{
$attributeLabels = parent::attributeLabels();
$attributeLabels['ns'] = 'Namespace';
$attributeLabels['savePath'] = 'Save Path';
$attributeLabels['filename'] = 'Filename';
$attributeLabels['messageCategory'] = 'Message Category';
return $attributeLabels;
}

/**
* @inheritdoc
*/
public function hints()
{
$hints = parent::hints();
$hints['ns'] = 'This is the namespace of the config file to be generated, e.g., <code>app\models</code>';
$hints['savePath'] = 'Path where the config file will be saved';
$hints['filename'] = 'This is the filename of the php config file to be generated, e.g., <code>giiant.php</code>. Must end with .php';
return $hints;
}

/**
* @inheritdoc
*/
public function stickyAttributes()
{
$stickyAttributes = parent::stickyAttributes();
$stickyAttributes[] = 'ns';
$stickyAttributes[] = 'savePath';
$stickyAttributes[] = 'filename';
return $stickyAttributes;
}

/**
* @see \yii\gii\generators\model\Generator::validateNamespace()
*/
public function validateNamespace($attribute)
{
$value = $this->$attribute;
$value = ltrim($value, '\\');
$path = Yii::getAlias('@' . str_replace('\\', '/', $value), false);
if ($path === false) {
$this->addError($attribute, 'Namespace must be associated with an existing directory.');
}
}

protected function getFilePath()
{
return $this->savePath . DIRECTORY_SEPARATOR . $this->filename;
}

/**
* The number of parent directories to go up. This must be an integer greater than 0.
*/
protected function getBaseConfigParentDirectoryCount(): int
{
return count(explode(DIRECTORY_SEPARATOR, trim($this->savePath, DIRECTORY_SEPARATOR)));
}
}
67 changes: 67 additions & 0 deletions src/generators/config/default/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* @var \schmunk42\giiant\generators\config\Generator $generator
* @var int $levels
*/

echo '<?php' . PHP_EOL;
echo PHP_EOL;
echo 'namespace ' . $generator->ns . ';' . PHP_EOL;
echo PHP_EOL;
echo <<<PHP
use schmunk42\giiant\generators\crud\callbacks\\base\Callback;
use schmunk42\giiant\generators\crud\providers\core\CallbackProvider;
use schmunk42\giiant\generators\crud\providers\core\OptsProvider;
use schmunk42\giiant\generators\crud\providers\core\RelationProvider;
use Yii;

\$config = require dirname(__DIR__, $levels) . '/config/main.php';

Yii::\$container->set(
CallbackProvider::class,
[
// Form
'activeFields' => [
'created_at|updated_at' => Callback::false()
],
// Index
'columnFormats' => [
'created_at|updated_at' => fn(\$attribute) => "'\$attribute:datetime'"
],
// View
'attributeFormats' => [
'created_at|updated_at' => fn(\$attribute) => "'\$attribute:datetime'"
]
]
);

\$config['controllerMap']['project-batch'] = [
'class' => 'schmunk42\giiant\commands\BatchController',
'overwrite' => true,
'interactive' => false,
'modelNamespace' => __NAMESPACE__ . '\\models',
'modelBaseClass' => __NAMESPACE__ . '\\models\\ActiveRecord',
'modelQueryNamespace' => __NAMESPACE__ . '\\models\\query',
'crudControllerNamespace' => __NAMESPACE__ . '\\controllers',
'crudSearchModelNamespace' => __NAMESPACE__ . '\\models\\search',
'crudViewPath' => '@' . str_replace('\\\\', '/', __NAMESPACE__) . '/views',
'crudPathPrefix' => '',
'crudTidyOutput' => false,
'crudFixOutput' => false,
'useTimestampBehavior' => false,
'useBlameableBehavior' => false,
'tablePrefix' => '',
'crudMessageCategory' => '$generator->messageCategory',
'modelMessageCategory' => '$generator->messageCategory',
'crudEnableCopy' => false,
'tables' => [],
'crudProviders' => [
CallbackProvider::class,
OptsProvider::class,
RelationProvider::class
]
];

return \$config;
PHP;
echo PHP_EOL;
11 changes: 11 additions & 0 deletions src/generators/config/form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
/**
* @var yii\web\View $this
* @var yii\widgets\ActiveForm $form
* @var yii\gii\generators\crud\Generator $generator
*/

echo $form->field($generator, 'ns');
echo $form->field($generator, 'filename');
echo $form->field($generator, 'savePath');
echo $form->field($generator, 'messageCategory');
Loading