Skip to content

Commit

Permalink
style: break long lines
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Oct 29, 2023
1 parent c90053b commit 6a97a03
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 20 deletions.
125 changes: 106 additions & 19 deletions system/CLI/GeneratorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,13 @@ private function generateFile(string $target, string $content): void
CLI::write(lang('CLI.generator.usingCINamespace'), 'yellow');
CLI::newLine();

if (CLI::prompt('Are you sure you want to continue?', ['y', 'n'], 'required') === 'n') {
if (
CLI::prompt(
'Are you sure you want to continue?',
['y', 'n'],
'required'
) === 'n'
) {
CLI::newLine();
CLI::write(lang('CLI.generator.cancelOperation'), 'yellow');
CLI::newLine();
Expand All @@ -160,7 +166,11 @@ private function generateFile(string $target, string $content): void
// Overwriting files unknowingly is a serious annoyance, So we'll check if
// we are duplicating things, If 'force' option is not supplied, we bail.
if (! $this->getOption('force') && $isFile) {
CLI::error(lang('CLI.generator.fileExist', [clean_path($target)]), 'light_gray', 'red');
CLI::error(
lang('CLI.generator.fileExist', [clean_path($target)]),
'light_gray',
'red'
);
CLI::newLine();

return;
Expand All @@ -179,21 +189,31 @@ private function generateFile(string $target, string $content): void
// contents from the template, and then we'll do the necessary replacements.
if (! write_file($target, $content)) {
// @codeCoverageIgnoreStart
CLI::error(lang('CLI.generator.fileError', [clean_path($target)]), 'light_gray', 'red');
CLI::error(
lang('CLI.generator.fileError', [clean_path($target)]),
'light_gray',
'red'
);
CLI::newLine();

return;
// @codeCoverageIgnoreEnd
}

if ($this->getOption('force') && $isFile) {
CLI::write(lang('CLI.generator.fileOverwrite', [clean_path($target)]), 'yellow');
CLI::write(
lang('CLI.generator.fileOverwrite', [clean_path($target)]),
'yellow'
);
CLI::newLine();

return;
}

CLI::write(lang('CLI.generator.fileCreate', [clean_path($target)]), 'green');
CLI::write(
lang('CLI.generator.fileCreate', [clean_path($target)]),
'green'
);
CLI::newLine();
}

Expand Down Expand Up @@ -244,15 +264,34 @@ protected function qualifyClassName(): string
$class = $matches[1] . ucfirst($matches[2]);
}

if ($this->enabledSuffixing && $this->getOption('suffix') && preg_match($pattern, $class) !== 1) {
if (
$this->enabledSuffixing && $this->getOption('suffix')
&& preg_match($pattern, $class) !== 1
) {
$class .= ucfirst($component);
}

// Trims input, normalize separators, and ensure that all paths are in Pascalcase.
$class = ltrim(implode('\\', array_map('pascalize', explode('\\', str_replace('/', '\\', trim($class))))), '\\/');
$class = ltrim(
implode(
'\\',
array_map(
'pascalize',
explode('\\', str_replace('/', '\\', trim($class)))
)
),
'\\/'
);

// Gets the namespace from input. Don't forget the ending backslash!
$namespace = trim(str_replace('/', '\\', $this->getOption('namespace') ?? APP_NAMESPACE), '\\') . '\\';
$namespace = trim(
str_replace(
'/',
'\\',
$this->getOption('namespace') ?? APP_NAMESPACE
),
'\\'
) . '\\';

if (strncmp($class, $namespace, strlen($namespace)) === 0) {
return $class; // @codeCoverageIgnore
Expand All @@ -268,21 +307,39 @@ protected function qualifyClassName(): string
protected function renderTemplate(array $data = []): string
{
try {
return view(config(Generators::class)->views[$this->name], $data, ['debug' => false]);
return view(
config(Generators::class)->views[$this->name],
$data,
['debug' => false]
);
} catch (Throwable $e) {
log_message('error', (string) $e);

return view("CodeIgniter\\Commands\\Generators\\Views\\{$this->template}", $data, ['debug' => false]);
return view(
"CodeIgniter\\Commands\\Generators\\Views\\{$this->template}",
$data,
['debug' => false]
);
}
}

/**
* Performs pseudo-variables contained within view file.
*/
protected function parseTemplate(string $class, array $search = [], array $replace = [], array $data = []): string
{
protected function parseTemplate(
string $class,
array $search = [],
array $replace = [],
array $data = []
): string {
// Retrieves the namespace part from the fully qualified class name.
$namespace = trim(implode('\\', array_slice(explode('\\', $class), 0, -1)), '\\');
$namespace = trim(
implode(
'\\',
array_slice(explode('\\', $class), 0, -1)
),
'\\'
);
$search[] = '<@php';
$search[] = '{namespace}';
$search[] = '{class}';
Expand All @@ -302,7 +359,14 @@ protected function buildContent(string $class): string
{
$template = $this->prepare($class);

if ($this->sortImports && preg_match('/(?P<imports>(?:^use [^;]+;$\n?)+)/m', $template, $match)) {
if (
$this->sortImports
&& preg_match(
'/(?P<imports>(?:^use [^;]+;$\n?)+)/m',
$template,
$match
)
) {
$imports = explode("\n", trim($match['imports']));
sort($imports);

Expand All @@ -317,22 +381,45 @@ protected function buildContent(string $class): string
*/
protected function buildPath(string $class): string
{
$namespace = trim(str_replace('/', '\\', $this->getOption('namespace') ?? APP_NAMESPACE), '\\');
$namespace = trim(
str_replace(
'/',
'\\',
$this->getOption('namespace') ?? APP_NAMESPACE
),
'\\'
);

// Check if the namespace is actually defined and we are not just typing gibberish.
$base = Services::autoloader()->getNamespace($namespace);

if (! $base = reset($base)) {
CLI::error(lang('CLI.namespaceNotDefined', [$namespace]), 'light_gray', 'red');
CLI::error(
lang('CLI.namespaceNotDefined', [$namespace]),
'light_gray',
'red'
);
CLI::newLine();

return '';
}

$base = realpath($base) ?: $base;
$file = $base . DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, trim(str_replace($namespace . '\\', '', $class), '\\')) . '.php';

return implode(DIRECTORY_SEPARATOR, array_slice(explode(DIRECTORY_SEPARATOR, $file), 0, -1)) . DIRECTORY_SEPARATOR . $this->basename($file);
$file = $base . DIRECTORY_SEPARATOR
. str_replace(
'\\',
DIRECTORY_SEPARATOR,
trim(str_replace($namespace . '\\', '', $class), '\\')
) . '.php';

return implode(
DIRECTORY_SEPARATOR,
array_slice(
explode(DIRECTORY_SEPARATOR, $file),
0,
-1
)
) . DIRECTORY_SEPARATOR . $this->basename($file);
}

/**
Expand Down
6 changes: 5 additions & 1 deletion system/Commands/Generators/CellGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,11 @@ public function run(array $params)

$className = $this->qualifyClassName();
$viewName = decamelize(class_basename($className));
$viewName = preg_replace('/([a-z][a-z0-9_\/\\\\]+)(_cell)$/i', '$1', $viewName) ?? $viewName;
$viewName = preg_replace(
'/([a-z][a-z0-9_\/\\\\]+)(_cell)$/i',
'$1',
$viewName
) ?? $viewName;
$namespace = substr($className, 0, strrpos($className, '\\') + 1);

$this->generateView($namespace . $viewName, $params);
Expand Down

0 comments on commit 6a97a03

Please sign in to comment.