Git Product home page Git Product logo

symfony's Introduction

A Symfony Bundle for use together with the php-tmdb/api TMDB API Wrapper.

License License Build Status Build Status codecov PHP Total Downloads

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.

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.

symfony's People

Contributors

danielsunnerberg avatar guilain avatar ivan1986 avatar kefisu avatar ker0x avatar norkunas avatar web-mi avatar wtfzdotnet avatar yukoff avatar z38 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

symfony's Issues

Symfony 6

Dear Developers,

Is it possible to make this library compatible with Symfony 6, which is the actual stable version of Symfony? It would be very useful for me.

This and php-tmdb/api packages require some Symfony libs with version 4 or 5 but less than 6 (e.g. "symfony/event-dispatcher": "^5.0,<6"). Since 5.4 is well supported in this package, and there is no difference between Symfony 5.4 and 6.0 in general, I think it could be possible without any problem. Or do you know any Symfony 5.* feature usage in your libs that was deprecated already in 5.4?

Thank you!

Language choose option

Hey guys,

Is there any option to change "on-the-fly" language of requested elements via tmdb api?
It would be so good if we can integrate it with Symfony native Translations/Locale module.
Is it possible in current version?

Thanks for help! :)

Deprecation notices with latest symfony versions

Autowiring services based on the types they implement is deprecated since Symfony 3.3 and won't be supported in version 4.0. You should rename (or alias) the "tmdb.search_repository" service to "Tmdb\Repository\SearchRepository" instead.

The solution?

  1. change the service ids to match the FQCNs
  2. alias the services to the old services ids
  3. deprecate the aliases : https://symfony.com/doc/current/service_container/alias_private.html#deprecating-services (not sure if that works) it does not work

Support for php-tmdb/api v2.1 and Guzzle

Hello,

I've been trying to use this symfony bundle, but it appears that it does not support the current version of the php-tmdb api, specifically with regards to Guzzle.

After installing via composer, I am getting this message on attempting to flush cache:

vagrant@stovetop:~/webapp$ php bin/console cache:clear -vvv


  [Symfony\Component\Debug\Exception\ContextErrorException]
  Catchable Fatal Error: Argument 1 passed to Kevinrob\GuzzleCache\Strategy\PrivateCacheStrategy::__construct() must be an instance of Kevinrob\GuzzleCache\Storage\CacheStorageInterface, instance of Doctrine\
  Common\Cache\RedisCache given, called in /home/vagrant/webapp/vendor/php-tmdb/api/lib/Tmdb/HttpClient/HttpClient.php on line 454 and defined


Exception trace:
 () at /home/vagrant/webapp/vendor/kevinrob/guzzle-cache-middleware/src/Strategy/PrivateCacheStrategy.php:54
 Kevinrob\GuzzleCache\Strategy\PrivateCacheStrategy->__construct() at /home/vagrant/webapp/vendor/php-tmdb/api/lib/Tmdb/HttpClient/HttpClient.php:454
 Tmdb\HttpClient\HttpClient->setDefaultCaching() at /home/vagrant/webapp/vendor/php-tmdb/api/lib/Tmdb/HttpClient/HttpClient.php:414
 Tmdb\HttpClient\HttpClient->setupCache() at /home/vagrant/webapp/vendor/php-tmdb/api/lib/Tmdb/HttpClient/HttpClient.php:401
 Tmdb\HttpClient\HttpClient->processOptions() at /home/vagrant/webapp/vendor/php-tmdb/api/lib/Tmdb/HttpClient/HttpClient.php:97
 Tmdb\HttpClient\HttpClient->__construct() at /home/vagrant/webapp/vendor/php-tmdb/api/lib/Tmdb/Client.php:182
 Tmdb\Client->constructHttpClient() at /home/vagrant/webapp/vendor/php-tmdb/api/lib/Tmdb/Client.php:73
 Tmdb\Client->__construct() at /home/vagrant/webapp/var/cache/dev/appDevDebugProjectContainer.php:3465
 appDevDebugProjectContainer->getTmdb_ClientService() at /home/vagrant/webapp/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Container.php:310
 Symfony\Component\DependencyInjection\Container->get() at /home/vagrant/webapp/var/cache/dev/appDevDebugProjectContainer.php:2251
 appDevDebugProjectContainer->getImage_HelperService() at /home/vagrant/webapp/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Container.php:310
 Symfony\Component\DependencyInjection\Container->get() at /home/vagrant/webapp/var/cache/dev/appDevDebugProjectContainer.php:2264
 appDevDebugProjectContainer->getImage_TwigExtensionService() at /home/vagrant/webapp/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Container.php:310
 Symfony\Component\DependencyInjection\Container->get() at /home/vagrant/webapp/var/cache/dev/appDevDebugProjectContainer.php:4152
 appDevDebugProjectContainer->getTwigService() at /home/vagrant/webapp/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Container.php:310
 Symfony\Component\DependencyInjection\Container->get() at /home/vagrant/webapp/var/cache/dev/appDevDebugProjectContainer.php:493
 appDevDebugProjectContainer->getCacheWarmerService() at /home/vagrant/webapp/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Container.php:310
 Symfony\Component\DependencyInjection\Container->get() at /home/vagrant/webapp/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Kernel.php:496
 Symfony\Component\HttpKernel\Kernel->initializeContainer() at /home/vagrant/webapp/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Kernel.php:116
 Symfony\Component\HttpKernel\Kernel->boot() at /home/vagrant/webapp/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Console/Application.php:68
 Symfony\Bundle\FrameworkBundle\Console\Application->doRun() at /home/vagrant/webapp/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:120
 Symfony\Component\Console\Application->run() at /home/vagrant/webapp/bin/console:29

