Completed
Pull Request — master (#154)
by Michal
02:01
created

src/CatalogueFactory.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * This file is part of the Kdyby (http://www.kdyby.org)
5
 *
6
 * Copyright (c) 2008 Filip Procházka ([email protected])
7
 *
8
 * For the full copyright and license information, please view the file license.txt that was distributed with this source code.
9
 */
10
11
namespace Kdyby\Translation;
12
13
use Kdyby;
14
15
class CatalogueFactory
16
{
17
18
	use \Kdyby\StrictObjects\Scream;
19
20
	/**
21
	 * @var \Kdyby\Translation\FallbackResolver
22
	 */
23
	private $fallbackResolver;
24
25
	/**
26
	 * @var \Kdyby\Translation\IResourceLoader
27
	 */
28
	private $loader;
29
30
	/**
31
	 * @var array[]
32
	 */
33
	private $resources = [];
34
35
	public function __construct(FallbackResolver $fallbackResolver, IResourceLoader $loader)
36
	{
37
		$this->fallbackResolver = $fallbackResolver;
38
		$this->loader = $loader;
39
	}
40
41
	/**
42
	 * {@inheritdoc}
43
	 */
44
	public function addResource($format, $resource, $locale, $domain = 'messages')
45
	{
46
		$this->resources[$locale][] = [$format, $resource, $domain];
47
	}
48
49
	/**
50
	 * @return array
51
	 */
52
	public function getResources()
53
	{
54
		$list = [];
55
		foreach ($this->resources as $locale => $resources) {
56
			foreach ($resources as $meta) {
57
				$list[] = $meta[1]; // resource file
58
			}
59
		}
60
61
		return $list;
62
	}
63
64
	/**
65
	 * @param \Kdyby\Translation\Translator $translator
66
	 * @param \Symfony\Component\Translation\MessageCatalogueInterface[] $availableCatalogues
67
	 * @param string $locale
68
	 * @throws \Symfony\Component\Translation\Exception\NotFoundResourceException
69
	 * @return \Symfony\Component\Translation\MessageCatalogueInterface
70
	 */
71
	public function createCatalogue(Translator $translator, array &$availableCatalogues, $locale)
72
	{
73
		try {
74
			$this->doLoadCatalogue($availableCatalogues, $locale);
75
76
		} catch (\Symfony\Component\Translation\Exception\NotFoundResourceException $e) {
0 ignored issues
show
The class Symfony\Component\Transl...tFoundResourceException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
77
			if (!$this->fallbackResolver->compute($translator, $locale)) {
78
				throw $e;
79
			}
80
		}
81
82
		$current = $availableCatalogues[$locale];
83
84
		$chain = [$locale => TRUE];
85
		foreach ($this->fallbackResolver->compute($translator, $locale) as $fallback) {
86
			if (!isset($availableCatalogues[$fallback])) {
87
				$this->doLoadCatalogue($availableCatalogues, $fallback);
88
			}
89
90
			$newFallback = $availableCatalogues[$fallback];
91
			$newFallbackFallback = $newFallback->getFallbackCatalogue();
92
			if ($newFallbackFallback !== NULL && isset($chain[$newFallbackFallback->getLocale()])) {
93
				break;
94
			}
95
96
			$current->addFallbackCatalogue($newFallback);
97
			$current = $newFallback;
98
			$chain[$fallback] = TRUE;
99
		}
100
101
		return $availableCatalogues[$locale];
102
	}
103
104
	private function doLoadCatalogue(array &$availableCatalogues, $locale)
105
	{
106
		$availableCatalogues[$locale] = $catalogue = new MessageCatalogue($locale);
107
108
		if (isset($this->resources[$locale])) {
109
			foreach ($this->resources[$locale] as $resource) {
110
				$this->loader->loadResource($resource[0], $resource[1], $resource[2], $catalogue);
111
			}
112
		}
113
114
		return $catalogue;
115
	}
116
117
}
118