Skip to content

Commit

Permalink
add native Lumen support (#18)
Browse files Browse the repository at this point in the history
* add native Lumen support

* add missing bindings to the container

* change deprecated ServiceBuilder to StorageClient

* add travis
  • Loading branch information
retnek authored and cedricziel committed Jan 27, 2018
1 parent 86ce73a commit 8a7ffa9
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 5 deletions.
12 changes: 12 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
language: php
sudo: false
php:
- '7.0'
- '7.1'

install:
- composer install --no-interaction --prefer-source
- composer require laravel/lumen-framework

script:
- php vendor/bin/phpunit
13 changes: 10 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
{
"name": "websight/l5-google-cloud-storage",
"description": "Laravel 5 Flysystem Google Cloud Storage Service Provider",
"description": "Laravel 5 Flysystem Google Cloud Storage Service Provider",
"require": {
"illuminate/filesystem": "~5.0.17|5.1.*|5.2.*|5.3.*|5.4.*|5.5.*",
"illuminate/support": "~5.0.17|5.1.*|5.2.*|5.3.*|5.4.*|5.5.*",
"cedricziel/flysystem-gcs": "^1.0"
},
"keywords": ["laravel", "laravel5", "flysystem"],
"require-dev": {
"phpunit/phpunit": "^6.5"
},
"keywords": ["laravel", "laravel5", "flysystem", "lumen"],
"homepage": "https://github.com/websightgmbh/l5-google-cloud-storage",
"license": "MIT",
"suggest": {
"laravel/lumen-framework": "To test the Lumen bindings"
},
"authors": [
{
"name": "Cedric Ziel",
Expand All @@ -17,7 +23,8 @@
],
"autoload": {
"psr-4": {
"Websight\\GcsProvider\\": "src/Websight/GcsProvider/"
"Websight\\GcsProvider\\": "src/Websight/GcsProvider/",
"Websight\\GcsProvider\\Tests\\": "src/Websight/GcsProvider/tests/"
}
},
"extra": {
Expand Down
18 changes: 18 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit bootstrap="./vendor/autoload.php" colors="true" forceCoversAnnotation="false">
<testsuites>
<testsuite name="lumen-support test suite">
<directory>tests</directory>
</testsuite>
</testsuites>

<filter processUncoveredFilesFromWhitelist="true">
<whitelist>
<directory suffix=".php">src</directory>
<exclude>
<directory>tests</directory>
</exclude>
</whitelist>
</filter>
</phpunit>
54 changes: 52 additions & 2 deletions src/Websight/GcsProvider/CloudStorageServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Websight\GcsProvider;

use CedricZiel\FlysystemGcs\GoogleCloudStorageAdapter;
use Google\Cloud\ServiceBuilder;
use Google\Cloud\Storage\StorageClient;
use Illuminate\Support\ServiceProvider;
use League\Flysystem\Filesystem;
use Storage;
Expand Down Expand Up @@ -35,12 +35,62 @@ public function boot()

if (array_key_exists('credentials', $config) && false === empty($config['credentials'])) {
$serviceBuilderConfig += ['keyFilePath' => $config['credentials']];
$optionalServiceBuilder = new ServiceBuilder($serviceBuilderConfig);
$optionalServiceBuilder = new StorageClient($serviceBuilderConfig);
}

$adapter = new GoogleCloudStorageAdapter($optionalServiceBuilder, $adapterConfiguration);

return new Filesystem($adapter);
});
}

/**
* Register bindings in the container.
*
* @return void
*/
public function register()
{
if (!$this->app->has('filesystem')) {
$this->app->singleton('filesystem', function ($app) {
/** @var \Laravel\Lumen\Application $app */
return $app->loadComponent(
'filesystems',
\Illuminate\Filesystem\FilesystemServiceProvider::class,
'filesystem'
);
});

$this->app->singleton(
\Illuminate\Contracts\Filesystem\Factory::class,
function ($app) {
return new \Illuminate\Filesystem\FilesystemManager($app);
}
);
}

$this->registerFacades();
}

/**
* Register Storage facade.
*
* @return void
*/
protected function registerFacades()
{
if (!class_exists('Storage')) {
class_alias(\Illuminate\Support\Facades\Storage::class, 'Storage');
}
}

/**
* Decides wheter the current app is lumen.
*
* @return bool
*/
protected function isLumen()
{
return str_contains($this->app->version(), 'Lumen');
}
}
62 changes: 62 additions & 0 deletions tests/LumenSupportTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Websight\GcsProvider\Tests;

use PHPUnit\Framework\TestCase;
use Laravel\Lumen\Application;
use Illuminate\Config\Repository;
use Websight\GcsProvider\CloudStorageServiceProvider;

class LumenSupportTest extends TestCase
{
/**
* @var Application
*/
protected $app;

/**
* @var CloudStorageServiceProvider
*/
protected $provider;

public function setUp()
{
if (!class_exists(Application::class)) {
$this->markTestSkipped();
}

$this->app = $this->setupApplication();
$this->provider = $this->setupServiceProvider($this->app);

parent::setUp();
}

protected function setupApplication()
{
$app = new Application(sys_get_temp_dir());
$app->instance('config', new Repository());

$app->withFacades();

return $app;
}

protected function setupServiceProvider(Application $app)
{
$provider = new CloudStorageServiceProvider($app);
$app->register($provider);
$provider->boot();

return $provider;
}

public function testFacadeIsExists()
{
$this->assertTrue(class_exists('Storage'));
}

public function testRequiredProvidersAreRegistered()
{
$this->assertTrue($this->app->has('filesystem'));
}
}

0 comments on commit 8a7ffa9

Please sign in to comment.