Skip to content

Commit

Permalink
Merge pull request #240 from moririnson/support-laravel
Browse files Browse the repository at this point in the history
Add Laravel Integration
  • Loading branch information
moririnson authored Oct 9, 2020
2 parents a1f5c4e + 4d910c3 commit d1646ea
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 1 deletion.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,21 @@ See also
A very simple SDK (subset) for the LINE Messaging API for PHP.
line-bot-sdk-tiny provides a simple interface and functions which makes it a good way to learn how to use the LINE Messaging API.

### Laravel Support
Easy to use from Laravel.
After installed, add `LINE_BOT_CHANNEL_ACCESS_TOKEN` and `LINE_BOT_CHANNEL_SECRET` to `.env`

```
LINE_BOT_CHANNEL_ACCESS_TOKEN=<Channel Access Token>
LINE_BOT_CHANNEL_SECRET=<Channel Secret>
```

then you can use `LINEBot` facade like following.

```
$profile = \LINEBot::getProfile($userId);
```

## Help and media

FAQ: https://developers.line.biz/en/faq/
Expand Down
13 changes: 12 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
"phpunit/phpunit": "^4.8.36||^5||^6||^7",
"phpmd/phpmd": "~2.9",
"squizlabs/php_codesniffer": "~3.5",
"ext-posix": "*"
"ext-posix": "*",
"orchestra/testbench": "*"
},
"suggest": {
"apigen/apigen": "Install with roave/better-reflection:dev-master to generate docs",
Expand All @@ -62,5 +63,15 @@
"doc": "apigen generate src --destination docs",
"cs": "phpcs --standard=PSR2 src tests examples/EchoBot/src examples/EchoBot/public examples/KitchenSink/src examples/KitchenSink/public",
"md": "phpmd --ignore-violations-on-exit src,examples/EchoBot/src,examples/EchoBot/public,examples/KitchenSink/src,examples/KitchenSink/public text phpmd.xml"
},
"extra": {
"laravel": {
"providers": [
"LINE\\Laravel\\LINEBotServiceProvider"
],
"aliases": {
"LINEBot": "LINE\\Laravel\\Facade\\LINEBot"
}
}
}
}
4 changes: 4 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit colors="true" bootstrap="./tests/bootstrap.php">
<php>
<env name="LINE_BOT_CHANNEL_ACCESS_TOKEN" value="test_channel_access_token"/>
<env name="LINE_BOT_CHANNEL_SECRET" value="test_channel_secret"/>
</php>
</phpunit>

29 changes: 29 additions & 0 deletions src/Laravel/Facade/LINEBot.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/**
* Copyright 2020 LINE Corporation
*
* LINE Corporation licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

namespace LINE\Laravel\Facade;

use Illuminate\Support\Facades\Facade;

class LINEBot extends Facade
{
public static function getFacadeAccessor()
{
return 'line-bot';
}
}
42 changes: 42 additions & 0 deletions src/Laravel/LINEBotServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/**
* Copyright 2020 LINE Corporation
*
* LINE Corporation licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

namespace LINE\Laravel;

use LINE\LINEBot;
use LINE\LINEBot\HTTPClient\CurlHTTPClient;

class LINEBotServiceProvider extends \Illuminate\Support\ServiceProvider
{
/**
* Register bindings in the container.
*
* @return void
*/
public function register()
{
$this->mergeConfigFrom(
__DIR__ . '/config/line-bot.php',
'line-bot'
);
$this->app->bind('line-bot', function () {
$httpClient = new CurlHTTPClient(config('line-bot.channel_access_token'));
return new LINEBot($httpClient, ['channelSecret' => config('line-bot.channel_secret')]);
});
}
}
22 changes: 22 additions & 0 deletions src/Laravel/config/line-bot.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/**
* Copyright 2020 LINE Corporation
*
* LINE Corporation licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

return [
'channel_access_token' => env('LINE_BOT_CHANNEL_ACCESS_TOKEN'),
'channel_secret' => env('LINE_BOT_CHANNEL_SECRET'),
];
56 changes: 56 additions & 0 deletions tests/Laravel/Facade/LINEBotTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

/**
* Copyright 2020 LINE Corporation
*
* LINE Corporation licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
namespace LINE\Tests\Laravel\Facade;

use LINEBot;

class LINEBotTest extends \Orchestra\Testbench\TestCase
{
/**
* Load package service provider
* @param \Illuminate\Foundation\Application $app
* @return array
*/
protected function getPackageProviders($app)
{
return ['LINE\Laravel\LINEBotServiceProvider'];
}

/**
* Load package alias
* @param \Illuminate\Foundation\Application $app
* @return array
*/
protected function getPackageAliases($app)
{
return [
'LINEBot' => 'LINE\Laravel\Facade\LINEBot',
];
}

/**
* Testing config loaded
*
* @return void
*/
public function testConfigLoaded()
{
$this->assertEquals('test_channel_access_token', config('line-bot.channel_access_token'));
$this->assertEquals('test_channel_secret', config('line-bot.channel_secret'));
}
}

0 comments on commit d1646ea

Please sign in to comment.