Update: I am also getting basically the same error when I attempt to access my site from the browser.

Question: Doctrine

Good morning all.

First of all sorry for my poor English.

I was wondering about the persistence of the data retrieved through this bundle.

I didn't know where to start but it was probably due to my shortcomings in Symfony and PHP in general.

So I reproduced my own Movie, Genre ... entities with my doctrine annotations.

It works very well but when I go through the Bundle and see the seriousness and the complexity of the proposed code and methods I find it a shame not to be able to use them, for example for the use of the MovieFactory to create a Movie entity of the bundle (therefore without my doctrine annotations) from the data retrieved from the repository.

Do you have an implementation idea to overcome this problem without rewriting the code already offered by this bundle?

(I looked in the doc and I did not find an answer to my question ...)

Thank you in advance !!

phpunit-bridge conflict in Symfony 5

Hi! I'm trying to use this library in a Symfony 5 project, but ran into a composer conflict. This seems to be because Symfony adds a "require: 5.2.*" to the "extra" configuration in composer.json.

Because this bundle requires "symfony/phpunit-bridge": "^4.4" this creates a conflict. I think this could be resolved by also accepting <6, as is done with the other Symfony dependencies.

"symfony/phpunit-bridge": "^4.4", would become "symfony/phpunit-bridge": "^4.4 || <6",.

Composer output:

$ composer require php-tmdb/symfony
Using version ^4.0 for php-tmdb/symfony
./composer.json has been updated
Running composer update php-tmdb/symfony
Loading composer repositories with package information
Updating dependencies
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - php-tmdb/symfony[4.0.4, ..., 4.0.x-dev] require symfony/phpunit-bridge ^4.4 -> found symfony/phpunit-bridge[v4.4.0-BETA1, ..., 4.4.x-dev] but it conflicts with your root composer.json require (^5.2).
    - php-tmdb/symfony[4.0.0, ..., 4.0.3] require symfony/phpunit-bridge ^4.2 -> found symfony/phpunit-bridge[v4.2.0-BETA1, ..., 4.4.x-dev] but it conflicts with your root composer.json require (^5.2).
    - Root composer.json requires php-tmdb/symfony ^4.0 -> satisfiable by php-tmdb/symfony[4.0.0, ..., 4.0.x-dev].

non-existent service "tmdb.cache_handler"

Just upgrading from "wtfzdotnet/php-tmdb-api": ">=1.2.0" to the latest version "php-tmdb/symfony": "~2.0" and have an issue.

The service "tmdb.client" has a dependency on a non-existent service "tmdb.cache_handler".

If I comment out /php-tmdb/symfony/Resources/config/services.xml [32-34] it works

<!--            <call method="setCacheHandler">
                <argument type="service" id="tmdb.cache_handler" />
            </call>-->

This is my configuration:

tmdb_symfony:
    api_key: x
    options:
        cache:
            enabled: true
            path: "/tmp/php-tmdb-api"

        log:
            enabled: true
            path: "/tmp/php-tmdb-api.log"  

Great package by the way, thank you.

Symfony 5 compatibility

Hello,

I'm using your bundle since Symfony 2 and it is working fine.

I have to migrate my application to Symfony 5.
Any plan to make your bundle compatible ?

PHP fatal error on php-nighly: incompatible declarations of createCollection()

Runtime: PHP 7.3.0-dev

PHP fatal error on php-nighly: incompatible declarations of createCollection() and deprecation warning on each()

Error

Fatal error: Declaration of Tmdb\Factory\People\CastFactory::createCollection(array $data = Array, $person = NULL) must be compatible with Tmdb\Factory\PeopleFactory::createCollection(array $data = Array, $person = NULL, $collection = NULL) in /home/travis/build/php-tmdb/symfony/vendor/php-tmdb/api/lib/Tmdb/Factory/People/CastFactory.php on line 22

UP: Deprecations caused by phpunit, there's nothing here we can do about them.

composer

Hi,
I am trying all day to run it locally and after that copy some parts of composer to my webhosting. But I´m not able to do that.

Can you please make some pack that I will only upload to my webhosting and will work?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.