From c3c7689b6743e5d36a6d46730a53b058499ca97a Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 27 Aug 2015 13:14:50 +0200 Subject: [PATCH 1/4] Add a public interface for the language factory so apps can use it --- lib/private/l10n/factory.php | 24 +++++++++++++++--------- lib/private/server.php | 9 ++++++++- lib/public/l10n/ifactory.php | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 10 deletions(-) create mode 100644 lib/public/l10n/ifactory.php diff --git a/lib/private/l10n/factory.php b/lib/private/l10n/factory.php index 4424d014f4..a9ac4da42a 100644 --- a/lib/private/l10n/factory.php +++ b/lib/private/l10n/factory.php @@ -25,29 +25,35 @@ namespace OC\L10N; +use OCP\L10N\IFactory; + /** - * TODO: Description + * A factory that generates language instances */ -class Factory { +class Factory implements IFactory { /** * cached instances */ protected $instances = array(); /** - * get an L10N instance + * Get a language instance * * @param string $app * @param string|null $lang - * @return \OC_L10N + * @return \OCP\IL10N */ public function get($app, $lang = null) { - if (!is_null($lang)) { - return new \OC_L10N($app, $lang); - } else if (!isset($this->instances[$app])) { - $this->instances[$app] = new \OC_L10N($app); + $key = $lang; + if ($key === null) { + $key = 'null'; } - return $this->instances[$app]; + + if (!isset($this->instances[$key][$app])) { + $this->instances[$key][$app] = new \OC_L10N($app, $lang); + } + + return $this->instances[$key][$app]; } } diff --git a/lib/private/server.php b/lib/private/server.php index 287b70eb80..a47fa2e43f 100644 --- a/lib/private/server.php +++ b/lib/private/server.php @@ -673,6 +673,13 @@ class Server extends SimpleContainer implements IServerContainer { return $this->query('AppConfig'); } + /** + * @return \OCP\L10N\IFactory + */ + public function getL10NFactory() { + return $this->query('L10NFactory'); + } + /** * get an L10N instance * @@ -681,7 +688,7 @@ class Server extends SimpleContainer implements IServerContainer { * @return \OC_L10N */ public function getL10N($app, $lang = null) { - return $this->query('L10NFactory')->get($app, $lang); + return $this->getL10NFactory()->get($app, $lang); } /** diff --git a/lib/public/l10n/ifactory.php b/lib/public/l10n/ifactory.php new file mode 100644 index 0000000000..e2dc1c313c --- /dev/null +++ b/lib/public/l10n/ifactory.php @@ -0,0 +1,32 @@ + + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see + * + */ +namespace OCP\L10N; + +interface IFactory { + /** + * Get a language instance + * + * @param string $app + * @param string|null $lang + * @return \OCP\IL10N + */ + public function get($app, $lang = null); +} From a12d3547627611a933b3e70ab5b51c2e130b0e24 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 28 Aug 2015 12:04:52 +0200 Subject: [PATCH 2/4] Deprecate OC_L10N::get() --- lib/private/l10n.php | 9 +++------ lib/public/il10n.php | 2 +- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/lib/private/l10n.php b/lib/private/l10n.php index 17acaac169..168011cfce 100644 --- a/lib/private/l10n.php +++ b/lib/private/l10n.php @@ -81,14 +81,11 @@ class OC_L10N implements \OCP\IL10N { * get an L10N instance * @param string $app * @param string|null $lang - * @return \OC_L10N + * @return \OCP\IL10N + * @deprecated Use \OC::$server->getL10NFactory()->get() instead */ public static function get($app, $lang=null) { - if (is_null($lang)) { - return OC::$server->getL10N($app); - } else { - return new \OC_L10N($app, $lang); - } + return \OC::$server->getL10NFactory()->get($app, $lang); } /** diff --git a/lib/public/il10n.php b/lib/public/il10n.php index e1d0102105..c6e076a21f 100644 --- a/lib/public/il10n.php +++ b/lib/public/il10n.php @@ -100,7 +100,7 @@ interface IL10N { /** - * The code (en, de, ...) of the language that is used for this OC_L10N object + * The code (en, de, ...) of the language that is used for this IL10N object * * @return string language * @since 7.0.0 From bd1215c1dd0955bc8c99a8d6d9005a6fd6bdc8aa Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 28 Aug 2015 12:08:54 +0200 Subject: [PATCH 3/4] Add a test for the interface --- tests/lib/server.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/lib/server.php b/tests/lib/server.php index bc44c50a22..e73fc8b3ab 100644 --- a/tests/lib/server.php +++ b/tests/lib/server.php @@ -90,6 +90,7 @@ class Server extends \Test\TestCase { ['JobList', '\OCP\BackgroundJob\IJobList'], ['L10NFactory', '\OC\L10N\Factory'], + ['L10NFactory', '\OCP\L10N\IFactory'], ['LockingProvider', '\OCP\Lock\ILockingProvider'], ['Logger', '\OC\Log'], ['Logger', '\OCP\ILogger'], From 37b00b7443bec09aac84b359a1c2f77caf143b30 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 28 Aug 2015 15:46:55 +0200 Subject: [PATCH 4/4] Add since tag --- lib/public/l10n/ifactory.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/public/l10n/ifactory.php b/lib/public/l10n/ifactory.php index e2dc1c313c..b784505a68 100644 --- a/lib/public/l10n/ifactory.php +++ b/lib/public/l10n/ifactory.php @@ -20,6 +20,9 @@ */ namespace OCP\L10N; +/** + * @since 8.2.0 + */ interface IFactory { /** * Get a language instance @@ -27,6 +30,7 @@ interface IFactory { * @param string $app * @param string|null $lang * @return \OCP\IL10N + * @since 8.2.0 */ public function get($app, $lang = null); }