1<?php
2
3/**
4 * 2007-2016 PrestaShop.
5 *
6 * NOTICE OF LICENSE
7 *
8 * This source file is subject to the Open Software License (OSL 3.0)
9 * that is bundled with this package in the file LICENSE.txt.
10 * It is also available through the world-wide-web at this URL:
11 * http://opensource.org/licenses/osl-3.0.php
12 * If you did not receive a copy of the license and are unable to
13 * obtain it through the world-wide-web, please send an email
14 * to license@prestashop.com so we can send you a copy immediately.
15 *
16 * DISCLAIMER
17 *
18 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
19 * versions in the future. If you wish to customize PrestaShop for your
20 * needs please refer to http://www.prestashop.com for more information.
21 *
22 * @author    PrestaShop SA <contact@prestashop.com>
23 * @copyright 2007-2015 PrestaShop SA
24 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25 * International Registered Trademark & Property of PrestaShop SA
26 */
27
28namespace PrestaShop\TranslationToolsBundle;
29
30use Symfony\Component\OptionsResolver\OptionsResolver;
31use Symfony\Component\Yaml\Yaml;
32
33class Configuration
34{
35    /**
36     * @var array
37     */
38    private static $paths = [];
39
40    /**
41     * @var array
42     */
43    private static $excludeFiles = [];
44
45    /**
46     * @var string
47     */
48    private static $projectDirectory = '';
49
50    /**
51     * @var string
52     */
53    private static $cacheDir;
54
55    /**
56     * @param array $arr
57     */
58    public static function fromArray(array $arr)
59    {
60        $optionsResolver = new OptionsResolver();
61        $options = $optionsResolver->setRequired([
62            'paths',
63            'exclude_files',
64        ])
65            ->setDefaults([
66                'cache_dir' => null,
67            ])
68            ->addAllowedTypes('paths', 'array')
69            ->addAllowedTypes('exclude_files', ['array', 'null'])
70            ->addAllowedTypes('cache_dir', ['string', 'null'])
71            ->resolve($arr);
72
73        self::$paths = (array) $options['paths'];
74        self::$excludeFiles = (array) $options['exclude_files'];
75        self::$cacheDir = $options['cache_dir'];
76    }
77
78    /**
79     * @param string $yamlFile
80     */
81    public static function fromYamlFile($yamlFile)
82    {
83        self::$projectDirectory = realpath(dirname($yamlFile));
84        self::fromArray(Yaml::parse(file_get_contents($yamlFile)));
85    }
86
87    /**
88     * @return array
89     */
90    public static function getPaths()
91    {
92        return self::$paths;
93    }
94
95    /**
96     * @return array
97     */
98    public static function getExcludeFiles()
99    {
100        return self::$excludeFiles;
101    }
102
103    /**
104     * @return string
105     */
106    public static function getProjectDirectory()
107    {
108        return self::$projectDirectory;
109    }
110
111    /**
112     * @param string $path
113     * @param string|bool $rootDir
114     *
115     * @return string
116     */
117    public static function getRelativePath($path, $rootDir = false)
118    {
119        $realpath = realpath($path);
120        $path = empty($realpath) ? $path : $realpath;
121
122        if (!empty($rootDir)) {
123            $rootDir = rtrim($rootDir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
124        } else {
125            $rootDir = '';
126        }
127
128        return str_replace($rootDir, '', $path);
129    }
130
131    /**
132     * @return string
133     */
134    public static function getCacheDir()
135    {
136        return empty(self::$cacheDir) ? sys_get_temp_dir().DIRECTORY_SEPARATOR : self::$cacheDir;
137    }
138}
139