Refactor common CRUD traits

This commit is contained in:
Timothy Warren 2023-07-21 10:23:16 -04:00
parent f6e00fbdb7
commit 80bb63fdd6
33 changed files with 1570 additions and 1436 deletions

View File

@ -2,30 +2,30 @@
"type": "project",
"license": "proprietary",
"require": {
"php": "^8.1.0",
"php": ">=8.2",
"composer/package-versions-deprecated": "1.11.99.5",
"doctrine/doctrine-bundle": "^2",
"doctrine/doctrine-bundle": "^2.8.2",
"doctrine/doctrine-migrations-bundle": "^3.2.2",
"doctrine/orm": "^2",
"sensio/framework-extra-bundle": "^6.2.6",
"symfony/form": "^6.0.3",
"symfony/maker-bundle": "^1.0",
"symfony/monolog-bundle": "^3.0",
"symfony/security-bundle": "^6.1",
"symfony/security-csrf": "^6.1",
"symfony/translation": "^6.0.3",
"symfony/twig-bundle": "^6.0",
"symfony/validator": "^6.0.3",
"symfony/yaml": "^6.0.3",
"twig/twig": "^2.12|^3.0"
"doctrine/orm": "^2.14.1",
"sensio/framework-extra-bundle": "^6.2.9",
"symfony/form": "^6.2.5",
"symfony/maker-bundle": "^1.48",
"symfony/monolog-bundle": "^3.8",
"symfony/security-bundle": "^6.2.6",
"symfony/security-csrf": "^6.2.5",
"symfony/translation": "^6.2.5",
"symfony/twig-bundle": "^6.2.5",
"symfony/validator": "^6.2.5",
"symfony/yaml": "^6.2.5",
"twig/twig": "^2.12|^3.5"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"phpunit/phpunit": "^9.5.28",
"roave/security-advisories": "dev-master",
"symfony/dotenv": "^6.0.3",
"symfony/flex": "^2.1.6",
"symfony/stopwatch": "^6.0.3",
"symfony/web-profiler-bundle": "^6.0"
"symfony/dotenv": "^6.2.5",
"symfony/flex": "^2.2.4",
"symfony/stopwatch": "^6.2.5",
"symfony/web-profiler-bundle": "^6.2.5"
},
"config": {
"preferred-install": {

2192
composer.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -4,7 +4,6 @@ namespace App\Controller;
use App\Entity\BrandCategory;
use App\Form\BrandCategoryType;
use App\Traits\FormControllerTrait;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
@ -14,7 +13,7 @@ use Symfony\Component\Routing\Annotation\Route;
#[Route('/brand/category')]
class BrandCategoryController extends AbstractController
{
use FormControllerTrait;
use FormControllerBase;
protected const ENTITY = BrandCategory::class;
protected const TEMPLATE_PATH = 'brand_category/';

View File

@ -4,7 +4,6 @@ namespace App\Controller;
use App\Entity\Brand;
use App\Form\BrandType;
use App\Traits\FormControllerTrait;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\{Request, Response};
@ -13,7 +12,7 @@ use Symfony\Component\Routing\Annotation\Route;
#[Route('/brand')]
class BrandController extends AbstractController
{
use FormControllerTrait;
use FormControllerBase;
protected const ENTITY = Brand::class;
protected const TEMPLATE_PATH = 'brand/';

View File

@ -4,7 +4,8 @@ namespace App\Controller;
use App\Entity\Camera;
use App\Form\CameraType;
use App\Traits\{DeleteFormTrait, FormControllerTrait};
use App\Controller\
{DeleteFormTrait};
use Doctrine\ORM\{EntityManagerInterface, ORMInvalidArgumentException};
use LogicException;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
@ -18,7 +19,7 @@ use Symfony\Component\Routing\Annotation\Route;
#[Route(path: 'camera')]
class CameraController extends AbstractController
{
use FormControllerTrait;
use FormControllerBase;
use DeleteFormTrait;
protected const ENTITY = Camera::class;

View File

@ -4,7 +4,8 @@ namespace App\Controller;
use App\Entity\CameraType;
use App\Form\CameraTypeType;
use App\Traits\{DeleteFormTrait, FormControllerTrait};
use App\Controller\
{DeleteFormTrait};
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\{RedirectResponse, Request, Response};
@ -13,7 +14,7 @@ use Symfony\Component\Routing\Annotation\Route;
#[Route(path: 'camera-type')]
class CameraTypeController extends AbstractController
{
use FormControllerTrait;
use FormControllerBase;
use DeleteFormTrait;
protected const ENTITY = CameraType::class;

View File

@ -4,7 +4,6 @@ namespace App\Controller;
use App\Entity\Chipset;
use App\Form\ChipsetType;
use App\Traits\FormControllerTrait;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
@ -13,7 +12,7 @@ use Symfony\Component\Routing\Annotation\Route;
#[Route('/chipset')]
class ChipsetController extends AbstractController {
use FormControllerTrait;
use FormControllerBase;
protected const ENTITY = Chipset::class;
protected const TEMPLATE_PATH = 'chipset/';

View File

@ -4,7 +4,6 @@ namespace App\Controller;
use App\Entity\Cpu;
use App\Form\CpuType;
use App\Traits\FormControllerTrait;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
@ -13,7 +12,7 @@ use Symfony\Component\Routing\Annotation\Route;
#[Route('/cpu')]
class CpuController extends AbstractController {
use FormControllerTrait;
use FormControllerBase;
protected const ENTITY = Cpu::class;
protected const TEMPLATE_PATH = 'cpu/';

View File

@ -1,6 +1,6 @@
<?php declare(strict_types=1);
namespace App\Traits;
namespace App\Controller;
use Symfony\Component\Form\FormInterface;

View File

@ -4,7 +4,8 @@ namespace App\Controller;
use App\Entity\Film;
use App\Form\FilmType;
use App\Traits\{DeleteFormTrait, FormControllerTrait};
use App\Controller\
{DeleteFormTrait};
use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\EntityManagerInterface;
use LogicException;
@ -18,7 +19,7 @@ use Symfony\Component\Routing\Annotation\Route;
#[Route(path: 'film')]
class FilmController extends AbstractController
{
use FormControllerTrait;
use FormControllerBase;
use DeleteFormTrait;
protected const ENTITY = Film::class;

View File

@ -4,7 +4,8 @@ namespace App\Controller;
use App\Entity\Flash;
use App\Form\FlashType;
use App\Traits\{DeleteFormTrait, FormControllerTrait};
use App\Controller\
{DeleteFormTrait};
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\{RedirectResponse, Request, Response};
@ -16,7 +17,7 @@ use Symfony\Component\Routing\Annotation\Route;
#[Route(path: 'flash')]
class FlashController extends AbstractController
{
use FormControllerTrait;
use FormControllerBase;
use DeleteFormTrait;
protected const ENTITY = Flash::class;

View File

@ -1,14 +1,16 @@
<?php declare(strict_types=1);
namespace App\Traits;
namespace App\Controller;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\{RedirectResponse, Request, Response};
trait FormControllerTrait
trait FormControllerBase
{
private readonly EntityManagerInterface $entityManager;
public function __construct(private readonly EntityManagerInterface $entityManager)
{
}
/**
* Create a form generator

View File

@ -4,7 +4,6 @@ namespace App\Controller;
use App\Entity\Fpu;
use App\Form\FpuType;
use App\Traits\FormControllerTrait;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
@ -14,7 +13,7 @@ use Symfony\Component\Routing\Annotation\Route;
#[Route('/fpu')]
class FpuController extends AbstractController
{
use FormControllerTrait;
use FormControllerBase;
protected const ENTITY = Fpu::class;
protected const TEMPLATE_PATH = 'fpu/';

View File

@ -5,7 +5,6 @@ namespace App\Controller;
use App\Entity\Gpu;
use App\Enum\SlotKey;
use App\Form\GpuType;
use App\Traits\FormControllerTrait;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\{Request, Response};
@ -14,7 +13,7 @@ use Symfony\Component\Routing\Annotation\Route;
#[Route('/gpu')]
class GpuController extends AbstractController
{
use FormControllerTrait;
use FormControllerBase;
protected const ENTITY = Gpu::class;
protected const TEMPLATE_PATH = 'gpu/';

View File

@ -4,7 +4,6 @@ namespace App\Controller;
use App\Entity\GpuCore;
use App\Form\GPUCoreType;
use App\Traits\FormControllerTrait;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\{Request, Response};
@ -13,7 +12,7 @@ use Symfony\Component\Routing\Annotation\Route;
#[Route('/gpu-core')]
class GpuCoreController extends AbstractController
{
use FormControllerTrait;
use FormControllerBase;
protected const ENTITY = GpuCore::class;
protected const TEMPLATE_PATH = 'gpu_core/';

View File

@ -4,7 +4,8 @@ namespace App\Controller;
use App\Entity\Lenses;
use App\Form\LensesType;
use App\Traits\{DeleteFormTrait, FormControllerTrait};
use App\Controller\
{DeleteFormTrait};
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\FormInterface;
@ -17,7 +18,7 @@ use Symfony\Component\Routing\Annotation\Route;
#[Route(path: 'lens')]
class LensesController extends AbstractController
{
use FormControllerTrait;
use FormControllerBase;
use DeleteFormTrait;
protected const ENTITY = Lenses::class;

View File

@ -4,7 +4,7 @@ namespace App\Controller;
use App\Entity\PreviouslyOwnedCamera;
use App\Form\PreviouslyOwnedCameraType;
use App\Traits\FormControllerTrait;
use App\Controller\FormControllerBase;
use Doctrine\ORM\{EntityManagerInterface, ORMInvalidArgumentException};
use LogicException;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
@ -19,7 +19,7 @@ use UnexpectedValueException;
#[Route(path: 'previously-owned-camera')]
class PreviouslyOwnedCameraController extends AbstractController
{
use FormControllerTrait;
use FormControllerBase;
protected const ENTITY = PreviouslyOwnedCamera::class;
protected const TEMPLATE_PATH = 'previouslyownedcamera/';

View File

@ -4,7 +4,7 @@ namespace App\Controller;
use App\Entity\PreviouslyOwnedFlash;
use App\Form\PreviouslyOwnedFlashType;
use App\Traits\FormControllerTrait;
use App\Controller\FormControllerBase;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\{RedirectResponse, Request, Response};
@ -16,7 +16,7 @@ use Symfony\Component\Routing\Annotation\Route;
#[Route(path: 'previously-owned-flash')]
class PreviouslyOwnedFlashController extends AbstractController
{
use FormControllerTrait;
use FormControllerBase;
protected const ENTITY = PreviouslyOwnedFlash::class;
protected const ROUTE_PREFIX = 'previously-owned-flash_';

View File

@ -4,7 +4,7 @@ namespace App\Controller;
use App\Entity\PreviouslyOwnedLenses;
use App\Form\PreviouslyOwnedLensesType;
use App\Traits\FormControllerTrait;
use App\Controller\FormControllerBase;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\{RedirectResponse, Request, Response};
@ -13,7 +13,7 @@ use Symfony\Component\Routing\Annotation\Route;
#[Route(path: 'previously-owned-lens')]
class PreviouslyOwnedLensesController extends AbstractController
{
use FormControllerTrait;
use FormControllerBase;
protected const ENTITY = PreviouslyOwnedLenses::class;
protected const TEMPLATE_PATH = 'previouslyownedlenses/';

View File

@ -3,8 +3,7 @@
namespace App\Controller;
use App\Entity\Socket;
use App\Form\SocketType;
use Doctrine\ORM\EntityManagerInterface;
use App\Form\SocketTypeForm;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
@ -13,72 +12,40 @@ use Symfony\Component\Routing\Annotation\Route;
#[Route('/socket')]
class SocketController extends AbstractController
{
#[Route('/', name: 'socket_index', methods: ['GET'])]
public function index(EntityManagerInterface $entityManager): Response
{
$sockets = $entityManager
->getRepository(Socket::class)
->findAll();
use FormControllerBase;
return $this->render('socket/index.html.twig', [
'sockets' => $sockets,
]);
protected const ENTITY = Socket::class;
protected const ROUTE_PREFIX = 'socket_';
protected const TEMPLATE_PATH = 'socket/';
protected const FORM = SocketTypeForm::class;
#[Route('/', name: 'socket_index', methods: ['GET'])]
public function index(): Response
{
return $this->itemListView('sockets');
}
#[Route('/new', name: 'socket_new', methods: ['GET', 'POST'])]
public function new(Request $request, EntityManagerInterface $entityManager): Response
public function new(Request $request): Response
{
$socket = new Socket();
$form = $this->createForm(SocketType::class, $socket);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager->persist($socket);
$entityManager->flush();
return $this->redirectToRoute('socket_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('socket/new.html.twig', [
'socket' => $socket,
'form' => $form,
]);
return $this->itemCreate($request, 'sockets');
}
#[Route('/{id}', name: 'socket_show', methods: ['GET'])]
public function show(Socket $socket): Response
{
return $this->render('socket/show.html.twig', [
'socket' => $socket,
]);
return $this->itemView($socket, 'socket');
}
#[Route('/{id}/edit', name: 'socket_edit', methods: ['GET', 'POST'])]
public function edit(Request $request, Socket $socket, EntityManagerInterface $entityManager): Response
public function edit(Request $request, Socket $socket): Response
{
$form = $this->createForm(SocketType::class, $socket);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager->flush();
return $this->redirectToRoute('socket_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('socket/edit.html.twig', [
'socket' => $socket,
'form' => $form,
]);
return $this->itemUpdate($request, $socket, 'socket');
}
#[Route('/{id}', name: 'socket_delete', methods: ['POST'])]
public function delete(Request $request, Socket $socket, EntityManagerInterface $entityManager): Response
public function delete(Request $request, Socket $socket): Response
{
if ($this->isCsrfTokenValid('delete'.$socket->getId(), $request->request->get('_token'))) {
$entityManager->remove($socket);
$entityManager->flush();
}
return $this->redirectToRoute('socket_index', [], Response::HTTP_SEE_OTHER);
return $this->deleteCSRF($request, $socket);
}
}

View File

@ -13,7 +13,7 @@ use Doctrine\ORM\Mapping as ORM;
class Camera
{
use GetSet;
use CameraTrait;
use CameraBase;
#[ORM\Column(name: 'id', type: 'integer', nullable: FALSE)]
#[ORM\Id]

View File

@ -9,7 +9,7 @@ use Doctrine\ORM\Mapping as ORM;
*
* Shared columns for camera, and previously_owned_camera tables
*/
trait CameraTrait
trait CameraBase
{
use PurchasePrice;

View File

@ -12,7 +12,7 @@ use Doctrine\ORM\Mapping as ORM;
class Flash
{
use GetSet;
use FlashTrait;
use FlashBase;
#[ORM\Column(name: 'id', type: 'integer', nullable: FALSE)]
#[ORM\Id]

View File

@ -4,7 +4,7 @@ namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
trait FlashTrait
trait FlashBase
{
use PurchasePrice;

View File

@ -13,7 +13,7 @@ use Doctrine\ORM\Mapping as ORM;
class PreviouslyOwnedCamera
{
use GetSet;
use CameraTrait;
use CameraBase;
#[ORM\Column(name: 'id', type: 'integer', nullable: FALSE)]
#[ORM\Id]

View File

@ -12,7 +12,7 @@ use Doctrine\ORM\Mapping as ORM;
class PreviouslyOwnedFlash
{
use GetSet;
use FlashTrait;
use FlashBase;
#[ORM\Column(name: 'id', type: 'integer', nullable: FALSE)]
#[ORM\Id]

View File

@ -2,7 +2,7 @@
namespace App\Entity;
use App\Enum\SocketTypeEnum;
use App\Enum\SocketType;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
@ -37,9 +37,9 @@ class Socket {
#[ORM\Column(
name: 'socket_type',
type: 'string',
enumType: SocketTypeEnum::class,
enumType: SocketType::class,
)]
private SocketTypeEnum $type = SocketTypeEnum::PIN_GRID_ARRAY;
private SocketType $type = SocketType::PIN_GRID_ARRAY;
// ------------------------------------------------------------------------

View File

@ -2,7 +2,7 @@
namespace App\Enum;
enum SocketTypeEnum: string {
enum SocketType: string {
case DUAL_INLINE_PACKAGE = 'DIP';
case LEAD_LESS_CHIP_CARRIER = 'LLCC';
case PLASTIC_LEADED_CHIP_CARRIER = 'PLCC';

View File

@ -3,13 +3,13 @@
namespace App\Form;
use App\Entity\Socket;
use App\Enum\SocketTypeEnum;
use App\Enum\SocketType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\EnumType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class SocketType extends AbstractType
class SocketTypeForm extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
@ -18,7 +18,7 @@ class SocketType extends AbstractType
->add('otherName')
->add('pinCount')
->add('type', EnumType::class,[
'class' => SocketTypeEnum::class,
'class' => SocketType::class,
])
;
}

View File

@ -11,7 +11,6 @@
namespace App\Maker;
use App\Traits\FormControllerTrait;
use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
use Doctrine\Inflector\Inflector;
use Doctrine\Inflector\InflectorFactory;
@ -165,7 +164,6 @@ final class MakeCollectionCrud extends AbstractMaker
Request::class,
Response::class,
Route::class,
FormControllerTrait::class,
]);
$generator->generateController(

View File

@ -1,6 +1,6 @@
{
"require": {
"friendsofphp/php-cs-fixer": "^3.6",
"rector/rector": "^0.12.16"
"rector/rector": "^0.17.6"
}
}

608
tools/composer.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -135,6 +135,7 @@ return [
'Other' => 'Other',
'Others' => 'Others',
'P ttl type' => 'P-TTL Type',
'Parts' => 'Parts',
'Part number' => 'Part Number',
'PCI 32-bit' => 'PCI 32-bit',
'PCI 33' => 'PCI 33',