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 4317a1d
Show file tree
Hide file tree
Showing 13 changed files with 452 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
Expand Up @@ -115,6 +115,8 @@ private function setSymfonyWorkflowAsStateMachine(ContainerBuilder $container):
{
if (!$this->isSymfonyWorkflowEnabled($container)) {
if (class_exists(SymfonyWorkflow::class)) {
return;

throw new \LogicException('You can not use "Symfony" for your state machine if it is not enabled on framework bundle.');
}

Expand Down
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/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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?= "<?php\n" ?>

namespace <?= $namespace; ?>;

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: <?= $class_name_without_suffix ?>Grid::class,
)]
#[Create(
// processor: Create<?= $class_name_without_suffix ?>Processor::class,
)]
#[Update(
// provider: <?= $class_name_without_suffix ?>ItemProvider::class,
// processor: Update<?= $class_name_without_suffix ?>Processor::class,
)]
#[Delete(
// provider: <?= $class_name_without_suffix ?>ItemProvider::class,
// processor: Delete<?= $class_name_without_suffix ?>Processor::class,
)]
#[Show(
// template: '<?= $show_template_dir ?>/show.html.twig',
// provider: <?= $class_name_without_suffix ?>ItemProvider::class,
)]
final class <?= $class_name ?> implements ResourceInterface
{
<?php if (class_exists(Symfony\Component\Uid\AbstractUid::class)) : ?>
public function __construct(
public ?AbstractUid $id = null,
) {
}
<?php endif; ?>

public function getId(): ?string
{
<?php if (class_exists(Symfony\Component\Uid\AbstractUid::class)) : ?>
return $this->id?->__toString();
<?php else : ?>
throw new \LogicException('TODO: Implement getId method');
<?php endif; ?>
}
}
85 changes: 85 additions & 0 deletions src/Component/Symfony/Maker/MakeResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?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\Component\Resource\Symfony\Maker;

use Symfony\Bundle\MakerBundle\ConsoleStyle;
use Symfony\Bundle\MakerBundle\DependencyBuilder;
use Symfony\Bundle\MakerBundle\Generator;
use Symfony\Bundle\MakerBundle\InputConfiguration;
use Symfony\Bundle\MakerBundle\Maker\AbstractMaker;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;

final class MakeResource extends AbstractMaker
{
public static function getCommandName(): string
{
return 'make:resource';
}

public static function getCommandDescription(): string
{
return 'Creates a Resource class';
}

public function configureCommand(Command $command, InputConfiguration $inputConfig): void
{
$command
->setDescription(self::getCommandDescription())
->addArgument('class', InputArgument::OPTIONAL, 'Class name of the resource to create')
->addOption('namespace', null, InputOption::VALUE_REQUIRED, 'Customize the namespace for generated resource', 'Sylius\\Resource')
;
}

public function configureDependencies(DependencyBuilder $dependencies): void
{
}

public function interact(InputInterface $input, ConsoleStyle $io, Command $command): void
{
}

public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void
{
$class = $input->getArgument('class');

$namespace = $input->getOption('namespace');
$namespace = \trim($namespace, '\\');

$resourceClassDetails = $generator->createClassNameDetails(
$class,
$namespace,
'Resource',
);

if (\str_ends_with($class, 'Resource')) {
$class = \mb_substr($class, 0, -\strlen('Resource'));
}

$generator->generateClass(
$resourceClassDetails->getFullName(),
__DIR__ . '/../Bundle/Resources/config/skeleton/Resource.tpl.php',
[
'class_name_without_suffix' => $class,
'show_template_dir' => \strtolower($class),
],
);

$generator->writeChanges();

$this->writeSuccessMessage($io);
}
}
Loading

0 comments on commit 4317a1d

Please sign in to comment.