File size: 2,615 Bytes
d2897cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php

namespace Mautic\CoreBundle\Helper\Language;

use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;

class Installer
{
    private ?string $sourceDirectory = null;

    private ?string $installDirectory = null;

    private Filesystem $filesystem;

    /**
     * @param string $translationsDirectory
     */
    public function __construct(
        private $translationsDirectory
    ) {
        $this->filesystem            = new Filesystem();
    }

    /**
     * @param string $sourceDirectory
     * @param string $languageCode
     *
     * @return $this
     */
    public function install($sourceDirectory, $languageCode)
    {
        $this->sourceDirectory  = $sourceDirectory.'/'.$languageCode;
        $this->installDirectory = $this->translationsDirectory.'/'.$languageCode;

        $this->createLanguageDirectory();
        $this->copyConfig();
        $this->copyBundles();

        return $this;
    }

    public function cleanup(): void
    {
        if (!$this->sourceDirectory) {
            return;
        }

        // Delete the temp directory
        $this->filesystem->remove($this->sourceDirectory);

        $this->sourceDirectory  = null;
        $this->installDirectory = null;
    }

    private function createLanguageDirectory(): void
    {
        if (is_dir($this->installDirectory)) {
            return;
        }

        $this->filesystem->mkdir($this->installDirectory, 0755);
    }

    private function copyConfig(): void
    {
        $this->filesystem->copy($this->sourceDirectory.'/config.json', $this->installDirectory.'/config.json', true);
    }

    private function copyBundles(): void
    {
        $bundles = new Finder();
        $bundles->directories()->name('*Bundle')->in($this->sourceDirectory);

        /** @var \SplFileInfo $bundle */
        foreach ($bundles as $bundle) {
            $this->copyBundle($bundle);
        }
    }

    private function copyBundle(\SplFileInfo $bundle): void
    {
        $name            = $bundle->getFilename();
        $targetDirectory = $this->installDirectory.'/'.$name;
        $sourceDirectory = $this->sourceDirectory.'/'.$name;

        // Create bundle directory
        if (!is_dir($targetDirectory)) {
            $this->filesystem->mkdir($targetDirectory, 0755);
        }

        // Copy INI files
        $iniFinder = new Finder();
        $iniFinder->files()->name('*.ini')->in($sourceDirectory);
        foreach ($iniFinder as $iniFile) {
            $this->filesystem->copy($iniFile->getPathname(), $targetDirectory.'/'.$iniFile->getFilename());
        }
    }
}