Skip to content

Commit 724f5a8

Browse files
committed
Laravel integration
1 parent 52d954d commit 724f5a8

File tree

7 files changed

+240
-1
lines changed

7 files changed

+240
-1
lines changed

README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,30 @@ Add the package to the `require` list of your `composer.json` file.
2525
"scriptotek/sru-client": "dev-master"
2626
},
2727
}
28-
```
28+
```
2929

3030
and run `composer install` to get the latest version of the package.
3131

32+
### Laravel 4/5 integration
33+
34+
In the $providers array add the service providers for this package:
35+
36+
'Scriptotek\Sru\Providers\SruServiceProvider'
37+
38+
Add the facade of this package to the `$aliases` array:
39+
40+
'SruClient' => 'Scriptotek\Sru\Facades\SruClient'
41+
42+
Publish configuration in Laravel 5:
43+
44+
$ php artisan vendor:publish --provider="Scriptotek\Sru\Providers\SruServiceProviderLaravel5"
45+
46+
Publish configuration in Laravel 4:
47+
48+
$ php artisan config:publish scriptotek/sru
49+
50+
In Laravel 5 applications the configuration file is copied to `config/sru.php`, in Laravel 4 applications you will find the file at `app/config/packages/scriptotek/sru/config.php`.
51+
3252
### Example
3353

3454
```php

config/config.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
return array(
3+
/*
4+
|--------------------------------------------------------------------------
5+
| Endpoint
6+
|--------------------------------------------------------------------------
7+
|
8+
| Default endpoint URL to use.
9+
|
10+
*/
11+
'endpoint' => 'URL-TO-ENDPOINT',
12+
13+
/*
14+
|--------------------------------------------------------------------------
15+
| Schema
16+
|--------------------------------------------------------------------------
17+
|
18+
| Default schema to use.
19+
|
20+
*/
21+
'schema' => 'marcxml',
22+
23+
/*
24+
|--------------------------------------------------------------------------
25+
| Version
26+
|--------------------------------------------------------------------------
27+
|
28+
| Default version to use.
29+
|
30+
*/
31+
'version' => '1.1',
32+
33+
/*
34+
|--------------------------------------------------------------------------
35+
| User-Agent
36+
|--------------------------------------------------------------------------
37+
|
38+
| Default user-agent to identify as.
39+
|
40+
*/
41+
'user-agent' => 'MyLaravelSite/0.1',
42+
43+
44+
);
45+

provides.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"providers": [
3+
"Scriptotek\Sru\Providers\SruServiceProvider"
4+
],
5+
"aliases": [
6+
{
7+
"alias": "SruClient",
8+
"facade": "Scriptotek\Sru\Facades\SruClient"
9+
}
10+
]
11+
}

src/Facades/SruClient.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Scriptotek\Sru\Facades;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
7+
class SruClient extends Facade
8+
{
9+
/**
10+
* Get the registered name of the component.
11+
*
12+
* @return string
13+
*/
14+
protected static function getFacadeAccessor()
15+
{
16+
return 'sru-client';
17+
}
18+
}

src/Providers/SruServiceProvider.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
namespace Scriptotek\Sru\Providers;
4+
5+
use Illuminate\Foundation\Application;
6+
use Illuminate\Support\ServiceProvider;
7+
8+
class SruServiceProvider extends ServiceProvider
9+
{
10+
/**
11+
* Indicates if loading of the provider is deferred.
12+
*
13+
* @var bool
14+
*/
15+
protected $defer = false;
16+
/**
17+
* Actual provider
18+
*
19+
* @var \Illuminate\Support\ServiceProvider
20+
*/
21+
protected $provider;
22+
/**
23+
* Create a new service provider instance.
24+
*
25+
* @param \Illuminate\Contracts\Foundation\Application $app
26+
* @return void
27+
*/
28+
public function __construct($app)
29+
{
30+
parent::__construct($app);
31+
$this->provider = $this->getProvider();
32+
}
33+
/**
34+
* Bootstrap the application events.
35+
*
36+
* @return void
37+
*/
38+
public function boot()
39+
{
40+
return $this->provider->boot();
41+
}
42+
/**
43+
* Register the service provider.
44+
*
45+
* @return void
46+
*/
47+
public function register()
48+
{
49+
return $this->provider->register();
50+
}
51+
/**
52+
* Return ServiceProvider according to Laravel version
53+
*
54+
* @return \Scriptotek\Sru\Provider\ProviderInterface
55+
*/
56+
private function getProvider()
57+
{
58+
if (version_compare(Application::VERSION, '5.0', '<')) {
59+
$provider = '\Scriptotek\Sru\SruClientProviderLaravel4';
60+
} else {
61+
$provider = '\Scriptotek\Sru\SruClientProviderLaravel5';
62+
}
63+
return new $provider($this->app);
64+
}
65+
/**
66+
* Get the services provided by the provider.
67+
*
68+
* @return array
69+
*/
70+
public function provides()
71+
{
72+
return array('sru-client');
73+
}
74+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Scriptotek\Sru\Providers;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
use Scriptotek\Sru\Client as SruClient;
7+
8+
class SruServiceProviderLaravel4 extends ServiceProvider
9+
{
10+
/**
11+
* Bootstrap the application events.
12+
*
13+
* @return void
14+
*/
15+
public function boot()
16+
{
17+
$this->package('scriptotek/sru');
18+
}
19+
/**
20+
* Register the service provider.
21+
*
22+
* @return void
23+
*/
24+
public function register()
25+
{
26+
$app = $this->app;
27+
$app['sru-client'] = $app->share(function ($app) {
28+
return new SruClient($app['config']->get('sru-client::config'));
29+
});
30+
$app->alias('sru-client', 'Scriptotek\Sru\Client');
31+
}
32+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Scriptotek\Sru\Providers;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
use Scriptotek\Sru\Client as SruClient;
7+
8+
class SruServiceProviderLaravel5 extends ServiceProvider
9+
{
10+
/**
11+
* Bootstrap the application events.
12+
*
13+
* @return void
14+
*/
15+
public function boot()
16+
{
17+
$this->publishes(array(
18+
__DIR__.'/../../config/config.php' => config_path('sru.php')
19+
));
20+
}
21+
22+
/**
23+
* Register the service provider.
24+
*
25+
* @return void
26+
*/
27+
public function register()
28+
{
29+
$app = $this->app;
30+
$this->mergeConfigFrom(
31+
__DIR__.'/../../config/config.php',
32+
'sru'
33+
);
34+
$app['sru-client'] = $app->share(function ($app) {
35+
return new SruClient($app['config']->get('sru'));
36+
});
37+
$app->alias('sru-client', 'Scriptotek\Sru\Client');
38+
}
39+
}

0 commit comments

Comments
 (0)