Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial Laravel support #46

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ matrix:

install:
- travis_retry composer install --no-interaction --no-suggest
- travis_retry composer require --no-interaction illuminate/support:^6.0 vlucas/phpdotenv:^3.3

script:
- mkdir -p build/logs
Expand Down
10 changes: 10 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,15 @@
"phpunit/phpunit": "^6.5",
"mockery/mockery": "^1.0",
"php-coveralls/php-coveralls": "^2.3"
},
"extra": {
"laravel": {
"providers": [
"Fcm\\Laravel\\FcmServiceProvider"
]
}
},
"suggest": {
"laravel/framework": "Required to use Laravel (^6.0)."
}
}
4 changes: 4 additions & 0 deletions src/FcmClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ public function __call(string $name, array $arguments)
// and concatinate it back together.
$group = array_slice($camelCaseSplit, 0, -1);
$group = ucwords(implode('', $group));

if ($group == 'Laravel') {
throw new FcmClientException('Invalid magic method called: cannot instantiate Laravel classes');
}

// The classname is always the last item in the group name.
$class = ucwords(end($camelCaseSplit));
Expand Down
17 changes: 17 additions & 0 deletions src/Laravel/Fcm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Fcm\Laravel;

use Fcm\FcmClient;
use Illuminate\Support\Facades\Facade;

/**
* @method static array send(\Fcm\Request $request)
*/
class Fcm extends Facade
{
protected static function getFacadeAccessor(): string
{
return FcmClient::class;
}
}
41 changes: 41 additions & 0 deletions src/Laravel/FcmServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Fcm\Laravel;

use Fcm\FcmClient;
use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Support\ServiceProvider;

class FcmServiceProvider extends ServiceProvider implements DeferrableProvider
{
public function boot(): void
{
if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__.'/stubs/config.php' => $this->app->configPath('php-fcm.php'),
], 'php-fcm');
}
}

public function register(): void
{
$this->mergeConfigFrom(__DIR__.'/stubs/config.php', 'php-fcm');

$this->app->singleton(FcmClient::class, function ($app) {
$config = $app->make('config');

return new FcmClient(
$config->get('php-fcm.key'),
$config->get('php-fcm.sender_id'),
[
'http_errors' => $config->get('php-fcm.http_errors'),
]
);
});
}

public function provides(): array
{
return [FcmClient::class];
}
}
7 changes: 7 additions & 0 deletions src/Laravel/stubs/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

return [
'key' => env('FCM_API_KEY'),
'sender_id' => env('FCM_SENDER_ID'),
'http_errors' => false,
];