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

WIP Configure Satis repository when enabling extension #21

Merged
merged 10 commits into from
Apr 11, 2017
18 changes: 18 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
language: php

php:
- 5.6
- 7.0
- 7.1

before_install:
# include prestissimo which speeds up package download
- composer global require hirak/prestissimo
- composer install --prefer-dist -n

script:
# run the script calling unit tests and so on
- vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover

after_success:
- bash <(curl -s https://codecov.io/bash)
2 changes: 2 additions & 0 deletions bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
$events->subscribe(Listeners\AddApiControllers::class);
$events->subscribe(Listeners\AddClientAssets::class);
$events->subscribe(Listeners\BazaarEnabled::class);
$events->subscribe(Listeners\AddSatisConfiguration::class);

$app->register(Providers\ComposerEnvironmentProvider::class);
$app->register(Providers\ExtensionProvider::class);
$app->register(Providers\ExtensionSearcherProvider::class);
};
3 changes: 1 addition & 2 deletions js/admin/dist/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,7 @@ System.register('flagrow/bazaar/components/BazaarSettingsModal', ['flarum/app',
return [m('div', { className: 'Form-group' }, [m('label', { for: 'bazaar-api-token' }, app.translator.trans('flagrow-bazaar.admin.popup.field.apiToken')), m('input', {
id: 'bazaar-api-token',
className: 'FormControl',
bidi: this.setting('flagrow.bazaar.api_token'),
disabled: this.setting('flagrow.bazaar.api_token')().length > 0
bidi: this.setting('flagrow.bazaar.api_token')
}), m('span', app.translator.trans('flagrow-bazaar.admin.popup.field.apiTokenDescription'))])];
}
}]);
Expand Down
3 changes: 1 addition & 2 deletions js/admin/src/components/BazaarSettingsModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ export default class BazaarSettingsModal extends SettingsModal {
m('input', {
id: 'bazaar-api-token',
className: 'FormControl',
bidi: this.setting('flagrow.bazaar.api_token'),
disabled: this.setting('flagrow.bazaar.api_token')().length > 0
bidi: this.setting('flagrow.bazaar.api_token')
}),
m('span', app.translator.trans('flagrow-bazaar.admin.popup.field.apiTokenDescription'))
])
Expand Down
8 changes: 7 additions & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
Expand All @@ -13,7 +14,12 @@
<testsuites>
<testsuite name="all">
<directory suffix="Test.php">./tests</directory>
<exclude>./tests/Bazaar/Composer/composer-tests/</exclude>
</testsuite>
</testsuites>

<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src/</directory>
</whitelist>
</filter>
</phpunit>
4 changes: 3 additions & 1 deletion src/Composer/Commands/BaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ public function run(array $packages = null)

$installer->setUpdate(true);
$installer->setSkipSuggest(true);
$installer->setUpdateWhitelist($packages);
if (!empty($packages)) {
$installer->setUpdateWhitelist($packages);
}
$installer->setWhitelistDependencies(true);
$installer->setOptimizeAutoloader(true);

Expand Down
22 changes: 19 additions & 3 deletions src/Composer/Utils/ComposerFileEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ public function getContents()

/**
* Write to file and handle errors
* @param string $content
* @param string $contents
* @throws CannotWriteComposerFileException
*/
protected function writeFile($content)
protected function writeFile($contents)
{
$status = file_put_contents($this->filename, $content);
$status = file_put_contents($this->filename, $contents);

if ($status === false) {
throw new CannotWriteComposerFileException();
Expand Down Expand Up @@ -94,6 +94,22 @@ public function removePackage($package)
$this->manipulator->removeSubNode('require', $package);
}

/**
* @param $name
* @param $url
* @param array $options
* @param string $type
* @return bool
*/
public function addRepository($name, $url, array $options, $type = 'composer')
{
return $this->manipulator->addRepository($name, [
'type' => $type,
'url' => $url,
'options' => $options
]);
}

/**
* Get a dependency pool
* Based on the protected InitCommand::getPool() method of Composer
Expand Down
16 changes: 16 additions & 0 deletions src/Events/TokenSet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Flagrow\Bazaar\Events;

class TokenSet
{
/**
* @var string
*/
public $token;

public function __construct(string $token)
{
$this->token = $token;
}
}
30 changes: 5 additions & 25 deletions src/Extensions/ExtensionPackageManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,29 @@
use Flagrow\Bazaar\Composer\ComposerCommand;
use Flagrow\Bazaar\Composer\ComposerEnvironment;
use Flagrow\Bazaar\Composer\ComposerOutput;
use Flarum\Foundation\Application;
use Illuminate\Filesystem\Filesystem;
use Psr\Log\LoggerInterface;

