Completed
Push — master ( 770316...74fc07 )
by Jeroen
09:08 queued 02:44
created

Kunstmaan/AdminBundle/Composer/ScriptHandler.php (1 issue)

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
namespace Kunstmaan\AdminBundle\Composer;
4
5
use Composer\Script\Event;
6
use Symfony\Component\Filesystem\Exception\IOException;
7
use Symfony\Component\Filesystem\Filesystem;
8
use Symfony\Component\Yaml\Parser;
9
10
@trigger_error(sprintf('The composer script class "%s" is deprecated in KunstmaanAdminBundle 5.4 and will be removed in KunstmaanAdminBundle 6.0. If you use this script handler, remove it from your composer.json scripts section.', __CLASS__), E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
11
12
/**
13
 * NEXT_MAJOR remove the symfony/filesystem and symfony/yaml as direct dependency of the admin-bundle (if unused in other classes)
14
 */
15
class ScriptHandler
16
{
17
    protected static $options = array(
18
        'symfony-app-dir' => 'app',
19
        'multi-language-option' => 'multilanguage',
20
    );
21
22
    /**
23
     * @param Event $event
24
     */
25
    public static function checkMultiLangConfigs(Event $event)
26
    {
27
        $options = static::getOptions($event);
28
        $appDir = getcwd() . '/' . $options['symfony-app-dir'];
29
        $configDir = $appDir . '/config';
30
        $parametersFile = $configDir . '/parameters.yml';
31
        $routingFile = $configDir . '/routing.yml';
32
        $securityFile = $configDir . '/security.yml';
33
        $singleLangRoutingFile = $configDir . '/routing.singlelang.yml';
34
        $singleLangSecurityFile = $configDir . '/security.singlelang.yml';
35
        $multiLangRoutingFile = $configDir . '/routing.multilang.yml';
36
        $multiLangSecurityFile = $configDir . '/security.multilang.yml';
37
38
        if (is_file($parametersFile)) {
39
            $parameters = self::getConfigParameters($parametersFile);
40
41
            if (isset($parameters[$options['multi-language-option']])) {
42
                $multiLanguage = $parameters[$options['multi-language-option']];
43
                if (!$multiLanguage) {
44
                    $fs = new Filesystem();
45
46
                    // move routing
47
                    if (is_file($singleLangRoutingFile) && !is_file($multiLangRoutingFile)) {
48
                        try {
49
                            $fs->rename($routingFile, $multiLangRoutingFile);
50
                            $fs->rename($singleLangRoutingFile, $routingFile);
51
                            $event->getIO()->write(sprintf('Replaced routing config with single language config'));
52
                        } catch (IOException $ioE) {
53
                            $event->getIO()->write(sprintf('Exception while moving routing file to singlelang routing file: <error>%s</error>', $ioE->getMessage()));
54
                        }
55
                    }
56
                    // move security
57
                    if (is_file($singleLangSecurityFile) && !is_file($multiLangSecurityFile)) {
58
                        try {
59
                            $fs->rename($securityFile, $multiLangSecurityFile);
60
                            $fs->rename($singleLangSecurityFile, $securityFile);
61
                            $event->getIO()->write(sprintf('Replaced security config with single language config'));
62
                        } catch (IOException $ioE) {
63
                            $event->getIO()->write(sprintf('Exception while moving routing file to singlelang routing file: <error>%s</error>', $ioE->getMessage()));
64
                        }
65
                    }
66
                }
67
            }
68
        }
69
    }
70
71
    /**
72
     * @param Event $event
73
     *
74
     * @return array
75
     */
76
    protected static function getOptions(Event $event)
77
    {
78
        return array_merge(static::$options, $event->getComposer()->getPackage()->getExtra());
79
    }
80
81
    /**
82
     * @param string $parametersFile
83
     *
84
     * @return array
85
     */
86
    protected static function getConfigParameters($parametersFile)
87
    {
88
        $ymlParser = new Parser();
89
        $config = $ymlParser->parse(file_get_contents($parametersFile));
90
91
        return isset($config['parameters']) ? $config['parameters'] : array();
92
    }
93
}
94