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

Cecilapp / Cecil / 7136406461

08 Dec 2023 01:59AM UTC coverage: 83.0% (+0.5%) from 82.534%
7136406461

Pull #1676

github

web-flow
Merge 820a808dd into 814daa587
Pull Request #1676: 8.x dev

186 of 231 new or added lines in 31 files covered. (80.52%)

17 existing lines in 6 files now uncovered.

2861 of 3447 relevant lines covered (83.0%)

0.83 hits per line

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

87.23
/src/Step/Optimize/AbstractOptimize.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\Optimize;
15

16
use Cecil\Assets\Cache;
17
use Cecil\Exception\RuntimeException;
18
use Cecil\Step\AbstractStep;
19
use Cecil\Util;
20
use Symfony\Component\Finder\Finder;
21

22
/**
23
 * Post Processing.
24
 */
25
abstract class AbstractOptimize extends AbstractStep
26
{
27
    /** @var string File type (ie: 'css') */
28
    protected $type;
29

30
    /** @var mixed File processor */
31
    protected $processor;
32

33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function init(array $options): void
37
    {
38
        if ($options['dry-run']) {
1✔
39
            return;
×
40
        }
41
        if (false === $this->config->get(sprintf('optimize.%s.enabled', $this->type))) {
1✔
42
            return;
×
43
        }
44
        if (true === $this->config->get('optimize.enabled')) {
1✔
45
            $this->canProcess = true;
1✔
46
        }
47
    }
48

49
    /**
50
     * {@inheritdoc}
51
     *
52
     * @throws RuntimeException
53
     */
54
    public function process(): void
55
    {
56
        $this->setProcessor();
1✔
57

58
        $extensions = (array) $this->config->get(sprintf('optimize.%s.ext', $this->type));
1✔
59
        if (empty($extensions)) {
1✔
NEW
60
            throw new RuntimeException(sprintf('The config key "optimize.%s.ext" is empty', $this->type));
×
61
        }
62

63
        $files = Finder::create()
1✔
64
            ->files()
1✔
65
            ->in($this->config->getOutputPath())
1✔
66
            ->name('/\.(' . implode('|', $extensions) . ')$/')
1✔
67
            ->notName('/\.min\.(' . implode('|', $extensions) . ')$/')
1✔
68
            ->sortByName(true);
1✔
69
        $max = \count($files);
1✔
70

71
        if ($max <= 0) {
1✔
72
            $this->builder->getLogger()->info('No files');
×
73

74
            return;
×
75
        }
76

77
        $count = 0;
1✔
78
        $optimized = 0;
1✔
79
        $cache = new Cache($this->builder, 'optimized');
1✔
80

81
        /** @var \Symfony\Component\Finder\SplFileInfo $file */
82
        foreach ($files as $file) {
1✔
83
            $count++;
1✔
84
            $sizeBefore = $file->getSize();
1✔
85
            $message = sprintf('File "%s" processed', $this->builder->isDebug() ? $file->getPathname() : $file->getRelativePathname());
1✔
86

87
            $cacheKey = $cache->createKeyFromPath($file->getPathname(), $file->getRelativePathname());
1✔
88
            if (!$cache->has($cacheKey)) {
1✔
89
                $processed = $this->processFile($file);
1✔
90
                $sizeAfter = \strlen($processed);
1✔
91
                if ($sizeAfter < $sizeBefore) {
1✔
92
                    $message = sprintf(
1✔
93
                        'File "%s" optimized (%s Ko -> %s Ko)',
1✔
94
                        $this->builder->isDebug() ? $file->getPathname() : $file->getRelativePathname(),
1✔
95
                        ceil($sizeBefore / 1000),
1✔
96
                        ceil($sizeAfter / 1000)
1✔
97
                    );
1✔
98
                }
99
                $cache->set($cacheKey, $this->encode($processed));
1✔
100
                $optimized++;
1✔
101

102
                $this->builder->getLogger()->info($message, ['progress' => [$count, $max]]);
1✔
103
            }
104
            $processed = $this->decode($cache->get($cacheKey));
1✔
105
            Util\File::getFS()->dumpFile($file->getPathname(), $processed);
1✔
106
        }
107
        if ($optimized == 0) {
1✔
108
            $this->builder->getLogger()->info('Nothing to do');
×
109
        }
110
    }
111

112
    /**
113
     * Set file processor object.
114
     */
115
    abstract public function setProcessor(): void;
116

117
    /**
118
     * Process a file.
119
     */
120
    abstract public function processFile(\Symfony\Component\Finder\SplFileInfo $file): string;
121

122
    /**
123
     * Encode file content.
124
     */
125
    public function encode(string $content = null): ?string
126
    {
127
        return $content;
1✔
128
    }
129

130
    /**
131
     * Decode file content.
132
     */
133
    public function decode(string $content = null): ?string
134
    {
135
        return $content;
1✔
136
    }
137
}
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

© 2025 Coveralls, Inc