class ExtensionPackageManager
{
/**
* @var Application
* @var ComposerEnvironment
*/
protected $app;

/**
* @var Filesystem
*/
protected $filesystem;
protected $env;

/**
* @var LoggerInterface
*/
protected $log;

public function __construct(Application $app, Filesystem $filesystem, LoggerInterface $log)
public function __construct(ComposerEnvironment $env, LoggerInterface $log)
{
$this->app = $app;
$this->filesystem = $filesystem;
$this->env = $env;
$this->log = $log;
}

public function getComposerInstallRoot()
{
return $this->app->basePath();
}

public function getComposerHome()
{
return $this->app->storagePath().'/composer-home';
}

public function getComposerCommand()
{
$env = new ComposerEnvironment($this->getComposerInstallRoot(), $this->getComposerHome(), $this->filesystem);

return new ComposerCommand($env);
return new ComposerCommand($this->env);
}

public function updatePackages()
Expand Down
58 changes: 58 additions & 0 deletions src/Listeners/AddSatisConfiguration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Flagrow\Bazaar\Listeners;

use Flagrow\Bazaar\Composer\ComposerEnvironment;
use Flagrow\Bazaar\Composer\Utils\ComposerFileEditor;
use Flagrow\Bazaar\Events\TokenSet;
use Flagrow\Bazaar\Search\FlagrowApi;
use Illuminate\Contracts\Events\Dispatcher;
use Psr\Log\LoggerInterface;

class AddSatisConfiguration
{
/**
* @var LoggerInterface
*/
private $log;

function __construct(LoggerInterface $log)
{
$this->log = $log;
}

/**
* @param Dispatcher $events
*/
public function subscribe(Dispatcher $events)
{
$events->listen(TokenSet::class, [$this, 'setSatis']);
}

/**
* @param TokenSet $event
*/
public function setSatis(TokenSet $event)
{
$env = app()->make(ComposerEnvironment::class);
$editor = new ComposerFileEditor($env->getComposerJsonPath());
$host = FlagrowApi::getFlagrowHost() . '/api/satis';

// TODO: if a "repositories" key is already present in composer.json but uses an array instead of an object, the command quietly fails
if ($editor->addRepository(
'flagrow',
$host,
[
'http' => [
'header' => [
"Authorization: 'Bearer {$event->token}'"
]
]
]
)) {
$editor->saveToFile();
} else {
$this->log->alert("Could not write flagrow satis repository for host $host to composer.json.");
}
}
}
11 changes: 9 additions & 2 deletions src/Listeners/BazaarEnabled.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

namespace Flagrow\Bazaar\Listeners;

use Flagrow\Bazaar\Events\TokenSet;
use Flagrow\Bazaar\Search\FlagrowApi;
use Flarum\Event\ConfigureWebApp;
use Flarum\Extension\ExtensionManager;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Support\Arr;
use Psr\Http\Message\ResponseInterface;

class BazaarEnabled
Expand All @@ -26,12 +26,17 @@ class BazaarEnabled
* @var FlagrowApi
*/
protected $client;
/**
* @var Dispatcher
*/
protected $events;

public function __construct(ExtensionManager $extensions, SettingsRepositoryInterface $settings, FlagrowApi $client)
public function __construct(ExtensionManager $extensions, SettingsRepositoryInterface $settings, FlagrowApi $client, Dispatcher $events)
{
$this->extensions = $extensions;
$this->settings = $settings;
$this->client = $client;
$this->events = $events;
}

/**
Expand Down Expand Up @@ -79,5 +84,7 @@ protected function storeTokenFromRequest(ResponseInterface $response)
}

$this->settings->set('flagrow.bazaar.api_token', $token);

$this->events->fire(new TokenSet($token));
}
}
20 changes: 20 additions & 0 deletions src/Providers/ComposerEnvironmentProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Flagrow\Bazaar\Providers;

use Flagrow\Bazaar\Composer\ComposerEnvironment;
use Flarum\Foundation\AbstractServiceProvider;
use Illuminate\Filesystem\Filesystem;

class ComposerEnvironmentProvider extends AbstractServiceProvider
{
public function boot(Filesystem $filesystem)
{
$this->app->singleton(ComposerEnvironment::class, function($app) use($filesystem) {
return new ComposerEnvironment(
$app->basePath(),
$app->storagePath('/composer-home'),
$filesystem);
});
}
}
50 changes: 41 additions & 9 deletions src/Search/FlagrowApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@
*/
class FlagrowApi extends Client
{
/**
* @var array
*/
protected static $flarumConfig;

public function __construct(array $config = [])
{
/** @var array $configFile */
$configFile = app('flarum.config');
/** @var ExtensionManager $extensions */
$extensions = app(ExtensionManager::class);
$bazaar = $extensions->getExtension('flagrow-bazaar');
static::$flarumConfig = app('flarum.config');

$host = Arr::get($configFile, 'flagrow', 'https://flagrow.io');
$headers = [];

if ($token = app()->make(SettingsRepositoryInterface::class)->get('flagrow.bazaar.api_token')) {
Expand All @@ -41,16 +41,48 @@ public function __construct(array $config = [])

parent::__construct(array_merge([
'handler' => $stack,
'base_uri' => "$host/api/",
'base_uri' => sprintf("%s/api/", static::getFlagrowHost()),
'headers' => array_merge([
'Accept' => 'application/vnd.api+json, application/json',
'Bazaar-From' => Arr::get($configFile, 'url'),
'Bazaar-From' => static::getFlarumHost(),
'Flarum-Version' => app()->version(),
'Bazaar-Version' => $bazaar ? $bazaar->getVersion() : null
'Bazaar-Version' => static::getBazaarVersion()
], $headers)
], $config));
}

/**
* The hostname to connect with Flagrow.io.
*
* @return string
*/
public static function getFlagrowHost()
{
return Arr::get(static::$flarumConfig, 'flagrow', 'https://flagrow.io');
}

/**
* The url specified in the config.php.
*
* @return string
*/
public static function getFlarumHost()
{
return Arr::get(static::$flarumConfig, 'url');
}

/**
* @return null|string
*/
public static function getBazaarVersion()
{
/** @var ExtensionManager $extensions */
$extensions = app(ExtensionManager::class);
$bazaar = $extensions->getExtension('flagrow-bazaar');

return $bazaar ? $bazaar->getVersion() : null;
}

/**
* Injects updating the connected state for calls to Flagrow.
*
Expand Down
Loading