• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Cecilapp / Cecil / 8841149368

26 Apr 2024 12:22AM UTC coverage: 83.543%. First build
8841149368

Pull #1955

github

web-flow
Merge 032b5869c into 1a6cc4596
Pull Request #1955: feat: data file localization

9 of 10 new or added lines in 3 files covered. (90.0%)

2924 of 3500 relevant lines covered (83.54%)

0.84 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.91
/src/Step/Data/Load.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of Cecil.
7
 *
8
 * Copyright (c) Arnaud Ligny <arnaud@ligny.fr>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13

14
namespace Cecil\Step\Data;
15

16
use Cecil\Collection\Page\PrefixSuffix;
17
use Cecil\Step\AbstractStep;
18
use Cecil\Util;
19
use Symfony\Component\Finder\Finder;
20
use Symfony\Component\Serializer\Encoder\CsvEncoder;
21
use Symfony\Component\Serializer\Encoder\JsonEncoder;
22
use Symfony\Component\Serializer\Encoder\XmlEncoder;
23
use Symfony\Component\Serializer\Encoder\YamlEncoder;
24
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
25
use Symfony\Component\Serializer\Serializer;
26

27
/**
28
 * Loads data files.
29
 */
30
class Load extends AbstractStep
31
{
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function getName(): string
36
    {
37
        return 'Loading data';
1✔
38
    }
39

40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function init(array $options): void
44
    {
45
        if (is_dir($this->config->getDataPath()) && (bool) $this->config->get('data.load')) {
1✔
46
            $this->canProcess = true;
1✔
47
        }
48
    }
49

50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function process(): void
54
    {
55
        $files = Finder::create()
1✔
56
            ->files()
1✔
57
            ->in($this->config->getDataPath())
1✔
58
            ->name('/\.(' . implode('|', (array) $this->config->get('data.ext')) . ')$/')
1✔
59
            ->sortByName(true);
1✔
60
        $total = \count($files);
1✔
61

62
        if ($total < 1) {
1✔
63
            $message = 'No files';
×
64
            $this->builder->getLogger()->info($message);
×
65

66
            return;
×
67
        }
68

69
        $serializerYaml = new Serializer([new ObjectNormalizer()], [new YamlEncoder()]);
1✔
70
        $serializerJson = new Serializer([new ObjectNormalizer()], [new JsonEncoder()]);
1✔
71
        $serializerCsv = new Serializer([new ObjectNormalizer()], [new CsvEncoder()]);
1✔
72
        $serializerXml = new Serializer([new ObjectNormalizer()], [new XmlEncoder()]);
1✔
73
        $count = 0;
1✔
74

75
        /** @var \Symfony\Component\Finder\SplFileInfo $file */
76
        foreach ($files as $file) {
1✔
77
            $count++;
1✔
78
            set_error_handler(
1✔
79
                function ($severity, $message, $file, $line) {
1✔
80
                    throw new \ErrorException($message, 0, $severity, $file, $line, null);
×
81
                }
1✔
82
            );
1✔
83
            $data = $file->getContents();
1✔
84
            restore_error_handler();
1✔
85

86
            switch ($file->getExtension()) {
1✔
87
                case 'yml':
1✔
88
                case 'yaml':
1✔
89
                    $dataAsArray = $serializerYaml->decode($data, 'yaml');
1✔
90
                    break;
1✔
91
                case 'json':
1✔
92
                    $dataAsArray = $serializerJson->decode($data, 'json');
1✔
93
                    break;
1✔
94
                case 'csv':
1✔
95
                    $dataAsArray = $serializerCsv->decode($data, 'csv');
1✔
96
                    break;
1✔
97
                case 'xml':
1✔
98
                    $dataAsArray = $serializerXml->decode($data, 'xml');
1✔
99
                    break;
1✔
100
                default:
101
                    return;
×
102
            }
103

104
            $lang = $this->config->getLanguageDefault();
1✔
105
            if (PrefixSuffix::hasSuffix($file->getBasename('.' . $file->getExtension()))) {
1✔
NEW
106
                $lang = PrefixSuffix::getSuffix($file->getBasename('.' . $file->getExtension()));
×
107
            }
108
            $basename = $file->getBasename('.' . $file->getExtension());
1✔
109
            $subpath = \Cecil\Util\File::getFS()->makePathRelative(
1✔
110
                $file->getPath(),
1✔
111
                $this->config->getDataPath()
1✔
112
            );
1✔
113
            $subpath = trim($subpath, './');
1✔
114
            $array = [];
1✔
115
            $path = !empty($subpath) ? Util::joinFile($subpath, $basename) : $basename;
1✔
116
            $localizedPath = Util::joinFile($lang, PrefixSuffix::sub($path));
1✔
117
            $this->pathToArray($array, $localizedPath, $dataAsArray);
1✔
118

119
            $dataAsArray = array_merge_recursive(
1✔
120
                $this->builder->getData(),
1✔
121
                $array
1✔
122
            );
1✔
123
            $this->builder->setData($dataAsArray);
1✔
124

125
            $message = sprintf('File "%s.%s" loaded', Util::joinFile($path), $file->getExtension());
1✔
126
            //$message = sprintf('File "%s" loaded', $file->getBasename());
127
            $this->builder->getLogger()->info($message, ['progress' => [$count, $total]]);
1✔
128
        }
129
    }
130

131
    /**
132
     * Puts a path/value couple into an array.
133
     *
134
     * @param array  $arr       Target array
135
     * @param string $path      Source path
136
     * @param array  $value     Source values
137
     * @param string $separator Path separator (ie: '/')
138
     */
139
    private function pathToArray(array &$arr, string $path, array $value, string $separator = DIRECTORY_SEPARATOR): void
140
    {
141
        $keys = explode($separator, $path);
1✔
142
        foreach ($keys as $key) {
1✔
143
            $arr = &$arr[$key];
1✔
144
        }
145
        $arr = $value;
1✔
146
    }
147
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc