Skip to content

Commit

Permalink
Tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
olvlvl committed Sep 30, 2023
1 parent 457d3b3 commit 869252b
Show file tree
Hide file tree
Showing 13 changed files with 98 additions and 40 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
icanboogie/bind-activerecord is free software.
It is released under the terms of the following BSD License.

Copyright (c) 2015-2023 by Olivier Laviale
Copyright (c) 2015-present by Olivier Laviale
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ test-coveralls: test-dependencies

.PHONY: test-cleanup
test-cleanup:
@rm -rf tests/lib/sandbox/*
@# nothing

.PHONY: test-container
test-container: test-container-81
Expand Down
19 changes: 17 additions & 2 deletions lib/ConfigBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Closure;
use ICanBoogie\ActiveRecord;
use ICanBoogie\ActiveRecord\Config;
use ICanBoogie\ActiveRecord\Config\AssociationBuilder;
use ICanBoogie\ActiveRecord\Config\ConnectionDefinition;
use ICanBoogie\ActiveRecord\Model;
use ICanBoogie\ActiveRecord\Query;
Expand Down Expand Up @@ -43,6 +44,14 @@ public function build(): Config
}

/**
* @param non-empty-string $id
* @param non-empty-string $dsn
* @param non-empty-string|null $username
* @param non-empty-string|null $password
* @param non-empty-string|null $table_name_prefix
* @param non-empty-string $charset_and_collate
* @param non-empty-string $time_zone
*
* @return $this
*/
public function add_connection(
Expand Down Expand Up @@ -71,9 +80,15 @@ public function add_connection(
* @param class-string<ActiveRecord> $activerecord_class
* @param class-string<Model> $model_class
* @param class-string<Query> $query_class
* @param (Closure(SchemaBuilder $schema): SchemaBuilder)|null $schema_builder
* @param non-empty-string|null $table_name
* @param non-empty-string|null $alias
* @param (Closure(SchemaBuilder): SchemaBuilder)|null $schema_builder
* @param (Closure(AssociationBuilder): AssociationBuilder)|null $association_builder
* @param non-empty-string $connection
*
* @return $this
*/
public function add_model(
public function add_model( // @phpstan-ignore-line
string $activerecord_class,
string $model_class = Model::class,
string $query_class = Query::class,
Expand Down
51 changes: 32 additions & 19 deletions lib/Console/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace ICanBoogie\Binding\ActiveRecord\Console;

use ICanBoogie\ActiveRecord;
use ICanBoogie\ActiveRecord\ModelIterator;
use ICanBoogie\ActiveRecord\ModelProvider;
use Symfony\Component\Console\Attribute\AsCommand;
Expand All @@ -11,6 +12,7 @@
use Throwable;

use function array_filter;
use function sprintf;

#[AsCommand('activerecord:install', "Install models")]
final class InstallCommand extends Command
Expand All @@ -24,50 +26,61 @@ public function __construct(

protected function execute(InputInterface $input, OutputInterface $output): int
{
/**
* @var array<class-string<ActiveRecord>, true|null> $tried
*/
$tried = [];

$recursive_install = function (string $id) use (&$recursive_install, &$tried, $output): bool {
if (isset($tried[$id])) {
return $tried[$id];
$recursive_install = function (string $activerecord_class) use (&$recursive_install, &$tried, $output): bool {
/** @var class-string<ActiveRecord> $activerecord_class */

if (isset($tried[$activerecord_class])) {
return $tried[$activerecord_class];
}

$model = $this->models->model_for_id($id);
$model = $this->models->model_for_record($activerecord_class);

if ($model->is_installed()) {
$output->writeln("<info>Model already installed: $id</info>");
$output->writeln("<info>Model already installed: $activerecord_class</info>");

return $tried[$id] = true;
return $tried[$activerecord_class] = true;
}

$parent_id = $model->parent?->id;
$parent_activerecord_class = $model->parent?->activerecord_class;

if ($parent_id) {
$rc = $recursive_install($parent_id);
if ($parent_activerecord_class) {
$rc = $recursive_install($parent_activerecord_class);

if (!$rc) {
$output->writeln("<error>Unable to install model '$id', parent install failed: '{$parent_id}'");

return $tried[$id] = false;
$output->writeln(
sprintf(
"<error>Unable to install model '%s', parent install failed: '%s'",
$activerecord_class,
$parent_activerecord_class
)
);

return $tried[$activerecord_class] = false;
}
}

try {
$model->install();
$output->writeln("<info>Model installed: $id</info>");
$output->writeln("<info>Model installed: $activerecord_class</info>");

return $tried[$id] = true;
return $tried[$activerecord_class] = true;
} catch (Throwable $e) {
$output->writeln("<error>Unable to install model '$id': {$e->getMessage()}</error>");
$output->writeln("<error>Unable to install model '$activerecord_class': {$e->getMessage()}</error>");
}

return $tried[$id] = false;
return $tried[$activerecord_class] = false;
};

foreach ($this->iterator->model_iterator() as $id => $_) {
$recursive_install($id);
foreach ($this->iterator->model_iterator() as $activerecord_class => $_) {
$recursive_install($activerecord_class);
}

return count(array_filter($tried, fn ($v) => $v === false))
return count(array_filter($tried, fn($v) => $v === false))
? Command::FAILURE
: Command::SUCCESS;
}
Expand Down
14 changes: 2 additions & 12 deletions lib/Hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,12 @@ final class Hooks
*/

/**
* Define model provider.
*
* Models are provided using the model collection bound to the application.
* Sets the factory for the {@link StaticModelProvider}.
*/
public static function on_app_boot(Application\BootEvent $event): void
{
$app = $event->app;

StaticModelProvider::define(
static function () use ($app): ModelProvider {
static $resolver;

return $resolver ??= $app->service_for_class(ModelProvider::class);
}
static fn() => $event->app->service_for_class(ModelProvider::class)
);
}

Expand All @@ -61,8 +53,6 @@ public static function active_record_validate(ActiveRecord $record): array|Valid

/**
* Returns the records cache associated with the model.
*
* @param Model<int|string|string[],ActiveRecord> $model
*/
public static function model_lazy_get_activerecord_cache(Model $model): RuntimeActiveRecordCache
{
Expand Down
4 changes: 2 additions & 2 deletions phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/squizlabs/PHP_CodeSniffer/master/phpcs.xsd">
<file>lib</file>
<file>tests</file>
<exclude-pattern>tests/lib/sandbox/*</exclude-pattern>
<exclude-pattern>tests/repository/*</exclude-pattern>
<exclude-pattern>tests/sandbox/*</exclude-pattern>
<exclude-pattern>tests/var/*</exclude-pattern>

<arg name="colors"/>

Expand Down
3 changes: 3 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ parameters:
level: max
paths:
- lib
ignoreErrors:
- '#ICanBoogie.+ActiveRecord but does not specify its types#'
- '#ICanBoogie.+ActiveRecord.+Model but does not specify its types:#'
6 changes: 6 additions & 0 deletions tests/lib/Acme/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@

namespace Test\ICanBoogie\Binding\ActiveRecord\Acme;

use ICanBoogie\ActiveRecord\Schema\Date;
use ICanBoogie\ActiveRecord\Schema\Text;

class Article extends Node
{
#[Text]
public string $body;

#[Date]
public string $date;
}
6 changes: 6 additions & 0 deletions tests/lib/Acme/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@
namespace Test\ICanBoogie\Binding\ActiveRecord\Acme;

use ICanBoogie\ActiveRecord;
use ICanBoogie\ActiveRecord\Schema\Character;
use ICanBoogie\ActiveRecord\Schema\Id;
use ICanBoogie\ActiveRecord\Schema\Serial;

class Node extends ActiveRecord
{
#[Serial, Id]
public int $id;

#[Character]
public string $title;
}
28 changes: 28 additions & 0 deletions tests/lib/ConfigBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,34 @@ public function test_build(): void
);
}

public function test_use_attributes(): void
{
$expected = (new ConfigBuilder())
->add_connection(Config::DEFAULT_CONNECTION_ID, 'sqlite::memory:')
->add_model(
activerecord_class: Node::class,
schema_builder: fn(SchemaBuilder $b) => $b
->add_serial('id', primary: true)
->add_character('title'),
)
->add_model(
activerecord_class: Article::class,
schema_builder: fn(SchemaBuilder $b) => $b
->add_text('body')
->add_date('date'),
)
->build();

$actual = (new ConfigBuilder())
->add_connection(Config::DEFAULT_CONNECTION_ID, 'sqlite::memory:')
->use_attributes()
->add_model(Node::class)
->add_model(Article::class)
->build();

$this->assertEquals($expected, $actual);
}

public function test_integration(): void
{
$expected = (new ConfigBuilder())
Expand Down
1 change: 0 additions & 1 deletion tests/lib/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Test\ICanBoogie\Binding\ActiveRecord\Acme\Article;
use Test\ICanBoogie\Binding\ActiveRecord\Acme\Node;
use Test\ICanBoogie\Binding\ActiveRecord\Acme\NodeModel;

use Test\ICanBoogie\Binding\ActiveRecord\Acme\SampleService;

use function ICanBoogie\app;
Expand Down
2 changes: 0 additions & 2 deletions tests/repository/cache/.gitignore

This file was deleted.

File renamed without changes.

0 comments on commit 869252b

Please sign in to comment.