From efe71766dc3a8dac80d69602d8cef940c7e6be61 Mon Sep 17 00:00:00 2001 From: ARCANEDEV Date: Thu, 3 Oct 2019 17:53:41 +0100 Subject: [PATCH] Updating the package * Allow to install laravel 6 minor releases * Updating the service providers * Cleaning/Refactoring the code --- .travis.yml | 2 - README.md | 6 +- composer.json | 13 ++-- config/localization.php | 2 - src/Contracts/RouteTranslator.php | 12 +-- src/Entities/Locale.php | 6 +- src/Facades/Localization.php | 20 ----- src/Localization.php | 7 +- src/LocalizationServiceProvider.php | 46 ++---------- src/Middleware/LocaleSessionRedirect.php | 2 - ...vider.php => DeferredServicesProvider.php} | 41 +++++++---- src/Traits/HasTranslations.php | 7 +- src/Utilities/LocalesManager.php | 1 - src/Utilities/Negotiator.php | 14 ++-- src/Utilities/RouteTranslator.php | 18 ++--- src/Utilities/Url.php | 24 +++--- tests/LocalizationServiceProviderTest.php | 23 +----- tests/LocalizationTest.php | 9 --- .../DeferredServicesProviderTest.php | 73 +++++++++++++++++++ tests/TestCase.php | 15 +--- 20 files changed, 155 insertions(+), 186 deletions(-) delete mode 100644 src/Facades/Localization.php rename src/Providers/{UtilitiesServiceProvider.php => DeferredServicesProvider.php} (64%) create mode 100644 tests/Providers/DeferredServicesProviderTest.php diff --git a/.travis.yml b/.travis.yml index e170a4b..f6ee926 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,5 @@ language: php -sudo: false - php: - 7.2 - 7.3 diff --git a/README.md b/README.md index 3e7db50..32c6ead 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Localization [![Packagist License][badge_license]](LICENSE.md) [![For Laravel 5/6][badge_laravel]][link-github-repo] +# Localization [![Packagist License][badge_license]](LICENSE.md) [![For Laravel][badge_laravel]][link-github-repo] [![Travis Status][badge_build]][link-travis] [![Coverage Status][badge_coverage]][link-scrutinizer] @@ -21,7 +21,7 @@ Feel free to check out the [releases](https://github.com/ARCANEDEV/Localization/ ### Features * Easy setup & configuration. - * Laravel `5.x` & `6.0` are supported. + * Laravel `5.x | 6.x` are supported. * SEO-Friendly (Search engine optimization). * New extended Router to manage your localized routes. * Translated Eloquent Models. @@ -52,7 +52,7 @@ If you discover any security related issues, please email arcanedev.maroc@gmail. - [All Contributors][link-contributors] [badge_license]: http://img.shields.io/packagist/l/arcanedev/localization.svg?style=flat-square -[badge_laravel]: https://img.shields.io/badge/For-Laravel%205.x%20to%206.0-orange.svg?style=flat-square +[badge_laravel]: https://img.shields.io/badge/Laravel-5.x%20|%206.x-orange.svg?style=flat-square [badge_build]: http://img.shields.io/travis/ARCANEDEV/Localization.svg?style=flat-square [badge_coverage]: https://img.shields.io/scrutinizer/coverage/g/ARCANEDEV/Localization.svg?style=flat-square [badge_quality]: https://img.shields.io/scrutinizer/g/ARCANEDEV/Localization.svg?style=flat-square diff --git a/composer.json b/composer.json index 2c8b35b..997f2ef 100644 --- a/composer.json +++ b/composer.json @@ -16,13 +16,13 @@ "require": { "php": ">=7.2.0", "ext-json": "*", - "arcanedev/support": "~5.0.0" + "arcanedev/support": "^5.0" }, "require-dev": { - "orchestra/testbench": "~4.0.0", - "mockery/mockery": "~1.0", - "phpunit/phpcov": "~6.0", - "phpunit/phpunit": "~8.0" + "orchestra/testbench": "^4.0", + "mockery/mockery": "^1.0", + "phpunit/phpcov": "^6.0", + "phpunit/phpunit": "^8.0" }, "autoload": { "psr-4": { @@ -38,7 +38,8 @@ "extra": { "laravel": { "providers": [ - "Arcanedev\\Localization\\LocalizationServiceProvider" + "Arcanedev\\Localization\\LocalizationServiceProvider", + "Arcanedev\\Localization\\Providers\\DeferredServicesProvider" ] } }, diff --git a/config/localization.php b/config/localization.php index be8e398..1b4f295 100644 --- a/config/localization.php +++ b/config/localization.php @@ -15,8 +15,6 @@ 'redirection-code' => 302, - 'facade' => 'Localization', - 'utf-8-suffix' => '.UTF-8', /* ----------------------------------------------------------------- diff --git a/src/Contracts/RouteTranslator.php b/src/Contracts/RouteTranslator.php index f981b34..11a57ef 100644 --- a/src/Contracts/RouteTranslator.php +++ b/src/Contracts/RouteTranslator.php @@ -90,12 +90,12 @@ public function findTranslatedRouteByPath($path, $locale); /** * Get URL from route name. * - * @param string $locale - * @param string $defaultLocale - * @param string $transKey - * @param array $attributes - * @param bool|false $defaultHidden - * @param bool|false $showHiddenLocale + * @param string|bool $locale + * @param string $defaultLocale + * @param string $transKey + * @param array $attributes + * @param bool|false $defaultHidden + * @param bool|false $showHiddenLocale * * @return string */ diff --git a/src/Entities/Locale.php b/src/Entities/Locale.php index 0739f30..059e7b6 100644 --- a/src/Entities/Locale.php +++ b/src/Entities/Locale.php @@ -181,12 +181,8 @@ private function setScript($script) public function direction() { if (empty($this->direction)) { - $rtlScripts = ['Arab', 'Hebr', 'Mong', 'Tfng', 'Thaa']; - $this->setDirection( - in_array($this->script, $rtlScripts) - ? self::DIRECTION_RIGHT_TO_LEFT - : self::DIRECTION_LEFT_TO_RIGHT + in_array($this->script, ['Arab', 'Hebr', 'Mong', 'Tfng', 'Thaa']) ? self::DIRECTION_RIGHT_TO_LEFT : self::DIRECTION_LEFT_TO_RIGHT ); } diff --git a/src/Facades/Localization.php b/src/Facades/Localization.php deleted file mode 100644 index 43d48d9..0000000 --- a/src/Facades/Localization.php +++ /dev/null @@ -1,20 +0,0 @@ - - */ -class Localization extends Facade -{ - /** - * Get the registered name of the component. - * - * @return string - */ - protected static function getFacadeAccessor() { return LocalizationContract::class; } -} diff --git a/src/Localization.php b/src/Localization.php index 3d54114..3238a95 100644 --- a/src/Localization.php +++ b/src/Localization.php @@ -288,8 +288,6 @@ public function getNonLocalizedURL($url = null) /** * Returns an URL adapted to $locale or current locale. * - * @todo: Refactor this beast - * * @param string|null $locale * @param string|null $url * @param array $attributes @@ -404,7 +402,8 @@ public function localesNavbar() */ private function findTranslatedRouteByUrl($url, $attributes, $locale) { - if (empty($url)) return false; + if (empty($url)) + return false; // check if this url is a translated url foreach ($this->routeTranslator->getTranslatedRoutes() as $translatedRoute) { @@ -494,7 +493,7 @@ public function isLocaleSupported($locale) * * @throws \Arcanedev\Localization\Exceptions\UnsupportedLocaleException */ - private function isLocaleSupportedOrFail($locale) + private function isLocaleSupportedOrFail($locale): void { if ( ! $this->isLocaleSupported($locale)) throw new UnsupportedLocaleException( diff --git a/src/LocalizationServiceProvider.php b/src/LocalizationServiceProvider.php index 0e52c03..97ae9fa 100644 --- a/src/LocalizationServiceProvider.php +++ b/src/LocalizationServiceProvider.php @@ -1,6 +1,6 @@ registerConfig(); - $this->registerProviders([ - Providers\RoutingServiceProvider::class, - Providers\UtilitiesServiceProvider::class, - ]); - $this->registerLocalization(); + + $this->registerProvider(Providers\RoutingServiceProvider::class); } /** * Boot the package. */ - public function boot() + public function boot(): void { - parent::boot(); - $this->publishConfig(); $this->publishViews(); } - - /** - * Get the services provided by the provider. - * - * @return array - */ - public function provides() - { - return [ - Contracts\Localization::class, - ]; - } - - /* ----------------------------------------------------------------- - | Services Methods - | ----------------------------------------------------------------- - */ - - /** - * Register Localization. - */ - private function registerLocalization() - { - $this->singleton(Contracts\Localization::class, Localization::class); - - if ($alias = $this->config()->get('localization.facade')) { - $this->alias($alias, Facades\Localization::class); - - $this->registerAliases(); - } - } } diff --git a/src/Middleware/LocaleSessionRedirect.php b/src/Middleware/LocaleSessionRedirect.php index 1572658..810de09 100644 --- a/src/Middleware/LocaleSessionRedirect.php +++ b/src/Middleware/LocaleSessionRedirect.php @@ -8,8 +8,6 @@ * * @package Arcanedev\Localization\Middleware * @author ARCANEDEV - * - * @todo: Refactoring */ class LocaleSessionRedirect extends Middleware { diff --git a/src/Providers/UtilitiesServiceProvider.php b/src/Providers/DeferredServicesProvider.php similarity index 64% rename from src/Providers/UtilitiesServiceProvider.php rename to src/Providers/DeferredServicesProvider.php index 80c1af1..049eb73 100644 --- a/src/Providers/UtilitiesServiceProvider.php +++ b/src/Providers/DeferredServicesProvider.php @@ -1,20 +1,23 @@ */ -class UtilitiesServiceProvider extends ServiceProvider +class DeferredServicesProvider extends ServiceProvider implements DeferrableProvider { /* ----------------------------------------------------------------- | Main Methods @@ -24,10 +27,9 @@ class UtilitiesServiceProvider extends ServiceProvider /** * Register the service provider. */ - public function register() + public function register(): void { - parent::register(); - + $this->registerLocalization(); $this->registerRouteTranslator(); $this->registerLocalesManager(); $this->registerLocaleNegotiator(); @@ -41,6 +43,7 @@ public function register() public function provides() { return [ + LocalizationContract::class, RouteTranslatorContract::class, LocalesManagerContract::class, NegotiatorContract::class, @@ -48,18 +51,24 @@ public function provides() } /* ----------------------------------------------------------------- - | Utilities + | Services | ----------------------------------------------------------------- */ + /** + * Register Localization. + */ + private function registerLocalization() + { + $this->singleton(LocalizationContract::class, Localization::class); + } + /** * Register RouteTranslator utility. */ private function registerRouteTranslator() { - $this->singleton(RouteTranslatorContract::class, function ($app) { - return new RouteTranslator($app['translator']); - }); + $this->singleton(RouteTranslatorContract::class, RouteTranslator::class); } /** diff --git a/src/Traits/HasTranslations.php b/src/Traits/HasTranslations.php index ee4d6c8..b6b12d9 100644 --- a/src/Traits/HasTranslations.php +++ b/src/Traits/HasTranslations.php @@ -80,12 +80,13 @@ public function setAttribute($key, $value) * * @param string $key * @param string $locale + * @param bool $useFallbackLocale * * @return mixed */ - public function trans($key, $locale = '') + public function trans($key, $locale = '', bool $useFallbackLocale = true) { - return $this->getTranslation($key, $locale); + return $this->getTranslation($key, $locale, $useFallbackLocale); } /*** @@ -121,7 +122,7 @@ public function getTranslations($key = null) $this->guardAgainstNonTranslatableAttribute($key); return array_filter(json_decode($this->getAttributeFromArray($key) ?? '' ?: '{}', true) ?: [], function ($value) { - return $value !== null && $value !== false && $value !== ''; + return $value !== null && $value !== ''; }); } diff --git a/src/Utilities/LocalesManager.php b/src/Utilities/LocalesManager.php index 2914dc0..a4352b4 100644 --- a/src/Utilities/LocalesManager.php +++ b/src/Utilities/LocalesManager.php @@ -141,7 +141,6 @@ public function setDefaultLocale($defaultLocale = null) $this->isDefaultLocaleSupported($defaultLocale); $this->defaultLocale = $defaultLocale; - // TODO: Refresh locales & supportedLocales collection [Locale entity => default property] return $this; } diff --git a/src/Utilities/Negotiator.php b/src/Utilities/Negotiator.php index 64d9d3b..29f30e2 100644 --- a/src/Utilities/Negotiator.php +++ b/src/Utilities/Negotiator.php @@ -3,7 +3,6 @@ use Arcanedev\Localization\Contracts\Negotiator as NegotiatorContract; use Arcanedev\Localization\Entities\LocaleCollection; use Illuminate\Http\Request; -use Locale; /** * Class Negotiator @@ -105,8 +104,6 @@ public function negotiate(Request $request) if ( ! is_null($locale = $this->getFromRemoteHostServer())) return $locale; - // TODO: Adding negotiate form IP Address ?? - return $this->defaultLocale; } @@ -140,14 +137,13 @@ private function getFromHttpAcceptedLanguagesServer() { $httpAcceptLanguage = $this->request->server('HTTP_ACCEPT_LANGUAGE'); - // @codeCoverageIgnoreStart - if ( ! class_exists('Locale') || empty($httpAcceptLanguage)) - return null; - // @codeCoverageIgnoreEnd + $locale = null; - $locale = Locale::acceptFromHttp($httpAcceptLanguage); + if (class_exists('Locale') && ! empty($httpAcceptLanguage)) + $locale = \Locale::acceptFromHttp($httpAcceptLanguage); - if ($this->isSupported($locale)) return $locale; + if ($this->isSupported($locale)) + return $locale; return null; } diff --git a/src/Utilities/RouteTranslator.php b/src/Utilities/RouteTranslator.php index b16ee37..7d9e90b 100644 --- a/src/Utilities/RouteTranslator.php +++ b/src/Utilities/RouteTranslator.php @@ -3,7 +3,7 @@ use Arcanedev\Localization\Contracts\RouteTranslator as RouteTranslatorContract; use Arcanedev\Localization\Entities\LocaleCollection; use Arcanedev\Localization\Exceptions\InvalidTranslationException; -use Illuminate\Translation\Translator; +use Illuminate\Contracts\Translation\Translator; /** * Class RouteTranslator @@ -21,7 +21,7 @@ class RouteTranslator implements RouteTranslatorContract /** * The translator instance. * - * @var \Illuminate\Translation\Translator + * @var \Illuminate\Contracts\Translation\Translator */ private $translator; @@ -47,7 +47,7 @@ class RouteTranslator implements RouteTranslatorContract /** * Create RouteTranslator instance. * - * @param \Illuminate\Translation\Translator $translator + * @param \Illuminate\Contracts\Translation\Translator $translator */ public function __construct(Translator $translator) { @@ -196,12 +196,12 @@ public function findTranslatedRouteByPath($path, $locale) /** * Get URL from route name. * - * @param string $locale - * @param string $defaultLocale - * @param string $transKey - * @param array $attributes - * @param bool|false $defaultHidden - * @param bool|false $showHiddenLocale + * @param string|bool $locale + * @param string $defaultLocale + * @param string $transKey + * @param array $attributes + * @param bool|false $defaultHidden + * @param bool|false $showHiddenLocale * * @return string */ diff --git a/src/Utilities/Url.php b/src/Utilities/Url.php index c529043..4dbadaf 100644 --- a/src/Utilities/Url.php +++ b/src/Utilities/Url.php @@ -9,8 +9,6 @@ * * @package Arcanedev\Localization\Utilities * @author ARCANEDEV - * - * @todo: Refactoring */ class Url implements UrlContract { @@ -96,7 +94,7 @@ public static function unparse($parsed) * * @return array */ - private static function extractAttributesFromRoutes($url, $routes) + private static function extractAttributesFromRoutes(array $url, $routes): array { $attributes = []; @@ -128,7 +126,7 @@ private static function extractAttributesFromRoutes($url, $routes) * * @return bool */ - private static function hasAttributesFromUriPath($url, $path, &$attributes) + private static function hasAttributesFromUriPath($url, $path, &$attributes): bool { $i = 0; $match = true; @@ -166,7 +164,7 @@ private static function hasAttributesFromUriPath($url, $path, &$attributes) * @param string $segment * @param array $attributes */ - private static function extractAttributesFromSegment($url, $path, $i, $j, $segment, &$attributes) + private static function extractAttributesFromSegment($url, $path, $i, $j, $segment, &$attributes): void { // Required parameters if (preg_match('/{[\w]+}/', $segment)) { @@ -196,7 +194,7 @@ private static function extractAttributesFromSegment($url, $path, $i, $j, $segme * * @return array */ - private static function checkParsedUrl(array $parsed) + private static function checkParsedUrl(array $parsed): array { $scheme =& $parsed['scheme']; $user =& $parsed['user']; @@ -220,7 +218,7 @@ private static function checkParsedUrl(array $parsed) * * @return string */ - private static function getUrl(array $parsed) + private static function getUrl(array $parsed): string { return strlen($parsed['scheme']) ? $parsed['scheme'].':'.self::getHierPart($parsed) : ''; } @@ -232,7 +230,7 @@ private static function getUrl(array $parsed) * * @return string */ - private static function getHierPart(array $parsed) + private static function getHierPart(array $parsed): string { return strlen($authority = self::getAuthority($parsed)) ? '//'.$authority.$parsed['path'] @@ -246,7 +244,7 @@ private static function getHierPart(array $parsed) * * @return string */ - private static function getAuthority(array $parsed) + private static function getAuthority(array $parsed): string { $host = self::getHost($parsed); @@ -260,7 +258,7 @@ private static function getAuthority(array $parsed) * * @return string */ - private static function getUserInfo(array $parsed) + private static function getUserInfo(array $parsed): string { return strlen($parsed['pass']) ? $parsed['user'].':'.$parsed['pass'] : ''; } @@ -272,7 +270,7 @@ private static function getUserInfo(array $parsed) * * @return string */ - private static function getHost(array $parsed) + private static function getHost(array $parsed): string { return empty((string) $parsed['port']) ? $parsed['host'] @@ -286,7 +284,7 @@ private static function getHost(array $parsed) * * @return string */ - private static function getQuery(array $parsed) + private static function getQuery(array $parsed): string { return strlen($parsed['query']) ? '?'.$parsed['query'] : ''; } @@ -298,7 +296,7 @@ private static function getQuery(array $parsed) * * @return string */ - private static function getFragment(array $parsed) + private static function getFragment(array $parsed): string { return strlen($parsed['fragment']) ? '#'.$parsed['fragment'] : ''; } diff --git a/tests/LocalizationServiceProviderTest.php b/tests/LocalizationServiceProviderTest.php index 5de5417..306a1a6 100644 --- a/tests/LocalizationServiceProviderTest.php +++ b/tests/LocalizationServiceProviderTest.php @@ -47,8 +47,8 @@ public function it_can_be_instantiated() { $expectations = [ \Illuminate\Support\ServiceProvider::class, - \Arcanedev\Support\ServiceProvider::class, - \Arcanedev\Support\PackageServiceProvider::class, + \Arcanedev\Support\Providers\ServiceProvider::class, + \Arcanedev\Support\Providers\PackageServiceProvider::class, \Arcanedev\Localization\LocalizationServiceProvider::class, ]; @@ -56,23 +56,4 @@ public function it_can_be_instantiated() static::assertInstanceOf($expected, $this->provider); } } - - /** @test */ - public function it_can_provides() - { - $expected = [ - \Arcanedev\Localization\Contracts\Localization::class, - ]; - - static::assertSame($expected, $this->provider->provides()); - } - - /** @test */ - public function it_can_register_localization_facade() - { - static::assertEquals( - $this->app->getLocale(), - \Arcanedev\Localization\Facades\Localization::getDefaultLocale() - ); - } } diff --git a/tests/LocalizationTest.php b/tests/LocalizationTest.php index 4fe2ac0..15528c0 100644 --- a/tests/LocalizationTest.php +++ b/tests/LocalizationTest.php @@ -512,15 +512,6 @@ public function it_can_get_localized_url_with_route_name_from_lang() ); } - /** @test */ - public function it_can_use_facade() - { - static::assertSame( - $this->app->getLocale(), - \Arcanedev\Localization\Facades\Localization::getDefaultLocale() - ); - } - /* ----------------------------------------------------------------- | Other Methods | ----------------------------------------------------------------- diff --git a/tests/Providers/DeferredServicesProviderTest.php b/tests/Providers/DeferredServicesProviderTest.php new file mode 100644 index 0000000..903a5f7 --- /dev/null +++ b/tests/Providers/DeferredServicesProviderTest.php @@ -0,0 +1,73 @@ + + */ +class DeferredServicesProviderTest extends TestCase +{ + /* ----------------------------------------------------------------- + | Properties + | ----------------------------------------------------------------- + */ + + /** @var \Arcanedev\Localization\Providers\DeferredServicesProvider */ + private $provider; + + /* ----------------------------------------------------------------- + | Main Methods + | ----------------------------------------------------------------- + */ + + public function setUp(): void + { + parent::setUp(); + + $this->provider = $this->app->getProvider(DeferredServicesProvider::class); + } + + public function tearDown(): void + { + unset($this->provider); + + parent::tearDown(); + } + + /* ----------------------------------------------------------------- + | Tests + | ----------------------------------------------------------------- + */ + + /** @test */ + public function it_can_be_instantiated() + { + $expectations = [ + \Illuminate\Support\ServiceProvider::class, + \Illuminate\Contracts\Support\DeferrableProvider::class, + \Arcanedev\Support\Providers\ServiceProvider::class, + \Arcanedev\Localization\Providers\DeferredServicesProvider::class, + ]; + + foreach ($expectations as $expected) { + static::assertInstanceOf($expected, $this->provider); + } + } + + /** @test */ + public function it_can_provides() + { + $expected = [ + \Arcanedev\Localization\Contracts\Localization::class, + \Arcanedev\Localization\Contracts\RouteTranslator::class, + \Arcanedev\Localization\Contracts\LocalesManager::class, + \Arcanedev\Localization\Contracts\Negotiator::class, + ]; + + static::assertEquals($expected, $this->provider->provides()); + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php index 76fe407..8838c0b 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -46,20 +46,7 @@ protected function getPackageProviders($app) { return [ \Arcanedev\Localization\LocalizationServiceProvider::class, - ]; - } - - /** - * Get package aliases. - * - * @param \Illuminate\Foundation\Application $app - * - * @return array - */ - protected function getPackageAliases($app) - { - return [ - 'Localization' => \Arcanedev\Localization\Facades\Localization::class, + \Arcanedev\Localization\Providers\DeferredServicesProvider::class, ]; }