Skip to content

Commit

Permalink
Resource maker
Browse files Browse the repository at this point in the history
  • Loading branch information
loic425 committed Apr 17, 2023
1 parent ed14316 commit dd3135a
Show file tree
Hide file tree
Showing 17 changed files with 621 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@
/symfony.lock

/.phpunit.result.cache

src/Bundle/test/src/Tests/Tmp/
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@
"rector/rector": "^0.13.5",
"symfony/messenger": "^5.4 || ^6.0",
"symfony/serializer": "^5.4 || ^6.0",
"symfony/security-bundle": "^5.4 || ^6.0"
"symfony/security-bundle": "^5.4 || ^6.0",
"symfony/maker-bundle": "^1.48"
},
"suggest": {
"doctrine/orm": "^2.5",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Bundle\ResourceBundle\DependencyInjection\Compiler;

use Sylius\Component\Resource\Symfony\Command\StubMakeResource;
use Sylius\Component\Resource\Symfony\Maker\MakeResource;
use Symfony\Bundle\MakerBundle\MakerBundle;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

final class RegisterStubCommandsPass implements CompilerPassInterface
{
/**
* @inheritdoc
*/
public function process(ContainerBuilder $container): void
{
if (!$this->isMakerEnabled($container)) {
$container->register(StubMakeResource::class)->setClass(StubMakeResource::class)->addTag('console.command');
$container->removeDefinition('sylius.maker.resource');
$container->removeAlias(MakeResource::class);
}
}

private function isMakerEnabled(ContainerBuilder $container): bool
{
if (!class_exists(MakerBundle::class)) {
return false;
}

/** @var array $bundles */
$bundles = $container->getParameter('kernel.bundles');

return in_array(MakerBundle::class, $bundles, true);
}
}
1 change: 1 addition & 0 deletions src/Bundle/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<import resource="services/form.xml" />
<import resource="services/helper.xml" />
<import resource="services/listener.xml" />
<import resource="services/maker.xml" />
<import resource="services/metadata.xml" />
<import resource="services/routing.xml" />
<import resource="services/state.xml" />
Expand Down
20 changes: 20 additions & 0 deletions src/Bundle/Resources/config/services/maker.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--
This file is part of the Sylius package.
(c) Paweł Jędrzejewski
For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.
-->

<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="sylius.maker.resource" class="Sylius\Component\Resource\Symfony\Maker\MakeResource">
<tag name="maker.command" />
</service>
</services>
</container>
2 changes: 2 additions & 0 deletions src/Bundle/SyliusResourceBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Sylius\Bundle\ResourceBundle\DependencyInjection\Compiler\RegisterResourcesPass;
use Sylius\Bundle\ResourceBundle\DependencyInjection\Compiler\RegisterResourceStateMachinePass;
use Sylius\Bundle\ResourceBundle\DependencyInjection\Compiler\RegisterStateMachinePass;
use Sylius\Bundle\ResourceBundle\DependencyInjection\Compiler\RegisterStubCommandsPass;
use Sylius\Bundle\ResourceBundle\DependencyInjection\Compiler\TwigPass;
use Sylius\Bundle\ResourceBundle\DependencyInjection\Compiler\WinzouStateMachinePass;
use Sylius\Bundle\ResourceBundle\DependencyInjection\PagerfantaExtension;
Expand Down Expand Up @@ -53,6 +54,7 @@ public function build(ContainerBuilder $container): void
$container->addCompilerPass(new RegisterResourceRepositoryPass());
$container->addCompilerPass(new RegisterResourcesPass());
$container->addCompilerPass(new RegisterStateMachinePass());
$container->addCompilerPass(new RegisterStubCommandsPass());
$container->addCompilerPass(new RegisterResourceStateMachinePass());
$container->addCompilerPass(new TwigPass());
$container->addCompilerPass(new WinzouStateMachinePass());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace DependencyInjection\Compiler;

use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractCompilerPassTestCase;
use Sylius\Bundle\ResourceBundle\DependencyInjection\Compiler\RegisterStubCommandsPass;
use Sylius\Component\Resource\Symfony\Command\StubMakeResource;
use Sylius\Component\Resource\Symfony\Maker\MakeResource;
use Symfony\Bundle\MakerBundle\MakerBundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;

final class RegisterStubCommandsPassTest extends AbstractCompilerPassTestCase
{
/** @test */
public function it_registers_stub_commands_when_maker_is_not_registered(): void
{
$this->compile();

$this->assertContainerBuilderHasService(StubMakeResource::class, StubMakeResource::class);
}

/** @test */
public function it_does_not_register_stub_commands_when_maker_is_registered(): void
{
$this->setParameter('kernel.bundles', [MakerBundle::class]);

$this->compile();

$this->assertContainerBuilderNotHasService(StubMakeResource::class);
}

/** @test */
public function it_unregisters_definition_for_grid_maker_when_maker_is_not_registered(): void
{
$this->registerService('sylius.maker.resource', MakeResource::class);

$this->compile();

$this->assertContainerBuilderNotHasService(MakeResource::class);
}

/** @test */
public function it_does_not_unregister_definition_for_grid_maker_when_maker_is_registered(): void
{
$this->setParameter('kernel.bundles', [MakerBundle::class]);
$this->registerService('sylius.maker.resource', MakeResource::class);

$this->compile();

$this->assertContainerBuilderHasService('sylius.maker.resource', MakeResource::class);
}

protected function registerCompilerPass(ContainerBuilder $container): void
{
$this->setParameter('kernel.bundles', []);

$container->addCompilerPass(new RegisterStubCommandsPass());
}
}
2 changes: 2 additions & 0 deletions src/Bundle/test/config/bundles.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Sylius\Bundle\GridBundle\SyliusGridBundle;
use Sylius\Bundle\ResourceBundle\SyliusResourceBundle;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Bundle\MakerBundle\MakerBundle;
use Symfony\Bundle\SecurityBundle\SecurityBundle;
use Symfony\Bundle\TwigBundle\TwigBundle;
use winzou\Bundle\StateMachineBundle\winzouStateMachineBundle;
Expand All @@ -39,4 +40,5 @@
NelmioAliceBundle::class => ['all' => true],
winzouStateMachineBundle::class => ['all' => true, 'test_without_state_machine' => false],
SyliusGridBundle::class => ['all' => true, 'test_without_twig' => false],
MakerBundle::class => ['all' => true],
];
31 changes: 31 additions & 0 deletions src/Bundle/test/src/Entity/Tests/Tmp/Entity/Book.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace App\Entity\Tests\Tmp\Entity;

