Skip to content

Commit 081bdf8

Browse files
committed
Merge pull request #9 from MarkRedeman/rework
Laravel 4 & 5 support and updated to php-tmdb/api version 2.
2 parents 7c8c048 + 1514b44 commit 081bdf8

16 files changed

+810
-389
lines changed

README.md

Lines changed: 139 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
Description
2-
----------------
1+
## Description
2+
A Laraval Package for use together with the [php-tmdb/api](https://github.com/php-tmdb/api) TMDB Wrapper.
3+
This package comes with a service provider that configures the `Tmdb\Client` and registers it to the IoC container.
4+
Both Laravel 5 and 4 are supported.
35

4-
A Laraval Package for use together with the [wtfzdotnet/php-tmdb-api](https://github.com/wtfzdotnet/php-tmdb-api) TMDB Wrapper.
56

6-
Installation
7-
------------
7+
## Installation
88
Install Composer
99

1010
```
@@ -15,43 +15,162 @@ $ sudo mv composer.phar /usr/local/bin/composer
1515
Add the following to your require block in composer.json config
1616

1717
```
18-
"wtfzdotnet/tmdb-package": "~0.1"
18+
"php-tmdb/laravel": "~0.1"
1919
```
2020

21-
Configuration
22-
----------------
23-
Add to your `app/config/app.php` the service provider:
21+
## Configuration
22+
Add to your `app/config/app.php` (Laravel 4) or 'config/app.php' (Laravel 5) the service provider:
2423

2524
```php
26-
// Provider
2725
'providers' => array(
28-
'Wtfz\TmdbPackage\TmdbServiceProvider',
26+
// other service providers
27+
28+
'Tmdb\Laravel\TmdbServiceProvider',
2929
)
3030
```
3131

3232
Then publish the configuration file:
3333

34+
#### Laravel 4:
35+
```
36+
php artisan config:publish php-tmdb/laravel
37+
```
38+
39+
#### Laravel 5:
3440
```
35-
php artisan config:publish wtfzdotnet/tmdb-package
41+
php artisan vendor:publish --provider=php-tmdb/laravel
3642
```
3743

38-
And modify the configuration file located at `app/config/packages/wtfz/tmdb/config.php` accordingly.
44+
Next you can modify the generated configuration file `tmdb.php` accordingly.
3945

4046
That's all! Fire away!
4147

42-
Usage
43-
----------------
48+
## Usage
49+
We can choose to either use the `Tmdb` Facade, or to use dependency injection.
50+
51+
### Facade example
52+
The example below shows how you can use the `Tmdb` facade.
53+
If you don't want to keep adding the `use Tmdb\Laravel\Facades\Tmdb;` statement in your files, then you can also add the facade as an alias in `config/app.php` file.
54+
```php
55+
use Tmdb\Laravel\Facades\Tmdb;
4456

45-
Obtaining the RAW data
57+
class MoviesController {
4658

59+
function show($id)
60+
{
61+
// returns information of a movie
62+
return Tmdb::getMoviesApi()->getMovie($id);
63+
}
64+
}
65+
```
66+
67+
### Dependency injection example
4768
```php
48-
$client = Tmdb::getMoviesApi()->load(13);
69+
use Tmdb\Repository\MovieRepository;
70+
71+
class MoviesController {
72+
73+
private $movies;
74+
75+
function __construct(MovieRepository $movies)
76+
{
77+
$this->movies = $movies;
78+
}
79+
80+
function index()
81+
{
82+
// returns information of a movie
83+
return $this->movies->getPopular();
84+
}
85+
}
86+
```
87+
88+
### Listening to events
89+
We can easily listen to events that are dispatched using the Laravel event dispatcher that we're familiar with.
90+
The following example listens to any request that is made and logs a message.
91+
```php
92+
use Log;
93+
use Event;
94+
use Tmdb\Event\TmdbEvents;
95+
use Tmdb\Event\RequestEvent;
96+
97+
Event::listen(TmdbEvents::REQUEST, function(RequestEvent $event) {
98+
Log::info("A request was made to TMDB");
99+
// do stuff with $event
100+
});
101+
```
102+
In Laravel 5 instead of using the `Event` facade we could also have used the `EventServiceProvider` to register our event listener.
103+
104+
### Image helper
105+
You can easily use the `ImageHelper` by using dependency injection. The following example shows how to show the poster image of the 20 most popular movies.
106+
107+
```php
108+
namespace App\Http\Controllers;
109+
110+
use Tmdb\Helper\ImageHelper;
111+
use Tmdb\Repository\MovieRepository;
112+
113+
class WelcomeController extends Controller {
114+
115+
private $movies;
116+
private $helepr;
117+
118+
public function __construct(MovieRepository $movies, ImageHelper $helper)
119+
{
120+
$this->movies = $movies;
121+
$this->helper = $helper;
122+
}
123+
124+
/**
125+
* Show the application welcome screen to the user.
126+
*
127+
* @return Response
128+
*/
129+
public function index()
130+
{
131+
$popular = $this->movies->getPopular();
132+
133+
foreach ($popular as $movie)
134+
{
135+
$image = $movie->getPosterImage();
136+
echo ($this->helper->getHtml($image, 'w154', 260, 420));
137+
}
138+
}
139+
140+
}
49141
```
142+
The `Configuration` used by the `Tmdb\Helper\ImageHelper` is automatically loaded by the IoC container.
143+
If you are a Laravel 5.1 (currently not released) user you could also use the blade's new `@inject` functionality,
144+
```
145+
@inject('image', 'Tmdb\Helper\ImageHelper')
50146
51-
Obtaining modeled data
147+
@foreach ($movies as $movie)
148+
{!! $image->getHtml($movie->getPosterImage(), 'w154', 260, 420) !!}
149+
@endforeach
150+
```
52151

152+
### Registering plugins
153+
Plugins can be registered in a service provider using the boot method.
53154
```php
54-
$movie = Tmdb::getMovieRepository()->load(13);
155+
namespace App\Providers;
156+
157+
use Illuminate\Support\ServiceProvider;
158+
use Tmdb\HttpClient\Plugin\LanguageFilterPlugin;
159+
160+
class TmdbServiceProvider extends ServiceProvider {
161+
162+
/**
163+
* Add a Dutch language filter to the Tmdb client
164+
*
165+
* @return void
166+
*/
167+
public function boot()
168+
{
169+
$plugin = new LanguageFilterPlugin('nl');
170+
$client = $this->app->make('Tmdb\Client');
171+
$client->getHttpClient()->addSubscriber($plugin);
172+
}
173+
}
55174
```
56175

57-
**For all all other interactions take a look at [wtfzdotnet/php-tmdb-api](https://github.com/wtfzdotnet/php-tmdb-api).**
176+
**For all all other interactions take a look at [php-tmdb/api](https://github.com/php-tmdb/api).**

composer.json

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
{
2-
"name": "wtfzdotnet/tmdb-package",
2+
"name": "php-tmdb/laravel",
33
"description": "Laravel Package for TMDB ( The Movie Database ) API. Provides easy access to the wtfzdotnet/php-tmdb-api library.",
44
"authors": [
55
{
6-
"name": "Michael Roterman",
7-
"email": "[email protected]"
6+
"name": "Mark Redeman",
7+
"email": "[email protected]"
88
}
99
],
1010
"require": {
1111
"php": ">=5.4.0",
12-
"illuminate/support": "4.2.*",
13-
"wtfzdotnet/php-tmdb-api": "~1.1"
12+
"illuminate/support": "~5.0 || ~4.0",
13+
"php-tmdb/api": "~2.0"
1414
},
1515
"require-dev": {
16+
"phpunit/phpunit": "~4.0",
17+
"symfony/event-dispatcher": "~2.0",
18+
"illuminate/events": "~5.0",
1619
"doctrine/cache": ">=1.3.0",
1720
"monolog/monolog": ">=1.7.0"
1821
},
@@ -21,8 +24,8 @@
2124
"monolog/monolog": "Required if you want to make use of logging features."
2225
},
2326
"autoload": {
24-
"psr-0": {
25-
"Wtfz\\TmdbPackage\\": "src/"
27+
"psr-4": {
28+
"Tmdb\\Laravel\\": "src/"
2629
}
2730
},
2831
"minimum-stability": "stable"
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
<?php
2+
/**
3+
* @package php-tmdb\laravel
4+
* @author Mark Redeman <[email protected]>
5+
* @copyright (c) 2014, Mark Redeman
6+
*/
7+
namespace Tmdb\Laravel\Adapters;
8+
9+
use Symfony\Component\EventDispatcher\Event;
10+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
11+
use Symfony\Component\EventDispatcher\EventDispatcherInterface as SymfonyDispatcher;
12+
13+
/**
14+
* This adapter provides a Laravel integration for applications
15+
* using the Symfony EventDispatcherInterface
16+
* It passes any request on to a Symfony Dispatcher and only
17+
* uses the Laravel Dispatcher only when dispatching events
18+
*/
19+
abstract class EventDispatcherAdapter implements SymfonyDispatcher
20+
{
21+
22+
/**
23+
* The Laravel Events Dispatcher
24+
* @var Illuminate\Contracts\Events\Dispatcher or Illuminate\Events\Dispatcher
25+
*/
26+
protected $laravelDispatcher;
27+
28+
/**
29+
* The Symfony Event Dispatcher
30+
* @var Symfony\Component\EventDispatcher
31+
*/
32+
protected $symfonyDispatcher;
33+
34+
/**
35+
* Dispatches an event to all registered listeners.
36+
*
37+
* @param string $eventName The name of the event to dispatch. The name of
38+
* the event is the name of the method that is
39+
* invoked on listeners.
40+
* @param Event $event The event to pass to the event handlers/listeners.
41+
* If not supplied, an empty Event instance is created.
42+
*
43+
* @return Event
44+
*
45+
* @api
46+
*/
47+
public function dispatch($eventName, Event $event = null)
48+
{
49+
if ($event == null)
50+
{
51+
$event = new Event();
52+
}
53+
54+
$event->setName($eventName);
55+
$event->setDispatcher($this);
56+
57+
$this->laravelDispatcher->fire($eventName, $event);
58+
$this->symfonyDispatcher->dispatch($eventName, $event);
59+
60+
$event->setDispatcher($this);
61+
62+
return $event;
63+
}
64+
65+
/**
66+
* Adds an event listener that listens on the specified events.
67+
*
68+
* @param string $eventName The event to listen on
69+
* @param callable $listener The listener
70+
* @param int $priority The higher this value, the earlier an event
71+
* listener will be triggered in the chain (defaults to 0)
72+
*
73+
* @api
74+
*/
75+
public function addListener($eventName, $listener, $priority = 0)
76+
{
77+
$this->symfonyDispatcher->addListener($eventName, $listener, $priority);
78+
}
79+
80+
/**
81+
* Adds an event subscriber.
82+
*
83+
* The subscriber is asked for all the events he is
84+
* interested in and added as a listener for these events.
85+
*
86+
* @param EventSubscriberInterface $subscriber The subscriber.
87+
*
88+
* @api
89+
*/
90+
public function addSubscriber(EventSubscriberInterface $subscriber)
91+
{
92+
$this->symfonyDispatcher->addSubscriber($subscriber);
93+
}
94+
95+
/**
96+
* Removes an event listener from the specified events.
97+
*
98+
* @param string $eventName The event to remove a listener from
99+
* @param callable $listener The listener to remove
100+
*/
101+
public function removeListener($eventName, $listenerToBeRemoved)
102+
{
103+
$this->symfonyDispatcher->removeListener($eventName, $listenerToBeRemoved);
104+
}
105+
106+
/**
107+
* Removes an event subscriber.
108+
*
109+
* @param EventSubscriberInterface $subscriber The subscriber
110+
*/
111+
public function removeSubscriber(EventSubscriberInterface $subscriber)
112+
{
113+
$this->symfonyDispatcher->removeSubscriber($subscriber);
114+
}
115+
116+
/**
117+
* Gets the listeners of a specific event or all listeners.
118+
*
119+
* @param string $eventName The name of the event
120+
*
121+
* @return array The event listeners for the specified event, or all event listeners by event name
122+
*/
123+
public function getListeners($eventName = null)
124+
{
125+
return $this->symfonyDispatcher->getListeners($eventName);
126+
}
127+
128+
/**
129+
* Checks whether an event has any registered listeners.
130+
*
131+
* @param string $eventName The name of the event
132+
*
133+
* @return bool true if the specified event has any listeners, false otherwise
134+
*/
135+
public function hasListeners($eventName = null)
136+
{
137+
return ($this->symfonyDispatcher->hasListeners($eventName) ||
138+
$this->laravelDispatcher->hasListeners($eventName));
139+
}
140+
}

0 commit comments

Comments
 (0)