Procházet zdrojové kódy

Essais avec ux-live-component

François Drouhard před 2 roky
rodič
revize
f2a6a997c6

+ 11 - 1
assets/controllers.json

@@ -1,4 +1,14 @@
 {
-    "controllers": [],
+    "controllers": {
+        "@symfony/ux-live-component": {
+            "live": {
+                "enabled": true,
+                "fetch": "eager",
+                "autoimport": {
+                    "@symfony/ux-live-component/styles/live.css": true
+                }
+            }
+        }
+    },
     "entrypoints": []
 }

+ 1 - 0
composer.json

@@ -35,6 +35,7 @@
         "symfony/serializer": "6.2.*",
         "symfony/translation": "6.2.*",
         "symfony/twig-bundle": "6.2.*",
+        "symfony/ux-live-component": "^2.7",
         "symfony/validator": "6.2.*",
         "symfony/web-link": "6.2.*",
         "symfony/webpack-encore-bundle": "^1.12",

+ 164 - 1
composer.lock

@@ -4,7 +4,7 @@
         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
         "This file is @generated automatically"
     ],
-    "content-hash": "39e043a805f59582386d250efc1b6e90",
+    "content-hash": "bf18cc20376273b95b84fe3128b6c8cc",
     "packages": [
         {
             "name": "composer/package-versions-deprecated",
@@ -7568,6 +7568,169 @@
             ],
             "time": "2023-02-14T08:44:56+00:00"
         },
