Skip to content

Commit

Permalink
minor #1107 [Site] Isolate packages/demo structure (smnandre)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the 2.x branch.

Discussion
----------

[Site] Isolate packages/demo structure

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | no
| License       | MIT

Rework a bit the folder structure / URLs to enforce separation of demos & ux packages
* controllers
* templates
* assets

(+ some minor CS fixes)

Commits
-------

fb738b8 [Site] Isolate packages/demo structure
  • Loading branch information
weaverryan committed Sep 13, 2023
2 parents 2f2eb5f + fb738b8 commit 5d9bf16
Show file tree
Hide file tree
Showing 117 changed files with 871 additions and 527 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ public function testCategoryFieldSubmitsCorrectly()
// so ONLY the placeholder shows up
->assertElementCount('#product_category_autocomplete option', 1)
->assertNotContains('First cat')

->post('/test-form', [
'body' => [
'product' => ['category' => ['autocomplete' => $fooCat->getId()]],
Expand Down
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
12 changes: 0 additions & 12 deletions ux.symfony.com/assets/styles/sections/_nav.scss
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,6 @@
}
}

.AppNav_separator {
display: flex;
color: inherit;
width: 2px;
border-radius: 1px;
height: 1rem;
margin: 0;
border-left: 1px dashed var(--text-color);
opacity: .5;
display: none; // TODO remove if ok
}

.nav-component-img {
height: 22px;
width: 22px;
Expand Down
2 changes: 1 addition & 1 deletion ux.symfony.com/config/routes.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
controllers:
resource: ../src/Controller/
type: annotation
type: attribute

kernel:
resource: ../src/Kernel.php
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Controller;
namespace App\Controller\Demo;

use App\Entity\Invoice;
use App\Entity\TodoItem;
Expand All @@ -14,18 +14,24 @@
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

#[Route('/live-component/demos')]
#[Route('/demos/live-component')]
class LiveComponentDemoController extends AbstractController
{
#[Route('/auto-validating-form', name: 'app_live_components_demo_auto_validating_form')]
#[Route('/', name: 'app_demo_live_component')]
public function __invoke(): Response
{
return $this->redirectToRoute('app_demos');
}

#[Route('/auto-validating-form', name: 'app_demo_live_component_auto_validating_form')]
public function demoAutoValidatingForm(LiveDemoRepository $liveDemoRepository): Response
{
return $this->render('live_component_demo/auto_validating_form.html.twig', [
return $this->render('demos/live_component/auto_validating_form.html.twig', [
'demo' => $liveDemoRepository->find('auto-validating-form'),
]);
}

#[Route('/form-collection-type/{id}', name: 'app_live_components_demo_form_collection_type', defaults: ['id' => null])]
#[Route('/form-collection-type/{id}', name: 'app_demo_live_component_form_collection_type', defaults: ['id' => null])]
public function demoFormCollectionType(LiveDemoRepository $liveDemoRepository, Request $request, TodoListRepository $todoListRepository, TodoList $todoList = null): Response
{
if (!$todoList) {
Expand All @@ -39,80 +45,80 @@ public function demoFormCollectionType(LiveDemoRepository $liveDemoRepository, R
$todoListRepository->add($form->getData(), true);
$this->addFlash('live_demo_success', 'Excellent! With this to-do list, I won’t miss anything.');

return $this->redirectToRoute('app_live_components_demo_form_collection_type', [
return $this->redirectToRoute('app_demo_live_component_form_collection_type', [
'id' => $todoList->getId(),
]);
}

return $this->render('live_component_demo/form_collection_type.html.twig', [
return $this->render('demos/live_component/form_collection_type.html.twig', [
'form' => $form,
'todoList' => $todoList,
'demo' => $liveDemoRepository->find('form-collection-type'),
]);
}

#[Route('/dependent-form-fields', name: 'app_live_components_demo_dependent_form_fields')]
#[Route('/dependent-form-fields', name: 'app_demo_live_component_dependent_form_fields')]
public function demoDependentFormFields(LiveDemoRepository $liveDemoRepository): Response
{
return $this->render('live_component_demo/dependent_form_fields.html.twig', [
return $this->render('demos/live_component/dependent_form_fields.html.twig', [
'demo' => $liveDemoRepository->find('dependent-form-fields'),
]);
}

#[Route('/voting', name: 'app_live_components_demo_voting')]
#[Route('/voting', name: 'app_demo_live_component_voting')]
public function demoVoting(LiveDemoRepository $liveDemoRepository, FoodRepository $foodRepository): Response
{
return $this->render('live_component_demo/voting.html.twig', [
return $this->render('demos/live_component/voting.html.twig', [
'demo' => $liveDemoRepository->find('voting'),
'foods' => $foodRepository->findAll(),
]);
}

#[Route('/inline-edit', name: 'app_live_components_demo_inline_edit')]
#[Route('/inline-edit', name: 'app_demo_live_component_inline_edit')]
public function inlineEdit(LiveDemoRepository $liveDemoRepository, FoodRepository $foodRepository): Response
{
$food = $foodRepository->findOneBy([]);
if (!$food) {
throw $this->createNotFoundException('No food found - try running "php bin/console app:load-data"');
}

return $this->render('live_component_demo/inline_edit.html.twig', parameters: [
'demo' => $liveDemoRepository->find('inline_edit'),
return $this->render('demos/live_component/inline_edit.html.twig', parameters: [
'demo' => $liveDemoRepository->find('inline-edit'),
'food' => $foodRepository->findOneBy([]),
]);
}

#[Route('/chartjs', name: 'app_live_components_demo_chartjs')]
#[Route('/chartjs', name: 'app_demo_live_component_chartjs')]
public function chartJs(LiveDemoRepository $liveDemoRepository): Response
{
return $this->render('live_component_demo/chartjs.html.twig', parameters: [
'demo' => $liveDemoRepository->find('chartjs_updating'),
return $this->render('demos/live_component/chartjs.html.twig', parameters: [
'demo' => $liveDemoRepository->find('chartjs'),
]);
}

#[Route('/invoice/{id}', name: 'app_live_components_invoice', defaults: ['id' => null])]
#[Route('/invoice/{id}', name: 'app_demo_live_component_invoice', defaults: ['id' => null])]
public function invoice(LiveDemoRepository $liveDemoRepository, Invoice $invoice = null): Response
{
$invoice = $invoice ?? new Invoice();

return $this->render('live_component_demo/invoice.html.twig', parameters: [
return $this->render('demos/live_component/invoice.html.twig', parameters: [
'demo' => $liveDemoRepository->find('invoice'),
'invoice' => $invoice,
]);
}

#[Route('/product-form', name: 'app_live_components_product_form')]
#[Route('/product-form', name: 'app_demo_live_component_product_form')]
public function productForm(LiveDemoRepository $liveDemoRepository): Response
{
return $this->render('live_component_demo/product_form.html.twig', parameters: [
'demo' => $liveDemoRepository->find('product_form'),
return $this->render('demos/live_component/product_form.html.twig', parameters: [
'demo' => $liveDemoRepository->find('product-form'),
]);
}

#[Route('/upload', name: 'app_live_components_upload')]
#[Route('/upload', name: 'app_demo_live_component_upload')]
public function uploadFiles(LiveDemoRepository $liveDemoRepository): Response
{
return $this->render('live_component_demo/upload.html.twig', parameters: [
return $this->render('demos/live_component/upload.html.twig', parameters: [
'demo' => $liveDemoRepository->find('upload'),
]);
}
Expand Down
19 changes: 19 additions & 0 deletions ux.symfony.com/src/Controller/DemosController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Controller;

use App\Service\LiveDemoRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class DemosController extends AbstractController
{
#[Route('/demos', name: 'app_demos')]
public function __invoke(LiveDemoRepository $liveDemoRepository): Response
{
return $this->render('main/demos.html.twig', [
'liveComponentDemos' => $liveDemoRepository->findAll(),
]);
}
}
19 changes: 0 additions & 19 deletions ux.symfony.com/src/Controller/LiveComponentController.php

This file was deleted.

21 changes: 2 additions & 19 deletions ux.symfony.com/src/Controller/MainController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
namespace App\Controller;

use App\Model\RecipeFileTree;
use App\Service\PackageRepository;
use App\Service\UxPackageRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class MainController extends AbstractController
{
#[Route('/', name: 'app_homepage')]
public function homepage(PackageRepository $packageRepository): Response
public function homepage(UxPackageRepository $packageRepository): Response
{
$packages = $packageRepository->findAll();

Expand All @@ -21,20 +20,4 @@ public function homepage(PackageRepository $packageRepository): Response
'recipeFileTree' => new RecipeFileTree(),
]);
}

#[Route('/packages', name: 'app_all_packages')]
public function allPackages(PackageRepository $packageRepository): Response
{
$packages = $packageRepository->findAll();

return $this->render('main/packages.html.twig', [
'packages' => $packages,
]);
}

#[Route('/components')]
public function componentsRedirect(): RedirectResponse
{
return $this->redirectToRoute('app_all_packages', [], Response::HTTP_MOVED_PERMANENTLY);
}
}
73 changes: 73 additions & 0 deletions ux.symfony.com/src/Controller/UxPackage/AutocompleteController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace App\Controller\UxPackage;

use App\Entity\Food;
use App\Form\TimeForAMealForm;
use App\Service\UxPackageRepository;
use Doctrine\Common\Collections\Collection;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class AutocompleteController extends AbstractController
{
#[Route('/autocomplete', name: 'app_autocomplete')]
public function __invoke(UxPackageRepository $packageRepository, Request $request): Response
{
$package = $packageRepository->find('autocomplete');

$form = $this->createForm(TimeForAMealForm::class);

$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();

$this->addFlash(
'autocomplete_success',
$this->generateEatingMessage(
$data['foods'],
$data['name']
)
);

return $this->redirectToRoute('app_autocomplete');
}

return $this->render('ux_packages/autocomplete.html.twig', [
'package' => $package,
'form' => $form,
]);
}

private function getDeliciousWord(): string
{
$words = ['delicious', 'scrumptious', 'mouth-watering', 'life-changing', 'world-beating', 'freshly-squeezed'];

return $words[array_rand($words)];
}

private function generateEatingMessage(Collection $foods, string $name): string
{
$i = 0;
$foodStrings = $foods->map(function (Food $food) use (&$i, $foods) {
++$i;
$str = $food->getName();

if ($i === \count($foods) && $i > 1) {
$str = 'and '.$str;
}

return $str;
});

return sprintf(
'Time for %s! Enjoy %s %s %s!',
$name,
\count($foodStrings) > 1 ? 'some' : 'a',
$this->getDeliciousWord(),
implode(\count($foodStrings) > 2 ? ', ' : ' ', $foodStrings->toArray())
);
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
<?php

namespace App\Controller;
namespace App\Controller\UxPackage;

use App\Service\UxPackageRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\UX\Chartjs\Builder\ChartBuilderInterface;
use Symfony\UX\Chartjs\Model\Chart;

class ChartController extends AbstractController
class ChartjsController extends AbstractController
{
#[Route('/chartjs', name: 'app_chartjs')]
public function chartjs(ChartBuilderInterface $chartBuilder): Response
public function __invoke(UxPackageRepository $packageRepository, ChartBuilderInterface $chartBuilder): Response
{
$chart = $chartBuilder->createChart(Chart::TYPE_LINE);
$package = $packageRepository->find('chartjs');

$chart = $chartBuilder->createChart(Chart::TYPE_LINE);
$chart->setData([
'labels' => ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
'datasets' => [
Expand All @@ -34,12 +36,12 @@ public function chartjs(ChartBuilderInterface $chartBuilder): Response
],
],
]);

$chart->setOptions([
'maintainAspectRatio' => false,
]);

return $this->render('chart/chartjs.html.twig', [
return $this->render('ux_packages/chartjs.html.twig', [
'package' => $package,
'chart' => $chart,
]);
}
Expand Down
Loading

0 comments on commit 5d9bf16

Please sign in to comment.