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

Updating Scaffolder code with PHP 8.1 features #640

Merged
merged 4 commits into from
Apr 2, 2022
Merged
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
2 changes: 1 addition & 1 deletion src/Scaffolder/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"spiral/http": "^2.12",
"spiral/migrations": "^2.3",
"spiral/prototype": "^2.12",
"spiral/jobs": "^2.2"
"spiral/queue": "^2.12"
},
"autoload": {
"files": [
Expand Down
26 changes: 5 additions & 21 deletions src/Scaffolder/src/Bootloader/ScaffolderBootloader.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework. Scaffolder
*
* @license MIT
* @author Valentin V (vvval)
*/

declare(strict_types=1);

namespace Spiral\Scaffolder\Bootloader;
Expand All @@ -30,19 +23,10 @@ class ScaffolderBootloader extends Bootloader
SlugifyInterface::class => Slugify::class,
];

/** @var ConfiguratorInterface */
private $config;

/** @var KernelInterface */
private $kernel;

/**
* ScaffolderBootloader constructor.
*/
public function __construct(ConfiguratorInterface $config, KernelInterface $kernel)
{
$this->config = $config;
$this->kernel = $kernel;
public function __construct(
private readonly ConfiguratorInterface $config,
private readonly KernelInterface $kernel
) {
}

public function boot(ConsoleBootloader $console): void
Expand All @@ -57,7 +41,7 @@ public function boot(ConsoleBootloader $console): void

try {
$defaultNamespace = (new ReflectionClass($this->kernel))->getNamespaceName();
} catch (ReflectionException $e) {
} catch (ReflectionException) {
$defaultNamespace = '';
}

Expand Down
42 changes: 9 additions & 33 deletions src/Scaffolder/src/Command/AbstractCommand.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
<?php

/**
* Spiral Framework. Scaffolder
*
* @license MIT
* @author Anton Titov (Wolfy-J)
* @author Valentin V (vvval)
*/

declare(strict_types=1);

namespace Spiral\Scaffolder\Command;
Expand All @@ -27,28 +19,12 @@ abstract class AbstractCommand extends Command
*/
protected const ELEMENT = '';

/**
* @var ScaffolderConfig
*/
protected $config;

/**
* @var FilesInterface
*/
protected $files;

/** @var FactoryInterface */
private $factory;

public function __construct(
ScaffolderConfig $config,
FilesInterface $files,
protected ScaffolderConfig $config,
protected FilesInterface $files,
ContainerInterface $container,
FactoryInterface $factory
private readonly FactoryInterface $factory
) {
$this->config = $config;
$this->files = $files;
$this->factory = $factory;
$this->setContainer($container);

parent::__construct();
Expand Down Expand Up @@ -84,19 +60,19 @@ protected function getClass(): string
/**
* Write declaration into file.
*
* @param string $type If null static::ELEMENT to be used.
* @param string $type If null static::ELEMENT to be used.
*/
protected function writeDeclaration(ClassDeclaration $declaration, string $type = null): void
{
$type = $type ?? static::ELEMENT;
$type ??= static::ELEMENT;

$filename = $this->config->classFilename($type, (string)$this->argument('name'));
$filename = $this->files->normalizePath($filename);

if ($this->files->exists($filename)) {
$this->writeln(
"<fg=red>Unable to create '<comment>{$declaration->getName()}</comment>' declaration, "
. "file '<comment>{$filename}</comment>' already exists.</fg=red>"
\sprintf("<fg=red>Unable to create '<comment>%s</comment>' declaration, ", $declaration->getName())
. \sprintf("file '<comment>%s</comment>' already exists.</fg=red>", $filename)
);

return;
Expand All @@ -119,8 +95,8 @@ protected function writeDeclaration(ClassDeclaration $declaration, string $type
);

$this->writeln(
"Declaration of '<info>{$declaration->getName()}</info>' "
. "has been successfully written into '<comment>{$filename}</comment>'."
\sprintf("Declaration of '<info>%s</info>' ", $declaration->getName())
. \sprintf("has been successfully written into '<comment>%s</comment>'.", $filename)
);
}

Expand Down
11 changes: 3 additions & 8 deletions src/Scaffolder/src/Command/BootloaderCommand.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework. Scaffolder
*
* @license MIT
* @author Valentin V (vvval)
*/

declare(strict_types=1);

namespace Spiral\Scaffolder\Command;
Expand Down Expand Up @@ -36,11 +29,13 @@ class BootloaderCommand extends AbstractCommand
/**
* Create bootloader declaration.
*/
public function perform(): void
public function perform(): int
{
/** @var BootloaderDeclaration $declaration */
$declaration = $this->createDeclaration();

$this->writeDeclaration($declaration);

return self::SUCCESS;
}
}
12 changes: 3 additions & 9 deletions src/Scaffolder/src/Command/CommandCommand.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
<?php

/**
* Spiral Framework. Scaffolder
*
* @license MIT
* @author Anton Titov (Wolfy-J)
* @author Valentin V (vvval)
*/

declare(strict_types=1);

namespace Spiral\Scaffolder\Command;
Expand Down Expand Up @@ -44,7 +36,7 @@ class CommandCommand extends AbstractCommand
/**
* Create command declaration.
*/
public function perform(): void
public function perform(): int
{
/** @var CommandDeclaration $declaration */
$declaration = $this->createDeclaration();
Expand All @@ -53,5 +45,7 @@ public function perform(): void
$declaration->setDescription((string)$this->option('description'));

$this->writeDeclaration($declaration);

return self::SUCCESS;
}
}
11 changes: 3 additions & 8 deletions src/Scaffolder/src/Command/ConfigCommand.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework. Scaffolder
*
* @license MIT
* @author Valentin V (vvval)
*/

declare(strict_types=1);

namespace Spiral\Scaffolder\Command;
Expand Down Expand Up @@ -47,12 +40,14 @@ class ConfigCommand extends AbstractCommand
/**
* Create config declaration.
*/
public function perform(): void
public function perform(): int
{
/** @var ConfigDeclaration $declaration */
$declaration = $this->createDeclaration(['configName' => $this->argument('name')]);
$declaration->create((bool)$this->option('reverse'));

$this->writeDeclaration($declaration);

return self::SUCCESS;
}
}
12 changes: 3 additions & 9 deletions src/Scaffolder/src/Command/ControllerCommand.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
<?php

/**
* Spiral Framework. Scaffolder
*
* @license MIT
* @author Anton Titov (Wolfy-J)
* @author Valentin V (vvval)
*/

declare(strict_types=1);

namespace Spiral\Scaffolder\Command;
Expand Down Expand Up @@ -49,7 +41,7 @@ class ControllerCommand extends AbstractCommand
/**
* Create controller declaration.
*/
public function perform(): void
public function perform(): int
{
/** @var ControllerDeclaration $declaration */
$declaration = $this->createDeclaration();
Expand All @@ -63,5 +55,7 @@ public function perform(): void
}

$this->writeDeclaration($declaration);

return self::SUCCESS;
}
}
Loading