+        {
+            "name": "symfony/ux-live-component",
+            "version": "v2.7.1",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/symfony/ux-live-component.git",
+                "reference": "79066fbf5e3e178bddc8cf2a847ce427f6bd9327"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/symfony/ux-live-component/zipball/79066fbf5e3e178bddc8cf2a847ce427f6bd9327",
+                "reference": "79066fbf5e3e178bddc8cf2a847ce427f6bd9327",
+                "shasum": ""
+            },
+            "require": {
+                "php": ">=8.0",
+                "symfony/property-access": "^5.4|^6.0",
+                "symfony/serializer": "^5.4|^6.0",
+                "symfony/ux-twig-component": "^2.5"
+            },
+            "conflict": {
+                "symfony/config": "<5.4.0"
+            },
+            "require-dev": {
+                "doctrine/annotations": "^1.0",
+                "doctrine/doctrine-bundle": "^2.0",
+                "doctrine/orm": "^2.7",
+                "symfony/dependency-injection": "^5.4|^6.0",
+                "symfony/form": "^5.4|^6.0",
+                "symfony/framework-bundle": "^5.4|^6.0",
+                "symfony/phpunit-bridge": "^6.0",
+                "symfony/security-csrf": "^5.4|^6.0",
+                "symfony/twig-bundle": "^5.4|^6.0",
+                "symfony/validator": "^5.4|^6.0",
+                "zenstruck/browser": "^1.2.0",
+                "zenstruck/foundry": "^1.10"
+            },
+            "type": "symfony-bundle",
+            "extra": {
+                "thanks": {
+                    "name": "symfony/ux",
+                    "url": "https://github.com/symfony/ux"
+                }
+            },
+            "autoload": {
+                "psr-4": {
+                    "Symfony\\UX\\LiveComponent\\": "src/"
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "authors": [
+                {
+                    "name": "Symfony Community",
+                    "homepage": "https://symfony.com/contributors"
+                }
+            ],
+            "description": "Live components for Symfony",
+            "homepage": "https://symfony.com",
+            "keywords": [
+                "components",
+                "symfony-ux",
+                "twig"
+            ],
+            "support": {
+                "source": "https://github.com/symfony/ux-live-component/tree/v2.7.1"
+            },
+            "funding": [
+                {
+                    "url": "https://symfony.com/sponsor",
+                    "type": "custom"
+                },
+                {
+                    "url": "https://github.com/fabpot",
+                    "type": "github"
+                },
+                {
+                    "url": "https://tidelift.com/funding/github/packagist/symfony/symfony",
+                    "type": "tidelift"
+                }
+            ],
+            "time": "2023-01-24T15:57:44+00:00"
+        },
+        {
+            "name": "symfony/ux-twig-component",
+            "version": "v2.7.1",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/symfony/ux-twig-component.git",
+                "reference": "d304d6b8b27821f7d7e62cbdff21e417636d87bc"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/symfony/ux-twig-component/zipball/d304d6b8b27821f7d7e62cbdff21e417636d87bc",
+                "reference": "d304d6b8b27821f7d7e62cbdff21e417636d87bc",
+                "shasum": ""
+            },
+            "require": {
+                "php": ">=8.0",
+                "symfony/dependency-injection": "^5.4|^6.0",
+                "symfony/event-dispatcher": "^5.4|^6.0",
+                "symfony/property-access": "^5.4|^6.0",
+                "twig/twig": "^2.0|^3.0"
+            },
+            "conflict": {
+                "symfony/config": "<5.4.0"
+            },
+            "require-dev": {
+                "symfony/framework-bundle": "^5.4|^6.0",
+                "symfony/phpunit-bridge": "^6.0",
+                "symfony/twig-bundle": "^5.4|^6.0",
+                "symfony/webpack-encore-bundle": "^1.15"
+            },
+            "type": "symfony-bundle",
+            "extra": {
+                "thanks": {
+                    "name": "symfony/ux",
+                    "url": "https://github.com/symfony/ux"
+                }
+            },
+            "autoload": {
+                "psr-4": {
+                    "Symfony\\UX\\TwigComponent\\": "src/"
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "authors": [
+                {
+                    "name": "Symfony Community",
+                    "homepage": "https://symfony.com/contributors"
+                }
+            ],
+            "description": "Twig components for Symfony",
+            "homepage": "https://symfony.com",
+            "keywords": [
+                "components",
+                "symfony-ux",
+                "twig"
+            ],
+            "support": {
+                "source": "https://github.com/symfony/ux-twig-component/tree/v2.7.1"
+            },
+            "funding": [
+                {
+                    "url": "https://symfony.com/sponsor",
+                    "type": "custom"
+                },
+                {
+                    "url": "https://github.com/fabpot",
+                    "type": "github"
+                },
+                {
+                    "url": "https://tidelift.com/funding/github/packagist/symfony/symfony",
+                    "type": "tidelift"
+                }
+            ],
+            "time": "2023-01-19T15:55:42+00:00"
+        },
         {
             "name": "symfony/validator",
             "version": "v6.2.8",

+ 2 - 0
config/bundles.php

@@ -13,4 +13,6 @@ return [
     Symfony\WebpackEncoreBundle\WebpackEncoreBundle::class => ['all' => true],
     Symfony\Bundle\DebugBundle\DebugBundle::class => ['dev' => true],
     Twig\Extra\TwigExtraBundle\TwigExtraBundle::class => ['all' => true],
+    Symfony\UX\TwigComponent\TwigComponentBundle::class => ['all' => true],
+    Symfony\UX\LiveComponent\LiveComponentBundle::class => ['all' => true],
 ];

+ 5 - 0
config/routes/ux_live_component.yaml

@@ -0,0 +1,5 @@
+live_component:
+    resource: '@LiveComponentBundle/config/routes.php'
+    prefix: '/_components'
+    # adjust prefix to add localization to your components
+    #prefix: '/{_locale}/_components'

+ 1 - 0
package.json

@@ -5,6 +5,7 @@
         "@hotwired/stimulus": "^3.0.0",
         "@popperjs/core": "^2.10.2",
         "@symfony/stimulus-bridge": "^3.2.1",
+        "@symfony/ux-live-component": "file:vendor/symfony/ux-live-component/assets",
         "@symfony/webpack-encore": "^4.0.0",
         "bootstrap": "^5.2.2",
         "bootstrap-star-rating": "^4.1.2",

+ 52 - 6
src/Controller/DashboardController.php

@@ -2,9 +2,15 @@
 
 namespace App\Controller;
 
+use App\Repository\CommentaireRepository;
 use App\Repository\FilmRepository;
+use App\Repository\GenreRepository;
+use App\Repository\MediaVideoRepository;
+use App\Repository\RealisateurRepository;
+use App\Repository\UserRepository;
 use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
 use Symfony\Bundle\SecurityBundle\Security;
+use Symfony\Component\HttpFoundation\JsonResponse;
 use Symfony\Component\HttpFoundation\Response;
 use Symfony\Component\Routing\Annotation\Route;
 
@@ -12,14 +18,54 @@ class DashboardController extends AbstractController
 {
     #[Route('/dashboard', name: 'app_dashboard')]
     public function index(FilmRepository $filmRepository, Security $security): Response
+    {
+        return $this->render("dashboard/index.html.twig");
+    }
+
+    #[Route('/jsdashboard', name: 'app_json_dashboard')]
+    public function jsonDashboard(
+        FilmRepository $filmRepository,
+        GenreRepository $genreRepository,
+        RealisateurRepository $realisateurRepository,
+        MediaVideoRepository $mediaVideoRepository,
+        CommentaireRepository $commentaireRepository,
+        UserRepository $userRepository,
+        Security $security
+    ) : JsonResponse
     {
         /** @var \App\Entity\User $user */
         $user = $security->getUser();
-        $nbSeen = $filmRepository->nbFilmSeenBy ($user->getId());
-        $nbToSee = $filmRepository->nbFilmToSeeBy($user->getId());
-        return $this->render('dashboard/index.html.twig', [
-            'nbSeen'    => $nbSeen,
-            'nbToSee'   => $nbToSee
-        ]);
+        $userId = $user->getId();
+
+        $nbComsAll = $commentaireRepository->count([]);
+        $nbComsUser = $commentaireRepository->nbComsByUser($userId);
+
+        $nbFilmsAll = $filmRepository->count([]);
+        $nbFilmsSeenByUser = $filmRepository->nbFilmSeenBy($userId);
+        $nbFilmsToSeeByUser = $filmRepository->nbFilmToSeeBy($userId);
+
+        $nbGenres = $genreRepository->count([]);
+        $nbrealisateurs = $realisateurRepository->count([]);
+
+        $nbUsers = $userRepository->count([]);
+
+        $nbMediaVideos = $mediaVideoRepository->count([]);
+
+        $tableau = [
+            'commentaires' => [
+                'all'   => $nbComsAll,
+                'user'  => $nbComsUser
+            ],
+            'films' => [
+                'all'   => $nbFilmsAll,
+                'seen-by-user'  => $nbFilmsSeenByUser,
+                'to-see-by-user' => $nbFilmsToSeeByUser 
+            ],
+            'genres' => $nbGenres,
+            'realisateurs' => $nbrealisateurs,
+            'users' => $nbUsers,
+            'medias'    => $nbMediaVideos
+        ];
+        return new JsonResponse($tableau, 200, [], false);
     }
 }

+ 11 - 1
src/Repository/CommentaireRepository.php

@@ -19,7 +19,7 @@ class CommentaireRepository extends ServiceEntityRepository
         parent::__construct($registry, Commentaire::class);
     }
 
-    public function findLast ($limit = 5)
+    public function findLast ($limit = 5): array
     {
         return $qb = $this->createQueryBuilder('c')
             ->innerJoin('c.film', 'flm')->addSelect('flm')
@@ -29,4 +29,14 @@ class CommentaireRepository extends ServiceEntityRepository
             ->getQuery()
             ->getResult();
     }
+
+    public function nbComsByUser(int $userId): int
+    {
+        return $this->createQueryBuilder('c')
+            ->where('c.user = :userId')
+            ->select('COUNT (c.id)')
+            ->setParameter(':userId', $userId)
+            ->getQuery()
+            ->getSingleScalarResult();
+    }
 }

+ 9 - 1
src/Repository/MediaVideoRepository.php

@@ -2,12 +2,20 @@
 
 namespace App\Repository;
 
+use App\Entity\MediaVideo;
+use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
+use Doctrine\Persistence\ManagerRegistry;
+
 /**
  * MediaVideoRepository
  *
  * This class was generated by the Doctrine ORM. Add your own custom
  * repository methods below.
  */
-class MediaVideoRepository extends \Doctrine\ORM\EntityRepository
+class MediaVideoRepository extends ServiceEntityRepository
 {
+    public function __construct(ManagerRegistry $registry)
+    {
+        parent::__construct($registry, MediaVideo::class);
+    }
 }

+ 23 - 0
src/Service/StatLoader.php

@@ -0,0 +1,23 @@
+<?php
+
+namespace App\Service;
+
+use Symfony\Contracts\HttpClient\HttpClientInterface;
+
+class StatLoader
+{
+    public function __construct(protected HttpClientInterface $httpClient)
+    {
+        
+    }
+
+    public function getStats(): ?string
+    {
+        $request = $this->httpClient->request(
+            'GET',
+            'jsdashboard'
+        );
+
+        return $request->getContent();
+    }
+}

+ 29 - 0
src/Twig/DashboardComponent.php

@@ -0,0 +1,29 @@
+<?php
+
+namespace App\Twig;
+
+use App\Service\StatLoader;
+use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
+use Symfony\UX\LiveComponent\Attribute\LiveProp;
+use Symfony\UX\LiveComponent\DefaultActionTrait;
+
+#[AsLiveComponent('dashboard-component')]
+class DashboardComponent
+{
+    use DefaultActionTrait;
+
+    #[LiveProp()]
+    public ?string $test = null;
+
+    public function __construct(protected StatLoader $statLoader)
+    {
+
+    }
+
+    public function getStats(): ?string
+    {
+        return $this->statLoader->getStats();
+    }
+
+
+}

+ 15 - 0
symfony.lock

@@ -522,6 +522,21 @@
             "templates/base.html.twig"
         ]
     },
