diff --git a/framework/core/.travis.yml b/framework/core/.travis.yml index b074b2100..89340f880 100644 --- a/framework/core/.travis.yml +++ b/framework/core/.travis.yml @@ -14,6 +14,7 @@ before_script: script: - vendor/bin/phpcs --standard=PSR2 -np src + - vendor/bin/phpspec run notifications: email: diff --git a/framework/core/composer.json b/framework/core/composer.json index 9122bc4c3..d7e66e32c 100644 --- a/framework/core/composer.json +++ b/framework/core/composer.json @@ -26,7 +26,8 @@ "codeception/codeception": "~2.0.0", "codeception/mockery-module": "*", "laracasts/testdummy": "~2.0", - "squizlabs/php_codesniffer": "2.*" + "squizlabs/php_codesniffer": "2.*", + "phpspec/phpspec": "^2.2" }, "autoload": { "psr-4": { diff --git a/framework/core/composer.lock b/framework/core/composer.lock index 2ee4e0071..ed93e5a0e 100644 --- a/framework/core/composer.lock +++ b/framework/core/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "09afef66b65df77878c1bdd8d51b1eab", + "hash": "6f47932716cfbd88646a021ea58c18b5", "packages": [ { "name": "danielstjules/stringy", @@ -856,7 +856,7 @@ }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/tobscure/json-api/zipball/fe013caafe9ed0797b1b6abb116d514b301fbf1c", + "url": "https://api.github.com/repos/tobscure/json-api/zipball/2629b6f48eb380973848c7e5fc36d41f33b305b3", "reference": "fe013caafe9ed0797b1b6abb116d514b301fbf1c", "shasum": "" }, @@ -1600,6 +1600,118 @@ ], "time": "2015-02-03 12:10:50" }, + { + "name": "phpspec/php-diff", + "version": "v1.0.2", + "source": { + "type": "git", + "url": "https://github.com/phpspec/php-diff.git", + "reference": "30e103d19519fe678ae64a60d77884ef3d71b28a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpspec/php-diff/zipball/30e103d19519fe678ae64a60d77884ef3d71b28a", + "reference": "30e103d19519fe678ae64a60d77884ef3d71b28a", + "shasum": "" + }, + "type": "library", + "autoload": { + "psr-0": { + "Diff": "lib/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Chris Boulton", + "homepage": "http://github.com/chrisboulton", + "role": "Original developer" + } + ], + "description": "A comprehensive library for generating differences between two hashable objects (strings or arrays).", + "time": "2013-11-01 13:02:21" + }, + { + "name": "phpspec/phpspec", + "version": "2.2.1", + "source": { + "type": "git", + "url": "https://github.com/phpspec/phpspec.git", + "reference": "e9a40577323e67f1de2e214abf32976a0352d8f8" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpspec/phpspec/zipball/e9a40577323e67f1de2e214abf32976a0352d8f8", + "reference": "e9a40577323e67f1de2e214abf32976a0352d8f8", + "shasum": "" + }, + "require": { + "doctrine/instantiator": "^1.0.1", + "php": ">=5.3.3", + "phpspec/php-diff": "~1.0.0", + "phpspec/prophecy": "~1.4", + "sebastian/exporter": "~1.0", + "symfony/console": "~2.3", + "symfony/event-dispatcher": "~2.1", + "symfony/finder": "~2.1", + "symfony/process": "~2.1", + "symfony/yaml": "~2.1" + }, + "require-dev": { + "behat/behat": "^3.0.11", + "bossa/phpspec2-expect": "~1.0", + "phpunit/phpunit": "~4.4", + "symfony/filesystem": "~2.1", + "symfony/process": "~2.1" + }, + "suggest": { + "phpspec/nyan-formatters": "~1.0 – Adds Nyan formatters" + }, + "bin": [ + "bin/phpspec" + ], + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.2.x-dev" + } + }, + "autoload": { + "psr-0": { + "PhpSpec": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Konstantin Kudryashov", + "email": "ever.zet@gmail.com", + "homepage": "http://everzet.com" + }, + { + "name": "Marcello Duarte", + "homepage": "http://marcelloduarte.net/" + } + ], + "description": "Specification-oriented BDD framework for PHP 5.3+", + "homepage": "http://phpspec.net/", + "keywords": [ + "BDD", + "SpecBDD", + "TDD", + "spec", + "specification", + "testing", + "tests" + ], + "time": "2015-05-30 15:21:40" + }, { "name": "phpspec/prophecy", "version": "v1.4.1", @@ -2841,6 +2953,55 @@ "homepage": "https://symfony.com", "time": "2015-06-04 20:11:48" }, + { + "name": "symfony/process", + "version": "v2.7.2", + "source": { + "type": "git", + "url": "https://github.com/symfony/Process.git", + "reference": "48aeb0e48600321c272955132d7606ab0a49adb3" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/Process/zipball/48aeb0e48600321c272955132d7606ab0a49adb3", + "reference": "48aeb0e48600321c272955132d7606ab0a49adb3", + "shasum": "" + }, + "require": { + "php": ">=5.3.9" + }, + "require-dev": { + "symfony/phpunit-bridge": "~2.7" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.7-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Component\\Process\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony Process Component", + "homepage": "https://symfony.com", + "time": "2015-07-01 11:25:50" + }, { "name": "symfony/yaml", "version": "v2.7.1", diff --git a/framework/core/spec/Flarum/Core/Settings/MemoryCacheSettingsRepositorySpec.php b/framework/core/spec/Flarum/Core/Settings/MemoryCacheSettingsRepositorySpec.php new file mode 100644 index 000000000..7459b9e33 --- /dev/null +++ b/framework/core/spec/Flarum/Core/Settings/MemoryCacheSettingsRepositorySpec.php @@ -0,0 +1,54 @@ +beConstructedWith($inner); + } + + function it_is_initializable() + { + $this->shouldHaveType('Flarum\Core\Settings\MemoryCacheSettingsRepository'); + } + + function it_retrieves_data_from_inner(SettingsRepository $inner) + { + $settings = ['a' => 1, 'b' => 2]; + $inner->all()->willReturn($settings); + $inner->all()->shouldBeCalled(); + + // Test fetching all settings + $this->all()->shouldReturn($settings); + + // Test fetching single settings + $this->get('a')->shouldReturn(1); + $this->get('b')->shouldReturn(2); + + // Test invalid key + $this->get('c')->shouldReturn(null); + + // Test invalid key with custom default + $this->get('d', 'foobar')->shouldReturn('foobar'); + } + + function it_passes_new_data_to_inner(SettingsRepository $inner) + { + $this->set('a', 1); + $inner->set('a', 1)->shouldHaveBeenCalled(); + } + + function it_caches_new_data(SettingsRepository $inner) + { + $this->set('b', 2); + $this->get('b')->shouldReturn(2); + $inner->all()->shouldNotHaveBeenCalled(); + $inner->get('b')->shouldNotHaveBeenCalled(); + } +} diff --git a/framework/core/src/Console/ImportCommand.php b/framework/core/src/Console/ImportCommand.php index bbd31dc2c..ae0f3a414 100644 --- a/framework/core/src/Console/ImportCommand.php +++ b/framework/core/src/Console/ImportCommand.php @@ -4,15 +4,12 @@ use Illuminate\Console\Command; use Illuminate\Foundation\Application; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputArgument; -use Flarum\Core\Exceptions\ValidationFailureException; -use Flarum\Core\Models\User; -use Flarum\Core\Models\Discussion; -use Flarum\Core\Models\DiscussionState; -use Flarum\Core\Models\CommentPost; +use Flarum\Core\Users\User; +use Flarum\Core\Discussions\Discussion; +use Flarum\Core\Discussions\DiscussionState; +use Flarum\Core\Posts\CommentPost; use Flarum\Tags\Tag; -use Flarum\Core\Events\PostWasPosted; -use Flarum\Core\Events\UserWasRegistered; -use Flarum\Core\Activity\JoinedActivity; +use Flarum\Core\Posts\Events\PostWasPosted; use Symfony\Component\Console\Helper\ProgressBar; class ImportCommand extends Command @@ -23,7 +20,7 @@ class ImportCommand extends Command * * @var string */ - protected $name = 'flarum:import'; + protected $name = 'import'; /** * The console command description. diff --git a/framework/core/src/Core.php b/framework/core/src/Core.php index 8772d1f27..f7ba7546c 100644 --- a/framework/core/src/Core.php +++ b/framework/core/src/Core.php @@ -18,10 +18,6 @@ class Core return $default; } - if (is_null($value = app('flarum.db')->table('config')->where('key', $key)->pluck('value'))) { - $value = $default; - } - - return $value; + return app('Flarum\Core\Settings\SettingsRepository')->get($key, $default); } } diff --git a/framework/core/src/Core/CoreServiceProvider.php b/framework/core/src/Core/CoreServiceProvider.php index a33ebda76..ed3618bab 100644 --- a/framework/core/src/Core/CoreServiceProvider.php +++ b/framework/core/src/Core/CoreServiceProvider.php @@ -1,5 +1,7 @@ app->singleton('Flarum\Core\Settings\SettingsRepository', function() { + return new MemoryCacheSettingsRepository( + new DatabaseSettingsRepository( + $this->app->make('Illuminate\Database\ConnectionInterface') + ) + ); + }); + $this->app->singleton('flarum.forum', 'Flarum\Core\Forum'); // TODO: probably use Illuminate's AggregateServiceProvider diff --git a/framework/core/src/Core/Model.php b/framework/core/src/Core/Model.php index f52b21a04..ee461350a 100755 --- a/framework/core/src/Core/Model.php +++ b/framework/core/src/Core/Model.php @@ -3,7 +3,6 @@ use Illuminate\Contracts\Validation\Factory; use Illuminate\Database\Eloquent\Model as Eloquent; use Illuminate\Database\Eloquent\Relations\Relation; -use Flarum\Core\Exceptions\ValidationFailureException; use LogicException; /** diff --git a/framework/core/src/Core/DatabaseSettingsRepository.php b/framework/core/src/Core/Settings/DatabaseSettingsRepository.php similarity index 73% rename from framework/core/src/Core/DatabaseSettingsRepository.php rename to framework/core/src/Core/Settings/DatabaseSettingsRepository.php index c7d86d5c2..ec726eefe 100644 --- a/framework/core/src/Core/DatabaseSettingsRepository.php +++ b/framework/core/src/Core/Settings/DatabaseSettingsRepository.php @@ -1,10 +1,10 @@ database = $connection; } + public function all() + { + return $this->database->table('config')->lists('value', 'key'); + } + public function get($key, $default = null) { if (is_null($value = $this->database->table('config')->where('key', $key)->pluck('value'))) { diff --git a/framework/core/src/Core/Settings/MemoryCacheSettingsRepository.php b/framework/core/src/Core/Settings/MemoryCacheSettingsRepository.php new file mode 100644 index 000000000..777a7f5c7 --- /dev/null +++ b/framework/core/src/Core/Settings/MemoryCacheSettingsRepository.php @@ -0,0 +1,45 @@ +inner = $inner; + } + + public function all() + { + if (!$this->isCached) { + $this->cache = $this->inner->all(); + $this->isCached = true; + } + + return $this->cache; + } + + public function get($key, $default = null) + { + if (array_key_exists($key, $this->cache)) { + return $this->cache[$key]; + } else if (!$this->isCached) { + return array_get($this->all(), $key, $default); + } + + return $default; + } + + public function set($key, $value) + { + $this->cache[$key] = $value; + + $this->inner->set($key, $value); + } +} diff --git a/framework/core/src/Core/SettingsRepositoryInterface.php b/framework/core/src/Core/Settings/SettingsRepository.php similarity index 52% rename from framework/core/src/Core/SettingsRepositoryInterface.php rename to framework/core/src/Core/Settings/SettingsRepository.php index 5007e0f06..1b226236e 100644 --- a/framework/core/src/Core/SettingsRepositoryInterface.php +++ b/framework/core/src/Core/Settings/SettingsRepository.php @@ -1,9 +1,11 @@ users = $users; + $this->settings = $settings; $this->mailer = $mailer; $this->url = $url; } @@ -53,8 +66,8 @@ class RequestPasswordResetHandler // password route be part of core?? $data = [ 'username' => $user->username, - 'url' => Core::config('base_url').'/reset/'.$token->id, - 'forumTitle' => Core::config('forum_title') + 'url' => $this->settings->get('base_url').'/reset/'.$token->id, + 'forumTitle' => $this->settings->get('forum_title'), ]; $this->mailer->send(['text' => 'flarum::emails.resetPassword'], $data, function (Message $message) use ($user) { diff --git a/framework/core/src/Core/Users/Listeners/EmailConfirmationMailer.php b/framework/core/src/Core/Users/Listeners/EmailConfirmationMailer.php index cc259a077..111d3851a 100755 --- a/framework/core/src/Core/Users/Listeners/EmailConfirmationMailer.php +++ b/framework/core/src/Core/Users/Listeners/EmailConfirmationMailer.php @@ -1,5 +1,6 @@ settings = $settings; $this->mailer = $mailer; } @@ -90,8 +98,8 @@ class EmailConfirmationMailer // email route be part of core?? return [ 'username' => $user->username, - 'url' => Core::config('base_url').'/confirm/'.$token->id, - 'forumTitle' => Core::config('forum_title') + 'url' => $this->settings->get('base_url').'/confirm/'.$token->id, + 'forumTitle' => $this->settings->get('forum_title') ]; } } diff --git a/framework/core/src/Support/ClientAction.php b/framework/core/src/Support/ClientAction.php index d75c1e744..3ad5c5a96 100644 --- a/framework/core/src/Support/ClientAction.php +++ b/framework/core/src/Support/ClientAction.php @@ -5,6 +5,7 @@ use Flarum\Assets\AssetManager; use Flarum\Assets\JsCompiler; use Flarum\Assets\LessCompiler; use Flarum\Core; +use Flarum\Core\Settings\SettingsRepository; use Flarum\Core\Users\User; use Flarum\Locale\JsCompiler as LocaleJsCompiler; use Flarum\Locale\LocaleManager; @@ -61,14 +62,20 @@ abstract class ClientAction extends HtmlAction */ protected $locales; + /** + * @var SettingsRepository + */ + protected $settings; + /** * @param Client $apiClient * @param LocaleManager $locales */ - public function __construct(Client $apiClient, LocaleManager $locales) + public function __construct(Client $apiClient, LocaleManager $locales, SettingsRepository $settings) { $this->apiClient = $apiClient; $this->locales = $locales; + $this->settings = $settings; } /** @@ -156,7 +163,7 @@ abstract class ClientAction extends HtmlAction $assets->addLess("@$name: $value;"); } - $assets->addLess(Core::config('custom_less')); + $assets->addLess($this->settings->get('custom_less')); } /** @@ -168,10 +175,10 @@ abstract class ClientAction extends HtmlAction protected function getLessVariables() { return [ - 'fl-primary-color' => Core::config('theme_primary_color', '#000'), - 'fl-secondary-color' => Core::config('theme_secondary_color', '#000'), - 'fl-dark-mode' => Core::config('theme_dark_mode') ? 'true' : 'false', - 'fl-colored-header' => Core::config('theme_colored_header') ? 'true' : 'false' + 'fl-primary-color' => $this->settings->get('theme_primary_color', '#000'), + 'fl-secondary-color' => $this->settings->get('theme_secondary_color', '#000'), + 'fl-dark-mode' => $this->settings->get('theme_dark_mode') ? 'true' : 'false', + 'fl-colored-header' => $this->settings->get('theme_colored_header') ? 'true' : 'false' ]; } diff --git a/framework/core/src/Support/Extensions/ExtensionsServiceProvider.php b/framework/core/src/Support/Extensions/ExtensionsServiceProvider.php index f986dc062..606b9908e 100644 --- a/framework/core/src/Support/Extensions/ExtensionsServiceProvider.php +++ b/framework/core/src/Support/Extensions/ExtensionsServiceProvider.php @@ -11,13 +11,23 @@ class ExtensionsServiceProvider extends ServiceProvider * @return void */ public function register() + { + } + + /** + * Boot the service provider. + * + * @return void + */ + public function boot() { // Extensions will not be registered if Flarum is not installed yet if (!Core::isInstalled()) { return; } - $extensions = json_decode(Core::config('extensions_enabled'), true); + $config = $this->app->make('Flarum\Core\Settings\SettingsRepository')->get('extensions_enabled'); + $extensions = json_decode($config, true); $providers = []; foreach ($extensions as $extension) {