use App\Repository\Tests\Tmp\Entity\BookRepository;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: BookRepository::class)]
class Book
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;

public function getId(): ?int
{
return $this->id;
}
}
77 changes: 77 additions & 0 deletions src/Bundle/test/src/Repository/Tests/Tmp/Entity/BookRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace App\Repository\Tests\Tmp\Entity;

use App\Entity\Tests\Tmp\Entity\Book;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

/**
* @extends ServiceEntityRepository<Book>
*
* @method Book|null find($id, $lockMode = null, $lockVersion = null)
* @method Book|null findOneBy(array $criteria, array $orderBy = null)
* @method Book[] findAll()
* @method Book[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class BookRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Book::class);
}

public function save(Book $entity, bool $flush = false): void
{
$this->getEntityManager()->persist($entity);

if ($flush) {
$this->getEntityManager()->flush();
}
}

public function remove(Book $entity, bool $flush = false): void
{
$this->getEntityManager()->remove($entity);

if ($flush) {
$this->getEntityManager()->flush();
}
}

// /**
// * @return Book[] Returns an array of Book objects
// */
// public function findByExampleField($value): array
// {
// return $this->createQueryBuilder('b')
// ->andWhere('b.exampleField = :val')
// ->setParameter('val', $value)
// ->orderBy('b.id', 'ASC')
// ->setMaxResults(10)
// ->getQuery()
// ->getResult()
// ;
// }

// public function findOneBySomeField($value): ?Book
// {
// return $this->createQueryBuilder('b')
// ->andWhere('b.exampleField = :val')
// ->setParameter('val', $value)
// ->getQuery()
// ->getOneOrNullResult()
// ;
// }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace App\Sylius\Resource\Tests\Tmp\Sylius\Resource;

use Sylius\Component\Resource\Metadata\Create;
use Sylius\Component\Resource\Metadata\Delete;
use Sylius\Component\Resource\Metadata\Index;
use Sylius\Component\Resource\Metadata\Resource;
use Sylius\Component\Resource\Metadata\Show;
use Sylius\Component\Resource\Metadata\Update;
use Sylius\Component\Resource\Model\ResourceInterface;
use Symfony\Component\Uid\AbstractUid;

#[Resource]
#[Index(
// grid: Tests\Tmp\Sylius\Resource\BookGrid::class,
)]
#[Create(
// processor: CreateTests\Tmp\Sylius\Resource\BookProcessor::class,
)]
#[Update(
// provider: Tests\Tmp\Sylius\Resource\BookItemProvider::class,
// processor: UpdateTests\Tmp\Sylius\Resource\BookProcessor::class,
)]
#[Delete(
// provider: Tests\Tmp\Sylius\Resource\BookItemProvider::class,
// processor: DeleteTests\Tmp\Sylius\Resource\BookProcessor::class,
)]
#[Show(
// template: 'tests\tmp\sylius\resource\book/show.html.twig',
// provider: Tests\Tmp\Sylius\Resource\BookItemProvider::class,
)]
final class BookResource implements ResourceInterface
{
public function __construct(
public ?AbstractUid $id = null,
) {
}

public function getId(): ?string
{
return $this->id?->__toString();
}
}
Loading

0 comments on commit dd3135a

Please sign in to comment.