Passed
Push — assets ( cceb7d...725893 )
by Arnaud
11:04 queued 08:30
created

AbstractPostProcess::removesHashFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 5
rs 10
1
<?php
2
/**
3
 * This file is part of the Cecil/Cecil package.
4
 *
5
 * Copyright (c) Arnaud Ligny <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Cecil\Step;
12
13
use Cecil\Cache\Cache;
14
use Cecil\Exception\Exception;
15
use Symfony\Component\Finder\Finder;
16
17
/**
18
 * Post Processing.
19
 */
20
abstract class AbstractPostProcess extends AbstractStep
21
{
22
    /** @var string File type (ie: 'css') */
23
    protected $type = 'type';
24
    /** @var mixed File processor */
25
    protected $processor;
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function init($options)
31
    {
32
        if ($options['dry-run']) {
33
            $this->process = false;
34
35
            return;
36
        }
37
        if (false === $this->builder->getConfig()->get(sprintf('postprocess.%s.enabled', $this->type))) {
38
            $this->process = false;
39
40
            return;
41
        }
42
        if (true === $this->builder->getConfig()->get('postprocess.enabled')) {
43
            $this->process = true;
44
        }
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function process()
51
    {
52
        $this->setProcessor();
53
54
        call_user_func_array(
55
            $this->builder->getMessageCb(),
56
            ['POSTPROCESS', sprintf('Post-processing %s', $this->type)]
57
        );
58
59
        $extensions = $this->builder->getConfig()->get(sprintf('postprocess.%s.ext', $this->type));
60
        if (empty($extensions)) {
61
            throw new Exception(sprintf('The config key "postprocess.%s.ext" is empty', $this->type));
62
        }
63
64
        $files = Finder::create()
65
            ->files()
66
            ->in($this->builder->getConfig()->getOutputPath())
67
            ->name('/\.('.implode('|', $extensions).')$/')
68
            ->notName('/\.min\.('.implode('|', $extensions).')$/')
69
            ->sortByName(true);
70
        $max = count($files);
71
72
        if ($max <= 0) {
73
            $message = 'No files';
74
            call_user_func_array($this->builder->getMessageCb(), ['POSTPROCESS_PROGRESS', $message]);
75
76
            return;
77
        }
78
79
        $count = 0;
80
        $postprocessed = 0;
81
82
        /** @var \Symfony\Component\Finder\SplFileInfo $file */
83
        foreach ($files as $file) {
84
            $count++;
85
            $sizeBefore = $file->getSize();
86
            $message = $file->getRelativePathname();
87
88
            $cache = new Cache($this->builder, 'postprocess', $this->config->getOutputPath());
89
            if (!$cache->isExists($file->getPathname())) {
90
                $hash = $cache->getHash($file->getPathname());
91
                $this->processFile($file);
92
                $postprocessed++;
93
                $sizeAfter = $file->getSize();
94
                if ($sizeAfter < $sizeBefore) {
95
                    $message = sprintf(
96
                        '%s (%s Ko -> %s Ko)',
97
                        $file->getRelativePathname(),
98
                        ceil($sizeBefore / 1000),
99
                        ceil($sizeAfter / 1000)
100
                    );
101
                }
102
                $cache->save($file->getPathname(), $hash);
103
            }
104
            call_user_func_array($this->builder->getMessageCb(), ['POSTPROCESS_PROGRESS', $message, $count, $max]);
105
        }
106
        if ($postprocessed < 1) {
107
            $message = '> Nothing to do';
108
            call_user_func_array($this->builder->getMessageCb(), ['POSTPROCESS_PROGRESS', $message]);
109
        }
110
    }
111
112
    /**
113
     * Set file processor object.
114
     *
115
     * @return void
116
     */
117
    abstract public function setProcessor();
118
119
    /**
120
     * Process a file.
121
     *
122
     * @param \Symfony\Component\Finder\SplFileInfo $file
123
     *
124
     * @return void
125
     */
126
    abstract public function processFile(\Symfony\Component\Finder\SplFileInfo $file);
127
}
128