php-tmdb / symfony
Symfony Bundle for TMDB (The Movie Database) API. Provides easy access to the php-tmdb/api library.
Installs: 47 161
Dependents: 0
Suggesters: 0
Security: 0
Stars: 35
Watchers: 7
Forks: 27
Open Issues: 1
Type:symfony-bundle
Requires
- php: ^7.4 || ^8.0
- php-tmdb/api: ^4.0
- symfony/config: ^5.4 || ^6.0
- symfony/dependency-injection: ^5.4 || ^6.0
- symfony/event-dispatcher: ^5.4 || ^6.0
- symfony/http-kernel: ^5.4 || ^6.0
- symfony/yaml: ^5.4 || ^6.0
- twig/twig: ^3.0
Requires (Dev)
- nyholm/psr7: ^1.2
- php-http/cache-plugin: ^1.7
- php-http/guzzle7-adapter: ^0.1
- phpstan/phpstan: ^1.0
- phpunit/phpunit: ^7.5 || ^8.0 || ^9.3
- slevomat/coding-standard: ^6.4.1
- squizlabs/php_codesniffer: ^3.5.8
- symfony/framework-bundle: ^5.4 || ^6.0
- symfony/phpunit-bridge: ^5.4 || ^6.0
- vimeo/psalm: ^4.0
This package is auto-updated.
Last update: 2024-10-20 00:38:26 UTC
README
Compatible with Symfony 5 and 6, PHP 7.4 and up.
Buy me a coffee, or a beer :-)
My stomach will appreciate your donation!
Installation
- Install Composer
- Install php-tmdb/api dependencies
- For development within Symfony we recommend making use of Symfony's PSR-18 HTTP Client
Symfony\Component\HttpClient\Psr18Client
, as when non-cached results pass your profiler will be filled with data.
- For development within Symfony we recommend making use of Symfony's PSR-18 HTTP Client
Then require the bundle:
composer require php-tmdb/symfony:^4
Configuration
Register the bundle in app/bundles.php
:
<?php return [ // --- snip --- Tmdb\SymfonyBundle\TmdbSymfonyBundle::class => ['all' => true], ];
Add to your app/config/config.yml
the following, or replace values with services of your choice ( PSR-18 Http Client / PSR-17 Factories ):
tmdb_symfony: api_key: YOUR_API_KEY_HERE options: http: client: Symfony\Component\HttpClient\Psr18Client request_factory: Nyholm\Psr7\Factory\Psr17Factory response_factory: Nyholm\Psr7\Factory\Psr17Factory stream_factory: Nyholm\Psr7\Factory\Psr17Factory uri_factory: Nyholm\Psr7\Factory\Psr17Factory
services.yaml
:
services: Symfony\Component\HttpClient\Psr18Client: class: Symfony\Component\HttpClient\Psr18Client Nyholm\Psr7\Factory\Psr17Factory: class: Nyholm\Psr7\Factory\Psr17Factory
Configure caching
You can use any PSR-6 cache you wish to use, we will simply use symfony's cache.
When making use of caching, make sure to also include php-http/cache-plugin
in composer, this plugin handles the logic for us,
so we don't have to re-invent the wheel.
You are however also free to choose to implement your own cache listener, or add the caching logic inside the http client of your choice.
composer require php-http/cache-plugin:^1.7
First off configure the cache pool in symfony config/cache.yaml
:
framework: cache: pools: cache.tmdb: adapter: cache.adapter.filesystem default_lifetime: 86400
Then in your tmdb_symfony.yaml
configuration enable the cache and reference this cache pool:
tmdb_symfony: api_key: YOUR_API_KEY_HERE cache: enabled: true adapter: cache.tmdb
Want to make use of logging?
Logging capabilities as of 4.0
allow you to make a fine-grained configuration.
You can use any PSR-3 logger you wish to use, we will simply use monolog.
First off configure the monolog and add a channel and handler:
monolog: channels: - tmdb handlers: tmdb: type: stream path: "%kernel.logs_dir%/php-tmdb--symfony.%kernel.environment%.log" level: info channels: ["tmdb"]
Then in your tmdb_symfony.yaml
configuration:
tmdb_symfony: api_key: YOUR_API_KEY_HERE log: enabled: true adapter: monolog.logger.tmdb hydration: enabled: true with_hydration_data: false # We would only recommend to enable this with an in-memory logger, so you have access to the hydration data within the profiler. adapter: null # you can set different adapters for different logs, leave null to use the main adapter. listener: Tmdb\Event\Listener\Logger\LogHydrationListener formatter: Tmdb\Formatter\Hydration\SimpleHydrationFormatter request_logging: enabled: true adapter: null # you can set different adapters for different logs, leave null to use the main adapter. listener: Tmdb\Event\Listener\Logger\LogHttpMessageListener formatter: Tmdb\Formatter\HttpMessage\SimpleHttpMessageFormatter response_logging: enabled: true adapter: null # you can set different adapters for different logs, leave null to use the main adapter. listener: Tmdb\Event\Listener\Logger\LogHttpMessageListener formatter: Tmdb\Formatter\HttpMessage\SimpleHttpMessageFormatter api_exception_logging: enabled: true adapter: null # you can set different adapters for different logs, leave null to use the main adapter. listener: Tmdb\Event\Listener\Logger\LogApiErrorListener formatter: Tmdb\Formatter\TmdbApiException\SimpleTmdbApiExceptionFormatter client_exception_logging: enabled: true adapter: null # you can set different adapters for different logs, leave null to use the main adapter. listener: Tmdb\Event\Listener\Logger\LogHttpMessageListener formatter: Tmdb\Formatter\HttpMessage\SimpleHttpMessageFormatter
Disable repositories :
tmdb_symfony: api_key: YOUR_API_KEY_HERE repositories: enabled: false
Disable twig extension :
tmdb_symfony: api_key: YOUR_API_KEY_HERE twig_extension: enabled: false
Disable https :
tmdb_symfony: api_key: YOUR_API_KEY_HERE options: secure: enabled: false
Disable legacy aliases :
Set to true to remove all legacy alises ( e.g. tmdb.client
or tmdb.movie_repository
).
tmdb_symfony: api_key: YOUR_API_KEY_HERE disable_legacy_aliases: true
Full configuration with defaults :
tmdb_symfony: api_key: YOUR_API_KEY_HERE cache: enabled: true adapter: cache.tmdb log: enabled: true adapter: monolog.logger.tmdb hydration: enabled: true with_hydration_data: false adapter: null listener: Tmdb\Event\Listener\Logger\LogHydrationListener formatter: Tmdb\Formatter\Hydration\SimpleHydrationFormatter request_logging: enabled: true adapter: null listener: Tmdb\Event\Listener\Logger\LogHttpMessageListener formatter: Tmdb\Formatter\HttpMessage\SimpleHttpMessageFormatter response_logging: enabled: true adapter: null listener: Tmdb\Event\Listener\Logger\LogHttpMessageListener formatter: Tmdb\Formatter\HttpMessage\SimpleHttpMessageFormatter api_exception_logging: enabled: true adapter: null listener: Tmdb\Event\Listener\Logger\LogApiErrorListener formatter: Tmdb\Formatter\TmdbApiException\SimpleTmdbApiExceptionFormatter client_exception_logging: enabled: true adapter: null listener: Tmdb\Event\Listener\Logger\LogHttpMessageListener formatter: Tmdb\Formatter\HttpMessage\SimpleHttpMessageFormatter options: bearer_token: YOUR_BEARER_TOKEN_HERE http: client: Symfony\Component\HttpClient\Psr18Client request_factory: Nyholm\Psr7\Factory\Psr17Factory response_factory: Nyholm\Psr7\Factory\Psr17Factory stream_factory: Nyholm\Psr7\Factory\Psr17Factory uri_factory: Nyholm\Psr7\Factory\Psr17Factory secure: true host: api.themoviedb.org/3 guest_session_token: null event_dispatcher: adapter: event_dispatcher hydration: event_listener_handles_hydration: false only_for_specified_models: { } api_token: YOUR_API_KEY_HERE # you don't have to set this if you set it at the root level session_token: null repositories: enabled: true twig_extension: enabled: true disable_legacy_aliases: false
Usage
Obtaining the client
<?php namespace App; use Tmdb\Client; class MovieParser { private Client $client; // Have Symfony auto-wire the client via your constructor public function __construct(Client $client) { $this->client = $client; } }
Obtaining repositories
<?php namespace App; use Tmdb\Model\AbstractModel; use Tmdb\Repository\MovieRepository; class MovieParser { private MovieRepository $movieRepository; // Have Symfony auto-wire the repository via your constructor public function __construct(MovieRepository $movieRepository) { $this->movieRepository = $movieRepository; } public function findMovie(string $id): AbstractModel { // Use the auto-wired repository in any of your methods return $this->movieRepository->load($id); } }
An overview of all the repositories can be found in the services configuration repositories.xml.
There is also a Twig helper that makes use of the Tmdb\Helper\ImageHelper
to output urls and html.
{{ movie.backdropImage|tmdb_image_url }} {{ movie.backdropImage|tmdb_image_html('original', null, 50)|raw }}
For all all other interactions take a look at php-tmdb/api.