Skip to content

Commit

Permalink
fix autoload and typo
Browse files Browse the repository at this point in the history
  • Loading branch information
absszero committed Jan 6, 2024
1 parent 9a9eed4 commit 925e9b3
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 34 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ A PHP code generator to generate code via your LAYOUT file.

1. Install the package:
```shell
composer require global absszero/phead
composer global require absszero/phead
```

2. Set up Composer bin path:

### Linux / macOS

```bash sehll
```bash shell
# Bash shell
echo 'export PATH="$PATH:~/.composer/vendor/bin"' >> ~/.bashrc
source ~/.bashrc
Expand All @@ -40,9 +40,11 @@ A PHP code generator to generate code via your LAYOUT file.


```shell
# Get a sample layout file named "my-layout.yaml".
# Get a sample layout file.
$ phead sample
my-layout.yaml is generated.
# Generate code via your layout file.
$ phead my-layout.yaml
Expand All @@ -63,7 +65,7 @@ Hello/MyModel.php (skip)

### Overwrite files

Existing files will not be regenerated, you can orverwite with `--force`.
Existing files will not be regenerated, you can overwrite with `--force`.


```shell
Expand Down Expand Up @@ -104,7 +106,7 @@ $files:
model:
# The file variables.
# 'namespace', 'class' variables will be auto genreated via 'to' path
# 'namespace', 'class' variables will be auto generated via 'to' path
vars:
foo: bar
# Overwrite default namespace
Expand Down
30 changes: 28 additions & 2 deletions bin/phead
Original file line number Diff line number Diff line change
@@ -1,9 +1,35 @@
#!/usr/bin/env php
<?php
require __DIR__.'/../vendor/autoload.php';

use Symfony\Component\Console\Application;

/**
* Include Composer autoload.
*
* @return bool [return description]
*/
function includeAutoload(): bool
{
$files = [
__DIR__ . '/../vendor/autoload.php',
__DIR__ . '/../../../autoload.php',
__DIR__ . '/../../../../../vendor/autoload.php'
];

foreach ($files as $file) {
$included = file_exists($file) && include $file;
if ($included) {
return true;
}
}

return false;
}

if (false === includeAutoload()) {
fwrite(STDERR, 'Install dependencies using Composer.' . PHP_EOL);
exit(1);
}

try {
$application = new Application();
$application->add(new Absszero\Phead\PheadCommand);
Expand Down
23 changes: 11 additions & 12 deletions config/sample.yaml
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
$globals:
user: '{{ $env.USER }}'
dir: Hello
$files:
controller:
model:
vars:
foo: bar
from: |
<?php
namespace {{ namespace }};
class {{ class }}
{
public index()
{
$model = new {{ $files.model }};
}
public $foo = '{{ foo }}';
}
to: "{{ $globals.dir }}/MyController.php"
model:
vars:
foo: bar
to: "{{ $globals.dir }}/MyModel.php"
controller:
from: |
<?php
namespace {{ namespace }};
class {{ class }}
{
public $foo = '{{ foo }}';
public function index()
{
$model = new {{ $files.model }};
}
}
to: "{{ $globals.dir }}/MyModel.php"
to: "{{ $globals.dir }}/MyController.php"
12 changes: 6 additions & 6 deletions src/Layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,22 +290,22 @@ public function replaceGlobalVars(array $vars, ?array $globalVars = []): array
* replace with variables
*
* @param array<string, mixed> $data
* @param array<string, mixed> $vars
* @param null|array<string, mixed> $vars
*
* @return array<string, mixed> [return description]
*/
public function replaceLocalVars(array $data, ?array $vars = []): array
public function replaceLocalVars(array $data, ?array $vars = null): array
{
foreach ($data as $key => $value) {
if ($key === 'vars') {
continue;
}

if (is_array($value)) {
if (!$vars) {
$vars = $value['vars'];
}
if (is_null($vars)) {
$vars = $value['vars'];
}

if (is_array($value)) {
$data[$key] = $this->replaceLocalVars($value, $vars);
continue;
}
Expand Down
7 changes: 5 additions & 2 deletions src/PheadCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ protected function configure(): void
->addArgument('layout', InputArgument::REQUIRED, 'The layout to use.')
->addOption('dry', 'd', InputOption::VALUE_NONE, 'Dry run.')
->addOption('only', 'o', InputOption::VALUE_OPTIONAL, 'Only those file keys are generated. Separate by comma.')
->addOption('force', 'f', InputOption::VALUE_NONE, 'Ovverwrite existed files.');
->addOption('force', 'f', InputOption::VALUE_NONE, 'Overwrite existed files.');
}

protected function execute(InputInterface $input, OutputInterface $output): int
Expand Down Expand Up @@ -122,19 +122,22 @@ protected function getOnlyFiles(array $files, ?string $only): array
protected function genSample(InputInterface $input, OutputInterface $output): int
{
$target = getcwd() . '/my-layout.yaml';
$output->writeln('');
if (file_exists($target)) {
$helper = $this->getHelper('question');
$question = new ConfirmationQuestion('<comment>my-layout.yaml</comment> <info>already exists. Replace current file?</info>', false);
$question = new ConfirmationQuestion('<comment>my-layout.yaml</comment> <info>already exists. Replace the file?</info>', false);
if (!$helper->ask($input, $output, $question)) {
return Command::SUCCESS;
}
}

$result = copy(__DIR__ . '/../config/sample.yaml', $target);
if ($result) {
$output->writeln('<comment>my-layout.yaml</comment> <info>is generated.</info>');
return Command::SUCCESS;
}

$output->writeln('<comment>my-layout.yaml</comment><error> is not generated.</error>');
return Command::FAILURE;
}
}
8 changes: 5 additions & 3 deletions tests/LayoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ public function testReplaceLocalVars(): void
'vars' => [
'bar' => 'BAR',
],
'from' => '{{ bar }}',
'methods' => [
'{{ bar }}',
'{{ $globals.foo }}',
Expand All @@ -107,10 +108,11 @@ public function testReplaceLocalVars(): void
];

$layout = new Layout;
$methods = $layout->replaceLocalVars($files)['a']['methods'];
$file = $layout->replaceLocalVars($files)['a'];

$this->assertEquals('BAR', $methods[0]);
$this->assertEquals('{{ $globals.foo }}', $methods[1]);
$this->assertEquals('BAR', $file['from']);
$this->assertEquals('BAR', $file['methods'][0]);
$this->assertEquals('{{ $globals.foo }}', $file['methods'][1]);
}

// test collect files' vars
Expand Down
8 changes: 4 additions & 4 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,17 @@ protected function addCommand($class): Command
*
* @param Command $command [$command description]
* @param array<string, mixed> $input [$input description]
* @param Closure $clourse [$clourse description]
* @param Closure $closure
*
* @return CommandTester [return description]
*/
protected function executeCommand(Command $command, array $input = [], Closure $clourse = null): CommandTester
protected function executeCommand(Command $command, array $input = [], Closure $closure = null): CommandTester
{
$input['command'] = $command->getName();
$commandTester = new CommandTester($command);

if ($clourse) {
$clourse($commandTester);
if ($closure) {
$closure($commandTester);
}

$commandTester->execute($input);
Expand Down

0 comments on commit 925e9b3

Please sign in to comment.