Skip to content

Commit 41d170e

Browse files
committed
Add Laravel 5 ServiceProvider
1 parent 295a645 commit 41d170e

4 files changed

Lines changed: 273 additions & 106 deletions

File tree

README.md

Lines changed: 112 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
# Simple Twitter API Wrapper #
1+
# Simple Twitter API Wrapper
22

33
[![Build Status](https://travis-ci.org/codezero-be/twitter.svg?branch=master)](https://travis-ci.org/codezero-be/twitter)
4+
45
[![Latest Stable Version](https://poser.pugx.org/codezero/twitter/v/stable.svg)](https://packagist.org/packages/codezero/twitter)
6+
57
[![Total Downloads](https://poser.pugx.org/codezero/twitter/downloads.svg)](https://packagist.org/packages/codezero/twitter)
8+
69
[![License](https://poser.pugx.org/codezero/twitter/license.svg)](https://packagist.org/packages/codezero/twitter)
710

811
This package hides away the complexity of "talking" to the Twitter API, but instead offers a few simple functions to execute some basic queries.
912

10-
## Features ##
13+
## Features
1114

1215
- All queries require an **API key** and **API secret** to generate App Credentials
1316
- Authorization will be triggered automatically behind the scenes
@@ -17,124 +20,166 @@ This package hides away the complexity of "talking" to the Twitter API, but inst
1720
- Optional Caching (only [Laravel](http://www.laravel.com/ "Laravel") implementation included, see [codezero-be/courier](https://github.com/codezero-be/courier))
1821
- Optional [Laravel](http://www.laravel.com/ "Laravel") ServiceProvider included
1922

20-
## Installation ##
23+
## Installation
2124

2225
Install this package through Composer:
2326

24-
"require": {
25-
"codezero/twitter": "1.*"
26-
}
27+
```
28+
"require": {
29+
"codezero/twitter": "1.*"
30+
}
31+
```
32+
33+
## Setup
34+
35+
### Laravel Setup
36+
37+
After installing, update your Laravel `config/app.php` file to include a reference to this package's service provider in the providers array:
2738

28-
## Setup ##
39+
```
40+
'providers' => [
41+
'CodeZero\Twitter\TwitterServiceProvider'
42+
]
43+
```
2944

30-
### Laravel 4 Setup ###
45+
Next, publish the package config file and [enter your API credentials](#edit-configuration).
3146

32-
After installing, update your `app/config/app.php` file to include a reference to this package's service provider in the providers array:
47+
With Laravel 4.*:
3348

34-
'providers' => [
35-
'CodeZero\Twitter\TwitterServiceProvider'
36-
]
49+
```
50+
php artisan config:publish codezero/twitter
51+
```
3752

38-
Next, create the [config](#edit-configuration "Configuration File") file: `app/config/twitter.php`. Or publish the package config file by running this command in the console:
53+
With Laravel 5.*:
3954

40-
php artisan config:publish codezero/twitter
55+
```
56+
php artisan vendor:publish --provider="CodeZero\Twitter\TwitterServiceProvider"
57+
```
4158

42-
### Manual Setup ###
59+
### Manual Setup
4360

4461
Specify the location of your config file. An example configuration file is included in the `src/config` folder. You can put this anywhere you want.
4562

46-
$config = '/path/to/configFile.php';
63+
```
64+
$config = '/path/to/configFile.php';
65+
```
4766

4867
Create Twitter instance:
4968

50-
use CodeZero\Twitter\Twitter;
51-
$twitter = new Twitter($config);
69+
```
70+
use CodeZero\Twitter\Twitter;
71+
$twitter = new Twitter($config);
72+
```
5273

53-
## Edit Configuration ##
74+
## Edit Configuration
5475

5576
Your configuration file should look like the following:
5677

57-
<?php
58-
return [
59-
'base_url' => 'https://api.twitter.com/',
60-
'api_version' => '1.1',
61-
'api_key' => '',
62-
'api_secret' => ''
63-
];
78+
```
79+
<?php
80+
return [
81+
'base_url' => 'https://api.twitter.com/',
82+
'api_version' => '1.1',
83+
'api_key' => '',
84+
'api_secret' => ''
85+
];
86+
```
6487

6588
Be sure to enter your API key and API secret. Twitter requires this for all requests. Also, do not include the API version in the `base_url` as this would break the authorization request.
6689

67-
## Usage ##
90+
## Usage
6891

69-
### Set options ###
92+
### Set options
7093

7194
The number of results to return. Each Twitter request has a maximum limit. If you specify a `$count` greater than this limit, the maximum results will be returned. (Default: `10`)
7295

73-
$count = 10;
96+
```
97+
$count = 10;
98+
```
7499

75100
The number of minutes the results of the query should be cached. Twitter sets a request limit per hour, so caching is a good idea. Setting this to `0` (zero) will disable caching. (Default: `30`)
76101

77-
$cacheMinutes = 30;
102+
```
103+
$cacheMinutes = 30;
104+
```
78105

79106
This package includes a `Tweet` object which greatly simplifies the returned results. If you want the full JSON response to be returned, set this to `false`. (Default: `true`)
80107

81-
$returnEntities = true;
108+
```
109+
$returnEntities = true;
110+
```
82111

83-
### Get tweets ###
112+
### Get tweets
84113

85-
try
86-
{
87-
$username = 'laravelphp'; //=> Example...
88-
$tweets = $twitter->getTweetsFromUser($username, $count, $cacheMinutes, $returnEntities);
89-
}
90-
catch (\CodeZero\Twitter\TwitterException $e)
91-
{
92-
$error = $e->getMessage(); //=> user not found etc.
93-
}
94-
### Response formats ###
114+
```
115+
try
116+
{
117+
$username = 'laravelphp'; //=> Example...
118+
$tweets = $twitter->getTweetsFromUser($username, $count, $cacheMinutes, $returnEntities);
119+
}
120+
catch (\CodeZero\Twitter\TwitterException $e)
121+
{
122+
$error = $e->getMessage(); //=> user not found etc.
123+
}
124+
```
95125

96-
#### JSON ####
126+
### Response formats
127+
128+
#### JSON
97129

98130
If you `$returnEntities` is `false`, you get a `CodeZero\Courier\Response` object, which contains the actual JSON response.
99131

100-
$tweets = $twitter->getTweetsFromUser($username);
132+
```
133+
$tweets = $twitter->getTweetsFromUser($username);
101134
102-
echo $tweets; //=> Print the JSON
103-
$json = $tweets->getBody(); //=> Returns the JSON
104-
$array = $tweets->toArray(); //=> Convert JSON to an array
135+
echo $tweets; //=> Print the JSON
136+
$json = $tweets->getBody(); //=> Returns the JSON
137+
$array = $tweets->toArray(); //=> Convert JSON to an array
138+
```
105139

106140
For more information on this `Response` object, refer to [codezero-be/courier](https://github.com/codezero-be/courier).
107141

108-
#### Tweets ####
142+
#### Tweets
109143

110144
If you `$returnEntities` is `true`, you get an array of `CodeZero\Twitter\Entities\Tweet` objects. This is a very simplified `Tweet` object, which only contains the most useful info about the tweet and its user.
111145

112-
$tweets = $twitter->getTweetsFromUser($username);
113-
114-
foreach ($tweets as $tweet)
115-
{
116-
$user = $tweet->user();
117-
$tweetOwner = $user->getName();
118-
$tweetUsername = $user->getUsername();
119-
$tweetText = $tweet->getText();
120-
$tweetDate = $tweet->getCreatedAt();
121-
}
146+
```
147+
$tweets = $twitter->getTweetsFromUser($username);
148+
```
149+
150+
151+
152+
```
153+
foreach ($tweets as $tweet)
154+
{
155+
$user = $tweet->user();
156+
$tweetOwner = $user->getName();
157+
$tweetUsername = $user->getUsername();
158+
$tweetText = $tweet->getText();
159+
$tweetDate = $tweet->getCreatedAt();
160+
}
161+
```
122162

123163
For an overview of all available `Tweet` and `User` information, take a look at the source.
124164

125-
## Available Requests ##
165+
## Available Requests
126166

127-
### Get tweets from a user ###
167+
### Get tweets from a user
128168

129-
$username = 'laravelphp';
130-
$tweets = $twitter->getTweetsFromUser($username, $count, $cacheMinutes, $returnEntities);
169+
```
170+
$username = 'laravelphp';
171+
$tweets = $twitter->getTweetsFromUser($username, $count, $cacheMinutes, $returnEntities);
172+
```
131173

132-
### Search for tweets with a hash tag or keyword ###
174+
### Search for tweets with a hash tag or keyword
133175

134-
$query = '#laravel';
135-
$tweets = $twitter->searchTweets($query, $count, $cacheMinutes, $returnEntities);
176+
```
177+
$query = '#laravel';
178+
$tweets = $twitter->searchTweets($query, $count, $cacheMinutes, $returnEntities);
179+
```
136180

137-
### That's all for now... ###
181+
### That's all for now...
138182

139183
---
140-
[![Analytics](https://ga-beacon.appspot.com/UA-58876018-1/codezero-be/twitter)](https://github.com/igrigorik/ga-beacon)
184+
185+
[![Analytics](https://ga-beacon.appspot.com/UA-58876018-1/codezero-be/twitter)](https://github.com/igrigorik/ga-beacon)

src/ServiceProviders/Laravel4.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace CodeZero\Twitter\ServiceProviders;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
7+
class Laravel4 extends ServiceProvider
8+
{
9+
/**
10+
* Bootstrap the application events.
11+
*
12+
* @return void
13+
*/
14+
public function boot()
15+
{
16+
$this->package('codezero/twitter');
17+
}
18+
19+
/**
20+
* Register the service provider.
21+
*
22+
* @return void
23+
*/
24+
public function register()
25+
{
26+
$this->registerConfigurator();
27+
}
28+
29+
/**
30+
* Get the services provided by the provider.
31+
*
32+
* @return array
33+
*/
34+
public function provides()
35+
{
36+
return ['twitter'];
37+
}
38+
39+
/**
40+
* Register the configurator binding
41+
*/
42+
private function registerConfigurator()
43+
{
44+
$this->app->bind('CodeZero\Twitter\Twitter', function($app)
45+
{
46+
$config = $app['config']->has("twitter")
47+
? $app['config']->get("twitter")
48+
: $app['config']->get("twitter::config");
49+
50+
$configurator = new \CodeZero\Configurator\DefaultConfigurator();
51+
$courier = $app->make('CodeZero\Twitter\TwitterCourier');
52+
$authHelper = new \CodeZero\Twitter\AuthHelper();
53+
$urlHelper = new \CodeZero\Utilities\UrlHelper();
54+
$twitterFactory = new \CodeZero\Twitter\TwitterFactory();
55+
56+
return new \CodeZero\Twitter\Twitter($config, $configurator, $courier, $authHelper, $urlHelper, $twitterFactory);
57+
});
58+
}
59+
}

src/ServiceProviders/Laravel5.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace CodeZero\Twitter\ServiceProviders;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
7+
class Laravel5 extends ServiceProvider
8+
{
9+
/**
10+
* Bootstrap the application.
11+
*
12+
* @return void
13+
*/
14+
public function boot()
15+
{
16+
$this->setPublishPaths();
17+
$this->mergeConfig();
18+
}
19+
20+
/**
21+
* Register the service provider.
22+
*
23+
* @return void
24+
*/
25+
public function register()
26+
{
27+
$this->app->bind(\CodeZero\Twitter\Twitter::class, function ($app) {
28+
$config = config("twitter");
29+
$configurator = new \CodeZero\Configurator\DefaultConfigurator();
30+
$courier = $app->make(\CodeZero\Twitter\TwitterCourier::class);
31+
$authHelper = new \CodeZero\Twitter\AuthHelper();
32+
$urlHelper = new \CodeZero\Utilities\UrlHelper();
33+
$twitterFactory = new \CodeZero\Twitter\TwitterFactory();
34+
35+
return new \CodeZero\Twitter\Twitter($config, $configurator, $courier, $authHelper, $urlHelper, $twitterFactory);
36+
});
37+
}
38+
39+
/**
40+
* Set publish paths.
41+
*
42+
* @return void
43+
*/
44+
private function setPublishPaths()
45+
{
46+
$this->publishes([
47+
__DIR__ . '/../config/config.php' => config_path('twitter.php')
48+
], 'config');
49+
}
50+
51+
/**
52+
* Merge configuration files.
53+
*
54+
* @return void
55+
*/
56+
private function mergeConfig()
57+
{
58+
$this->mergeConfigFrom(__DIR__ . '/../config/config.php', 'twitter');
59+
}
60+
}

0 commit comments

Comments
 (0)