Conditions | 11 |
Paths | 96 |
Total Lines | 80 |
Code Lines | 49 |
Lines | 0 |
Ratio | 0 % |
Changes | 4 | ||
Bugs | 2 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
42 | public function __construct(Builder $builder, $templatesPath) |
||
43 | { |
||
44 | // load layouts |
||
45 | $loader = new \Twig\Loader\FilesystemLoader($templatesPath); |
||
46 | // default options |
||
47 | $loaderOptions = [ |
||
48 | 'debug' => $builder->isDebug(), |
||
49 | 'strict_variables' => true, |
||
50 | 'autoescape' => false, |
||
51 | 'auto_reload' => true, |
||
52 | 'cache' => false, |
||
53 | ]; |
||
54 | // use Twig cache? |
||
55 | if ($builder->getConfig()->get('cache.templates.enabled')) { |
||
56 | $templatesCachePath = \Cecil\Util::joinFile( |
||
57 | $builder->getConfig()->getCachePath(), |
||
58 | (string) $builder->getConfig()->get('cache.templates.dir') |
||
59 | ); |
||
60 | $loaderOptions = array_replace($loaderOptions, ['cache' => $templatesCachePath]); |
||
61 | } |
||
62 | // create the Twig instance |
||
63 | $this->twig = new \Twig\Environment($loader, $loaderOptions); |
||
64 | // set date format |
||
65 | $this->twig->getExtension(\Twig\Extension\CoreExtension::class) |
||
66 | ->setDateFormat($builder->getConfig()->get('date.format')); |
||
67 | // set timezone |
||
68 | if ($builder->getConfig()->has('date.timezone')) { |
||
69 | $this->twig->getExtension(\Twig\Extension\CoreExtension::class) |
||
70 | ->setTimezone($builder->getConfig()->get('date.timezone')); |
||
71 | } |
||
72 | // adds extensions |
||
73 | $this->twig->addExtension(new TwigExtension($builder)); |
||
74 | $this->twig->addExtension(new \Twig\Extension\StringLoaderExtension()); |
||
75 | // i18n |
||
76 | $locale = $builder->getConfig()->getLanguageProperty('locale'); |
||
77 | $this->translator = new Translator($locale, new MessageFormatter(new IdentityTranslator())); |
||
|
|||
78 | $this->translator->addLoader('mo', new MoFileLoader()); |
||
79 | if (count($builder->getConfig()->getLanguages()) > 1) { |
||
80 | foreach ($builder->getConfig()->getLanguages() as $lang) { |
||
81 | $translationFile = realpath(Util::joinFile($builder->getConfig()->getSourceDir(), \sprintf('translations/messages.%s.mo', $lang['locale']))); |
||
82 | if (Util\File::getFS()->exists($translationFile)) { |
||
83 | $this->translator->addResource('mo', $translationFile, $lang['locale']); |
||
84 | } |
||
85 | } |
||
86 | } else { |
||
87 | $translationFile = realpath(Util::joinFile($builder->getConfig()->getSourceDir(), \sprintf('translations/messages.%s.mo', $locale))); |
||
88 | if (Util\File::getFS()->exists($translationFile)) { |
||
89 | $this->translator->addResource('mo', $translationFile, $locale); |
||
90 | } |
||
91 | } |
||
92 | $this->twig->addExtension(new TranslationExtension($this->translator)); |
||
93 | // filters fallback |
||
94 | $this->twig->registerUndefinedFilterCallback(function ($name) use ($builder) { |
||
95 | switch ($name) { |
||
96 | case 'localizeddate': |
||
97 | return new \Twig\TwigFilter($name, function (\DateTime $value = null) use ($builder) { |
||
98 | return date((string) $builder->getConfig()->get('date.format'), $value->getTimestamp()); |
||
99 | }); |
||
100 | } |
||
101 | |||
102 | return false; |
||
103 | }); |
||
104 | // debug |
||
105 | if ($builder->isDebug()) { |
||
106 | // dump() |
||
107 | $this->twig->addExtension(new \Twig\Extension\DebugExtension()); |
||
108 | // profiler |
||
109 | $this->profile = new \Twig\Profiler\Profile(); |
||
110 | $this->twig->addExtension(new \Twig\Extension\ProfilerExtension($this->profile)); |
||
111 | } |
||
112 | /** |
||
113 | * Backward compatibility. |
||
114 | */ |
||
115 | if (extension_loaded('intl')) { |
||
116 | $this->twig->addExtension(new \Twig\Extensions\IntlExtension()); |
||
117 | $builder->getLogger()->debug('Intl extension is loaded'); |
||
118 | } |
||
119 | if (extension_loaded('gettext')) { |
||
120 | $this->twig->addExtension(new \Twig\Extensions\I18nExtension()); |
||
121 | $builder->getLogger()->debug('Gettext extension is loaded'); |
||
122 | } |
||
149 |