Skip to content

Commit

Permalink
Merge branch '8.x'
Browse files Browse the repository at this point in the history
Signed-off-by: Mior Muhammad Zaki <[email protected]>
  • Loading branch information
crynobone committed Sep 1, 2023
2 parents 4b65c91 + 7bf5c46 commit b04ff13
Show file tree
Hide file tree
Showing 14 changed files with 255 additions and 31 deletions.
2 changes: 1 addition & 1 deletion pint.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scope": "namespaced",
"strict": true
},
"no_superfluous_phpdoc_tags": false,
"no_superfluous_phpdoc_tags": true,
"php_unit_method_casing": false,
"nullable_type_declaration_for_default_null_value": {
"use_nullable_type_declaration": true
Expand Down
1 change: 0 additions & 1 deletion src/CodeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ trait CodeGenerator
/**
* Set Preset for generator.
*
* @param \Orchestra\Canvas\Core\Presets\Preset $preset
* @return $this
*/
public function setPreset(Presets\Preset $preset)
Expand Down
12 changes: 10 additions & 2 deletions src/Commands/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ abstract class Generator extends Command implements GeneratesCodeListener
/**
* Generator processor.
*
* @var string
* @var class-string<\Orchestra\Canvas\Core\GeneratesCode>
*/
protected $processor = GeneratesCode::class;
protected string $processor = GeneratesCode::class;

/**
* Construct a new generator command.
Expand Down Expand Up @@ -100,6 +100,14 @@ protected function execute(InputInterface $input, OutputInterface $output)
return $this->generateCode($force);
}

/**
* Handle generating code.
*/
public function generatingCode(string $stub, string $className): string
{
return $stub;
}

/**
* Code already exists.
*/
Expand Down
2 changes: 0 additions & 2 deletions src/CommandsProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ trait CommandsProvider
{
/**
* Setup preset for laravel.
*
* @return \Orchestra\Canvas\Core\Presets\Preset
*/
protected function presetForLaravel(Container $app): Presets\Preset
{
Expand Down
5 changes: 5 additions & 0 deletions src/Contracts/GeneratesCodeListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ interface GeneratesCodeListener
*/
public function codeAlreadyExists(string $className): mixed;

/**
* Handle generating code.
*/
public function generatingCode(string $stub, string $className): string;

/**
* Code successfully generated.
*
Expand Down
32 changes: 21 additions & 11 deletions src/GeneratesCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,20 @@ protected function makeDirectory(string $path): string
*/
protected function buildClass(string $name): string
{
$stub = $this->files->get($this->getListenerStubFile());
return $this->generatingCode(
$this->files->get($this->getListenerStubFile()), $name
);
}

return $this->replaceClass(
$this->replaceNamespace($stub, $name), $name
/**
* Handle generating code.
*/
protected function generatingCode(string $stub, string $name): string
{
return $this->listener->generatingCode(
$this->replaceClass(
$this->replaceNamespace($stub, $name), $name
), $name
);
}

Expand Down Expand Up @@ -184,14 +194,6 @@ protected function replaceNamespace(string $stub, string $name): string
return $stub;
}

/**
* Get the full namespace for a given class, without the class name.
*/
protected function getNamespace(string $name): string
{
return trim(implode('\\', \array_slice(explode('\\', $name), 0, -1)), '\\');
}

/**
* Replace the class name for the given stub.
*/
Expand All @@ -210,6 +212,14 @@ class_basename($this->userProviderModel()),
);
}

/**
* Get the full namespace for a given class, without the class name.
*/
protected function getNamespace(string $name): string
{
return trim(implode('\\', \array_slice(explode('\\', $name), 0, -1)), '\\');
}

/**
* Alphabetically sorts the imports for the given stub.
*/
Expand Down
34 changes: 34 additions & 0 deletions src/GeneratesCodeWithComponent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Orchestra\Canvas\Core;

use Illuminate\Foundation\Inspiring;
use Illuminate\Support\Str;

/**
* @see https://github.com/laravel/framework/blob/10.x/src/Illuminate/Foundation/Console/ComponentMakeCommand.php
*/
class GeneratesCodeWithComponent extends GeneratesCode
{
/**
* Handle generating code.
*/
protected function generatingCode(string $stub, string $name): string
{
$stub = parent::generatingCode($stub, $name);

if (! empty($this->options['inline'])) {
$stub = str_replace(
['DummyView', '{{ view }}', '{{view}}'],
"<<<'blade'\n<div>\n ".Inspiring::quote()."\n</div>\nblade",
$stub
);
}

return str_replace(
['DummyView', '{{ view }}', '{{view}}'],
'view(\'components.'.Str::kebab(class_basename($name)).'\')',
$stub
);
}
}
87 changes: 87 additions & 0 deletions src/GeneratesCodeWithEloquent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

namespace Orchestra\Canvas\Core;

use Illuminate\Support\Str;
use InvalidArgumentException;

/**
* @see https://github.com/laravel/framework/blob/10.x/src/Illuminate/Foundation/Console/ObserverMakeCommand.php
*/
class GeneratesCodeWithEloquent extends GeneratesCode
{
/**
* Handle generating code.
*/
protected function generatingCode(string $stub, string $name): string
{
$stub = parent::generatingCode($stub, $name);

$model = $this->options['model'];

return ! empty($model) ? $this->replaceModel($stub, $model) : $stub;
}

/**
* Replace the model for the given stub.
*/
protected function replaceModel(string $stub, string $model): string
{
$modelClass = $this->parseModel($model);

$replace = [
'DummyFullModelClass' => $modelClass,
'{{ namespacedModel }}' => $modelClass,
'{{namespacedModel}}' => $modelClass,
'DummyModelClass' => class_basename($modelClass),
'{{ model }}' => class_basename($modelClass),
'{{model}}' => class_basename($modelClass),
'DummyModelVariable' => lcfirst(class_basename($modelClass)),
'{{ modelVariable }}' => lcfirst(class_basename($modelClass)),
'{{modelVariable}}' => lcfirst(class_basename($modelClass)),
];

return str_replace(
array_keys($replace), array_values($replace), $stub
);
}

/**
* Get the fully-qualified model class name.
*
* @param string $model
*
* @throws \InvalidArgumentException
*/
protected function parseModel($model): string
{
if (preg_match('([^A-Za-z0-9_/\\\\])', $model)) {
throw new InvalidArgumentException('Model name contains invalid characters.');
}

return $this->qualifyModel($model);
}

/**
* Qualify the given model class base name.
*/
protected function qualifyModel(string $model): string
{
$model = ltrim($model, '\\/');

$model = str_replace('/', '\\', $model);

$rootNamespace = $this->rootNamespace();
$namespaceModel = $this->preset->modelNamespace().'\\'.$model;

if (Str::startsWith($model, $namespaceModel)) {
return $model;
} elseif (! \is_null($this->preset->config('model.namespace'))) {
return $namespaceModel;
}

return is_dir($this->preset->sourcePath().'/Models')
? $rootNamespace.'Models\\'.$model
: $namespaceModel;
}
}
20 changes: 20 additions & 0 deletions src/GeneratesCodeWithMarkdown.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Orchestra\Canvas\Core;

class GeneratesCodeWithMarkdown extends GeneratesCode
{
/**
* Handle generating code.
*/
protected function generatingCode(string $stub, string $name): string
{
$stub = parent::generatingCode($stub, $name);

if (! empty($this->options['view'])) {
$stub = str_replace(['DummyView', '{{ view }}', '{{view}}'], $this->options['view'], $stub);
}

return $stub;
}
}
8 changes: 3 additions & 5 deletions src/GeneratesCommandCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@
class GeneratesCommandCode extends GeneratesCode
{
/**
* Replace the class name for the given stub.
*
* @todo need to be updated
* Handle generating code.
*/
protected function replaceClass(string $stub, string $name): string
protected function generatingCode(string $stub, string $name): string
{
$stub = parent::replaceClass($stub, $name);
$stub = parent::generatingCode($stub, $name);

$command = $this->options['command'] ?: 'app:'.Str::of($name)->classBasename()->kebab()->value();

Expand Down
35 changes: 34 additions & 1 deletion src/Presets/Laravel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,28 @@

namespace Orchestra\Canvas\Core\Presets;

use Illuminate\Support\Arr;
use Symfony\Component\Console\Application;

class Laravel extends Preset
{
/**
* List of global generators.
*
* @var array<int, class-string<\Symfony\Component\Console\Command\Command>>
*/
protected static $generators = [];

/**
* Add global command.
*
* @param array<int, class-string<\Symfony\Component\Console\Command\Command>> $generators
*/
public static function commands(array $generators): void
{
static::$generators = array_merge(static::$generators, $generators);
}

/**
* Preset name.
*/
Expand Down Expand Up @@ -61,6 +81,19 @@ public function providerNamespace(): string
*/
public function getCustomStubPath(): ?string
{
return null;
return sprintf('%s/%s', $this->basePath(), 'stubs');
}

/**
* Sync commands to preset.
*/
public function addAdditionalCommands(Application $app): void
{
parent::addAdditionalCommands($app);

foreach (Arr::wrap(static::$generators) as $generator) {
/** @var class-string<\Symfony\Component\Console\Command\Command> $generator */
$app->add(new $generator($this));
}
}
}
32 changes: 32 additions & 0 deletions src/Presets/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,29 @@

namespace Orchestra\Canvas\Core\Presets;

use Illuminate\Support\Arr;
use InvalidArgumentException;
use Symfony\Component\Console\Application;

class Package extends Preset
{
/**
* List of global generators.
*
* @var array<int, class-string<\Symfony\Component\Console\Command\Command>>
*/
protected static $generators = [];

/**
* Add global command.
*
* @param array<int, class-string<\Symfony\Component\Console\Command\Command>> $generators
*/
public static function commands(array $generators): void
{
static::$generators = array_merge(static::$generators, $generators);
}

/**
* Preset name.
*/
Expand Down Expand Up @@ -71,4 +90,17 @@ public function getCustomStubPath(): ?string
{
return null;
}

/**
* Sync commands to preset.
*/
public function addAdditionalCommands(Application $app): void
{
parent::addAdditionalCommands($app);

foreach (Arr::wrap(static::$generators) as $generator) {
/** @var class-string<\Symfony\Component\Console\Command\Command> $generator */
$app->add(new $generator($this));
}
}
}
12 changes: 6 additions & 6 deletions src/Presets/Preset.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,12 @@ public function seederNamespace(): string
*/
public function addAdditionalCommands(Application $app): void
{
$generators = $this->config('generators') ?? [];

foreach (Arr::wrap($generators) as $generator) {
/** @var \Symfony\Component\Console\Command\Command $generator */
$app->add(new $generator($this));
}
tap($this->config('generators') ?? [], function ($generators) use ($app) {
foreach (Arr::wrap($generators) as $generator) {
/** @var class-string<\Symfony\Component\Console\Command\Command> $generator */
$app->add(new $generator($this));
}
});
}

/**
Expand Down
Loading

0 comments on commit b04ff13

Please sign in to comment.