DumpCommand::getJqueryUrls()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 3
nop 0
1
<?php
2
3
namespace Evheniy\HTML5CacheBundle\Command;
4
5
use Evheniy\HTML5CacheBundle\Exception\PathException;
6
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use Symfony\Component\Filesystem\Filesystem;
10
use Symfony\Component\Finder\Finder;
11
12
/**
13
 * Class DumpCommand
14
 *
15
 * @package Evheniy\HTML5CacheBundle\Command
16
 */
17
class DumpCommand extends ContainerAwareCommand
18
{
19
    /**
20
     * @var array
21
     */
22
    protected $_types  = array('jpg', 'jpeg', 'gif', 'png', 'bmp', 'ico', 'flv', 'swf', 'txt', 'css', 'js', 'eot', 'woff', 'woff2','ttf', 'svg');
23
    /**
24
     * @var string
25
     */
26
    protected $webDirectory;
27
    /**
28
     * @var array
29
     */
30
    protected $html5Cache;
31
    /**
32
     * @var Filesystem
33
     */
34
    protected $filesystem;
35
    /**
36
     * @var Finder
37
     */
38
    protected $finder;
39
40
    /**
41
     *
42
     */
43
    protected function configure()
44
    {
45
        $this
46
            ->setName('manifest:dump')
47
            ->setDescription('Dumps Cache Manifest file for using HTML5 Application Cache')
48
            ->setHelp("\nThe <info>%command.name%</info> command dumpsCache Manifest file for using HTML5 Application Cache.\n\n<info>%command.full_name%</info>\n");
49
    }
50
51
    /**
52
     * @param InputInterface  $input
53
     * @param OutputInterface $output
54
     *
55
     * @return int|null|void
56
     */
57
    protected function execute(InputInterface $input, OutputInterface $output)
58
    {
59
        $input;
60
        $output->writeln('<comment>Start dumping cache.manifest...</comment>');
61
        $this->webDirectory = $this->getContainer()->get('kernel')->getRootdir() . '/../web';
62
        $this->filesystem = new Filesystem();
63
        $this->finder = new Finder();
64
        $this->setHtml5Cache($this->getPaths());
65
        $this->dump();
66
        $output->writeln('<info>Done</info>');
67
    }
68
69
    /**
70
     * @param array $paths
71
     */
72
    protected function setHtml5Cache(array $paths = array())
73
    {
74
        $this->html5Cache = array_merge(
75
            $this->getContainer()->getParameter('html5_cache'),
76
            array('paths' => $paths)
77
        );
78
        $this->html5Cache['custom_paths'] = array_merge(
79
            $this->html5Cache['custom_paths'],
80
            $this->getJqueryUrls(),
81
            $this->getTwitterBootstrapUrls(),
82
            $this->getMaterializeUrls()
83
        );
84
    }
85
86
    /**
87
     * @return array
88
     */
89
    protected function getPaths()
90
    {
91
        $paths = array();
92
        foreach ($this->finder->files()->in($this->webDirectory) as $file) {
93
            if ($file->isFile() && in_array($file->getExtension(), $this->_types)) {
94
                $paths[] = $this->getPath($file->getRealPath());
95
            }
96
        }
97
98
        return $paths;
99
    }
100
101
    /**
102
     *
103
     */
104
    protected function dump()
105
    {
106
        $this->filesystem->dumpFile(
107
            $this->webDirectory . '/cache.manifest',
108
            $this->render()
109
        );
110
    }
111
112
    /**
113
     * @return string
114
     */
115
    protected function render()
116
    {
117
        return $this->getContainer()->get('twig')->render('HTML5CacheBundle::cache.html.twig', $this->html5Cache);
118
    }
119
120
    /**
121
     * @param string $filePath
122
     *
123
     * @return string
124
     * @throws PathException
125
     */
126
    protected function getPath($filePath)
127
    {
128
        $path = explode('/web/', $filePath);
129
        if (empty($path) || empty($path[1])) {
130
            throw new PathException('You must search files in web directory');
131
        }
132
133
        return $path[1];
134
    }
135
136
    /**
137
     * @return array
138
     */
139
    protected function getJqueryUrls()
140
    {
141
        $url = array();
142
        if ($this->getContainer()->hasParameter('jquery')) {
143
            $jquery = $this->getContainer()->getParameter('jquery');
144
            if (!empty($jquery) && !empty($jquery['version'])) {
145
                $url[] = "https://ajax.googleapis.com/ajax/libs/jquery/{$jquery['version']}/jquery.min.js";
146
            }
147
        }
148
149
        return $url;
150
    }
151
152
    /**
153
     * @return array
154
     */
155
    protected function getTwitterBootstrapUrls()
156
    {
157
        $url = array();
158
        if ($this->getContainer()->hasParameter('twitter_bootstrap')) {
159
            $twitterBootstrap = $this->getContainer()->getParameter('twitter_bootstrap');
160
            if (!empty($twitterBootstrap) && !empty($twitterBootstrap['version'])) {
161
                $url[] = "https://maxcdn.bootstrapcdn.com/bootstrap/{$twitterBootstrap['version']}/css/bootstrap.min.css";
162
                $url[] = "https://maxcdn.bootstrapcdn.com/bootstrap/{$twitterBootstrap['version']}/css/bootstrap-theme.min.css";
163
                $url[] = "https://maxcdn.bootstrapcdn.com/bootstrap/{$twitterBootstrap['version']}/js/bootstrap.min.js";
164
            }
165
        }
166
167
        return $url;
168
    }
169
170
    /**
171
     * @return array
172
     */
173
    protected function getMaterializeUrls()
174
    {
175
        $url = array();
176
        if ($this->getContainer()->hasParameter('materialize')) {
177
            $materialize = $this->getContainer()->getParameter('materialize');
178
            if (!empty($materialize) && !empty($materialize['version'])) {
179
                $url[] = "https://cdnjs.cloudflare.com/ajax/libs/materialize/{$materialize['version']}/css/materialize.min.css";
180
                $url[] = "https://cdnjs.cloudflare.com/ajax/libs/materialize/{$materialize['version']}/js/materialize.min.js";
181
            }
182
        }
183
184
        return $url;
185
    }
186
}