mirror of
https://github.com/devcode-it/openstamanager.git
synced 2025-06-05 22:09:38 +02:00
Correzioni minori e aggiornamento a Laravel 9
This commit is contained in:
131
app/LaravelGettext/Config/ConfigManager.php
Normal file
131
app/LaravelGettext/Config/ConfigManager.php
Normal file
@@ -0,0 +1,131 @@
|
||||
<?php
|
||||
|
||||
namespace App\LaravelGettext\Config;
|
||||
|
||||
use \App\LaravelGettext\Config\Models\Config as ConfigModel;
|
||||
use \App\LaravelGettext\Exceptions\RequiredConfigurationFileException;
|
||||
use \App\LaravelGettext\Exceptions\RequiredConfigurationKeyException;
|
||||
use \Illuminate\Support\Facades\Config;
|
||||
use App\LaravelGettext\Storages\SessionStorage;
|
||||
use Illuminate\Support\Arr;
|
||||
|
||||
class ConfigManager
|
||||
{
|
||||
/**
|
||||
* Config model
|
||||
*
|
||||
* @var ConfigModel
|
||||
*/
|
||||
protected $config;
|
||||
|
||||
/**
|
||||
* Package configuration route (published)
|
||||
*/
|
||||
const DEFAULT_PACKAGE_CONFIG = 'laravel-gettext';
|
||||
|
||||
/**
|
||||
* @param array $config
|
||||
* @throws RequiredConfigurationKeyException
|
||||
*/
|
||||
public function __construct($config = null)
|
||||
{
|
||||
if ($config) {
|
||||
$this->config = $this->generateFromArray($config);
|
||||
} else {
|
||||
// In Laravel 5.3 we need empty config model
|
||||
$this->config = new ConfigModel;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get new instance of the ConfigManager
|
||||
*
|
||||
* @param null $config
|
||||
* @return static
|
||||
* @throws RequiredConfigurationFileException
|
||||
*/
|
||||
public static function create($config = null)
|
||||
{
|
||||
if (is_null($config)) {
|
||||
// Default package configuration file (published)
|
||||
$config = Config::get(static::DEFAULT_PACKAGE_CONFIG);
|
||||
}
|
||||
|
||||
return new static($config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the config model
|
||||
*
|
||||
* @return ConfigModel
|
||||
*/
|
||||
public function get()
|
||||
{
|
||||
return $this->config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a configuration container and checks the required fields
|
||||
*
|
||||
* @param array $config
|
||||
* @return ConfigModel
|
||||
* @throws RequiredConfigurationKeyException
|
||||
*/
|
||||
protected function generateFromArray(array $config)
|
||||
{
|
||||
$requiredKeys = [
|
||||
'locale',
|
||||
'fallback-locale',
|
||||
'encoding'
|
||||
];
|
||||
|
||||
foreach ($requiredKeys as $key) {
|
||||
if (!array_key_exists($key, $config)) {
|
||||
throw new RequiredConfigurationKeyException(
|
||||
sprintf('Unconfigured required value: %s', $key)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
$container = new ConfigModel();
|
||||
|
||||
$id = isset($config['session-identifier']) ? $config['session-identifier'] : 'laravel-gettext-locale';
|
||||
|
||||
$adapter = isset($config['adapter']) ? $config['adapter'] : \App\LaravelGettext\Adapters\LaravelAdapter::class;
|
||||
|
||||
$storage = isset($config['storage']) ? $config['storage'] : SessionStorage::class;
|
||||
|
||||
$container->setLocale($config['locale'])
|
||||
->setSessionIdentifier($id)
|
||||
->setEncoding($config['encoding'])
|
||||
->setCategories(Arr::get('categories', $config, ['LC_ALL']))
|
||||
->setFallbackLocale($config['fallback-locale'])
|
||||
->setSupportedLocales($config['supported-locales'])
|
||||
->setDomain($config['domain'])
|
||||
->setTranslationsPath($config['translations-path'])
|
||||
->setProject($config['project'])
|
||||
->setTranslator($config['translator'])
|
||||
->setSourcePaths($config['source-paths'])
|
||||
->setSyncLaravel($config['sync-laravel'])
|
||||
->setAdapter($adapter)
|
||||
->setStorage($storage);
|
||||
|
||||
if (array_key_exists('relative-path', $config)) {
|
||||
$container->setRelativePath($config['relative-path']);
|
||||
}
|
||||
|
||||
if (array_key_exists("custom-locale", $config)) {
|
||||
$container->setCustomLocale($config['custom-locale']);
|
||||
}
|
||||
|
||||
if (array_key_exists("keywords-list", $config)) {
|
||||
$container->setKeywordsList($config['keywords-list']);
|
||||
}
|
||||
|
||||
if (array_key_exists("handler", $config)) {
|
||||
$container->setHandler($config['handler']);
|
||||
}
|
||||
|
||||
return $container;
|
||||
}
|
||||
}
|
589
app/LaravelGettext/Config/Models/Config.php
Normal file
589
app/LaravelGettext/Config/Models/Config.php
Normal file
@@ -0,0 +1,589 @@
|
||||
<?php
|
||||
|
||||
namespace App\LaravelGettext\Config\Models;
|
||||
|
||||
class Config
|
||||
{
|
||||
/**
|
||||
* Session identifier
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $sessionIdentifier;
|
||||
|
||||
/**
|
||||
* Charset encoding for files
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $encoding;
|
||||
|
||||
/**
|
||||
* Full ISO Locale (en_EN)
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $locale;
|
||||
|
||||
/**
|
||||
* Locale categories
|
||||
*
|
||||
* @type array
|
||||
*/
|
||||
protected $categories;
|
||||
|
||||
/**
|
||||
* Fallback locale
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $fallbackLocale;
|
||||
|
||||
/**
|
||||
* Supported locales
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $supportedLocales;
|
||||
|
||||
/**
|
||||
* Gettext domain
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $domain;
|
||||
|
||||
/**
|
||||
* Path to translation files
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $translationsPath;
|
||||
|
||||
/**
|
||||
* Project identifier
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $project;
|
||||
|
||||
/**
|
||||
* Translator contact data
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $translator;
|
||||
|
||||
/**
|
||||
* Source paths
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $sourcePaths;
|
||||
|
||||
/**
|
||||
* Sync with laravel locale
|
||||
*
|
||||
* @type Boolean
|
||||
*/
|
||||
protected $syncLaravel;
|
||||
|
||||
/**
|
||||
* The adapter class used to sync with laravel locale
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $adapter;
|
||||
|
||||
|
||||
/**
|
||||
* The storage class used store the current locale information
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $storage;
|
||||
|
||||
/**
|
||||
* Custom locale name
|
||||
* Used when needed locales are unavalilable
|
||||
*
|
||||
* @type Boolean
|
||||
*/
|
||||
protected $customLocale;
|
||||
|
||||
/**
|
||||
* Default relative path
|
||||
*
|
||||
* @type string
|
||||
*/
|
||||
protected $relativePath;
|
||||
|
||||
/**
|
||||
* Poedit keywords list
|
||||
*
|
||||
* @type array
|
||||
*/
|
||||
protected $keywordsList;
|
||||
|
||||
/**
|
||||
* Core translation handler
|
||||
*/
|
||||
protected $handler;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->encoding = 'UTF-8';
|
||||
$this->supportedLocales = [];
|
||||
$this->sourcePaths = [];
|
||||
$this->customLocale = false;
|
||||
$this->relativePath = "../../../../../app";
|
||||
}
|
||||
|
||||
public function getRelativePath()
|
||||
{
|
||||
return $this->relativePath;
|
||||
}
|
||||
|
||||
public function setRelativePath($path)
|
||||
{
|
||||
$this->relativePath = $path;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getSessionIdentifier()
|
||||
{
|
||||
return $this->sessionIdentifier;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sessionIdentifier
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setSessionIdentifier($sessionIdentifier)
|
||||
{
|
||||
$this->sessionIdentifier = $sessionIdentifier;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getEncoding()
|
||||
{
|
||||
return $this->encoding;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $encoding
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setEncoding($encoding)
|
||||
{
|
||||
$this->encoding = $encoding;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getLocale()
|
||||
{
|
||||
return $this->locale;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $locale
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setLocale($locale)
|
||||
{
|
||||
$this->locale = $locale;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets categories
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getCategories()
|
||||
{
|
||||
return $this->categories;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets categories
|
||||
*
|
||||
* @param array $categories
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
public function setCategories($categories)
|
||||
{
|
||||
$this->categories = $categories;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getFallbackLocale()
|
||||
{
|
||||
return $this->fallbackLocale;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $fallbackLocale
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setFallbackLocale($fallbackLocale)
|
||||
{
|
||||
$this->fallbackLocale = $fallbackLocale;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getSupportedLocales()
|
||||
{
|
||||
return $this->supportedLocales;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $supportedLocales
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setSupportedLocales($supportedLocales)
|
||||
{
|
||||
$this->supportedLocales = $supportedLocales;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getDomain()
|
||||
{
|
||||
return $this->domain;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $domain
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setDomain($domain)
|
||||
{
|
||||
$this->domain = $domain;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getTranslationsPath()
|
||||
{
|
||||
return $this->translationsPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $translationsPath
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setTranslationsPath($translationsPath)
|
||||
{
|
||||
$this->translationsPath = $translationsPath;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getProject()
|
||||
{
|
||||
return $this->project;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $project
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setProject($project)
|
||||
{
|
||||
$this->project = $project;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getTranslator()
|
||||
{
|
||||
return $this->translator;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $translator
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setTranslator($translator)
|
||||
{
|
||||
$this->translator = $translator;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getSourcePaths()
|
||||
{
|
||||
return $this->sourcePaths;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $sourcePaths
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setSourcePaths($sourcePaths)
|
||||
{
|
||||
$this->sourcePaths = $sourcePaths;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return boolean
|
||||
*/
|
||||
public function isSyncLaravel()
|
||||
{
|
||||
return $this->syncLaravel;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the Sync with laravel locale.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getSyncLaravel()
|
||||
{
|
||||
return $this->syncLaravel;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param boolean $syncLaravel
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setSyncLaravel($syncLaravel)
|
||||
{
|
||||
$this->syncLaravel = $syncLaravel;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the adapter class.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getAdapter()
|
||||
{
|
||||
return $this->adapter;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $adapter
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setAdapter($adapter)
|
||||
{
|
||||
$this->adapter = $adapter;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for storage
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getStorage()
|
||||
{
|
||||
return $this->storage;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $storage
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setStorage($storage)
|
||||
{
|
||||
$this->storage = $storage;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Return an array with all domain names
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getAllDomains()
|
||||
{
|
||||
$domains = [$this->domain]; // add the default domain
|
||||
|
||||
foreach ($this->sourcePaths as $domain => $paths) {
|
||||
if (is_array($paths)) {
|
||||
array_push($domains, $domain);
|
||||
}
|
||||
}
|
||||
|
||||
return array_unique($domains);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all routes for a single domain
|
||||
*
|
||||
* @param $domain
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getSourcesFromDomain($domain)
|
||||
{
|
||||
// grab any paths wrapped in $domain
|
||||
$explicitPaths = array_key_exists($domain, $this->sourcePaths)
|
||||
? $this->sourcePaths[$domain]
|
||||
: [];
|
||||
|
||||
// if we're not including the default domain, return what we have so far
|
||||
if ($this->domain != $domain) {
|
||||
return $explicitPaths;
|
||||
}
|
||||
|
||||
// otherwise, grab all the default domain paths
|
||||
// and merge them with paths wrapped in $domain
|
||||
return array_reduce(
|
||||
$this->sourcePaths,
|
||||
function ($carry, $path) {
|
||||
if (!is_array($path)) {
|
||||
$carry[] = $path;
|
||||
}
|
||||
|
||||
return $carry;
|
||||
},
|
||||
$explicitPaths
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets C locale setting.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function getCustomLocale()
|
||||
{
|
||||
return $this->customLocale;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets if will use C locale structure.
|
||||
*
|
||||
* @param mixed $sourcePaths the source paths
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
public function setCustomLocale($customLocale)
|
||||
{
|
||||
$this->customLocale = $customLocale;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the Poedit keywords list.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getKeywordsList()
|
||||
{
|
||||
return !empty($this->keywordsList)
|
||||
? $this->keywordsList
|
||||
: ['_'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the Poedit keywords list.
|
||||
*
|
||||
* @param mixed $keywordsList the keywords list
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
public function setKeywordsList($keywordsList)
|
||||
{
|
||||
$this->keywordsList = $keywordsList;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the handler type. Also check for valid handler name
|
||||
*
|
||||
* @param $handler
|
||||
*
|
||||
* @return $this
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function setHandler($handler)
|
||||
{
|
||||
if (!in_array($handler, [
|
||||
'symfony',
|
||||
'gettext',
|
||||
])
|
||||
) {
|
||||
throw new \Exception("Handler '$handler' is not supported'");
|
||||
};
|
||||
|
||||
$this->handler = $handler;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the handler name
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getHandler()
|
||||
{
|
||||
return !empty($this->handler)
|
||||
? $this->handler
|
||||
: 'symfony';
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user