+    "symfony/ux-live-component": {
+        "version": "2.7",
+        "recipe": {
+            "repo": "github.com/symfony/recipes",
+            "branch": "main",
+            "version": "2.6",
+            "ref": "73e69baf18f47740d6f58688c5464b10cdacae06"
+        },
+        "files": [
+            "config/routes/ux_live_component.yaml"
+        ]
+    },
+    "symfony/ux-twig-component": {
+        "version": "v2.7.1"
+    },
     "symfony/validator": {
         "version": "5.4",
         "recipe": {

+ 20 - 0
templates/components/dashboard-component.html.twig

@@ -0,0 +1,20 @@
+<div {{ attributes }}>
+    <div class="d-flex">
+        <div class="card m-3">
+            <div class="card-header">
+                <h4>Films vus</h4>
+            </div>
+            <div class="card-body text-center">
+                {{ dump(this.getStats()) }}
+            </div>
+        </div>
+        <div class="card m-3">
+            <div class="card-header">
+                <h4>A voir</h4>
+            </div>
+            <div class="card-body text-center">
+                
+            </div>
+        </div>
+    </div>
+</div>

+ 1 - 18
templates/dashboard/index.html.twig

@@ -3,22 +3,5 @@
 {% block title %}{{ parent() }} - {{"dashboard" | trans }}{% endblock %}
 {% block titre %}{{ "dashboard" | trans }}{% endblock %}
 {% block body %}
-    <div class="d-flex">
-        <div class="card m-3">
-            <div class="card-header">
-                <h4>Films vus</h4>
-            </div>
-            <div class="card-body text-center">
-                {{ nbSeen }}
-            </div>
-        </div>
-        <div class="card m-3">
-            <div class="card-header">
-                <h4>A voir</h4>
-            </div>
-            <div class="card-body text-center">
-                {{ nbToSee }}
-            </div>
-        </div>
-    </div>
+    {{ component ('dashboard-component') }}
 {% endblock %}

+ 10 - 0
yarn.lock

@@ -1025,6 +1025,11 @@
     loader-utils "^2.0.0"
     schema-utils "^3.0.0"
 
+"@symfony/ux-live-component@file:vendor/symfony/ux-live-component/assets":
+  version "1.0.0"
+  dependencies:
+    morphdom "^2.6.1"
+
 "@symfony/webpack-encore@^4.0.0":
   version "4.2.0"
   resolved "https://registry.yarnpkg.com/@symfony/webpack-encore/-/webpack-encore-4.2.0.tgz#1eec87e3fca9a410563511eb557f70461c10cce5"
@@ -2979,6 +2984,11 @@ minimatch@^3.1.1:
   dependencies:
     brace-expansion "^1.1.7"
 
+morphdom@^2.6.1:
+  version "2.7.0"
+  resolved "https://registry.yarnpkg.com/morphdom/-/morphdom-2.7.0.tgz#9ef0c4bc15ac8725df398d127c6984f62e7f89e8"
+  integrity sha512-8L8DwbdjjWwM/aNqj7BSoSn4G7SQLNiDcxCnMWbf506jojR6lNQ5YOmQqXEIE8u3C492UlkN4d0hQwz97+M1oQ==
+
 ms@2.0.0:
   version "2.0.0"
   resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"