Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
80.00% covered (warning)
80.00%
16 / 20
40.00% covered (danger)
40.00%
2 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
ExecTrait
80.00% covered (warning)
80.00%
16 / 20
40.00% covered (danger)
40.00%
2 / 5
15.57
0.00% covered (danger)
0.00%
0 / 1
 logLn
n/a
0 / 0
n/a
0 / 0
0
 hasNiceSupport
66.67% covered (warning)
66.67%
4 / 6
0.00% covered (danger)
0.00%
0 / 1
5.93
 checkNiceSupport
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
2.03
 niceOption
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 logExecOutput
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
 checkOperationalityExecTrait
50.00% covered (danger)
50.00%
1 / 2
0.00% covered (danger)
0.00%
0 / 1
2.50
1<?php
2
3namespace WebPConvert\Convert\Converters\ConverterTraits;
4
5use WebPConvert\Convert\Exceptions\ConversionFailed\ConverterNotOperational\SystemRequirementsNotMetException;
6use ExecWithFallback\ExecWithFallback;
7
8/**
9 * Trait for converters that uses exec() or similar
10 *
11 * @package    WebPConvert
12 * @author     Bjørn Rosell <it@rosell.dk>
13 * @since      Class available since Release 2.0.0
14 */
15trait ExecTrait
16{
17
18    abstract protected function logLn($msg, $style = '');
19
20
21    /**
22     * Helper function for examining if "nice" command is available
23     *
24     * @return  boolean  true if nice is available
25     */
26    protected static function hasNiceSupport()
27    {
28        ExecWithFallback::exec("nice 2>&1", $niceOutput);
29
30        if (is_array($niceOutput) && isset($niceOutput[0])) {
31            if (preg_match('/usage/', $niceOutput[0]) || (preg_match('/^\d+$/', $niceOutput[0]))) {
32                /*
33                 * Nice is available - default niceness (+10)
34                 * https://www.lifewire.com/uses-of-commands-nice-renice-2201087
35                 * https://www.computerhope.com/unix/unice.htm
36                 */
37
38                return true;
39            }
40            return false;
41        }
42        return false; // to satisfy phpstan
43    }
44
45    protected function checkNiceSupport()
46    {
47        $ok = self::hasNiceSupport();
48        if ($ok) {
49            $this->logLn('Tested "nice" command - it works :)');
50        } else {
51            $this->logLn(
52                '**No "nice" support. To save a few ms, you can disable the "use-nice" option.**'
53            );
54        }
55        return $ok;
56    }
57
58    protected static function niceOption()
59    {
60        return ['use-nice', 'boolean', [
61            'title' => 'Use nice',
62            'description' =>
63                'If *use-nice* is set, it will be examined if the *nice* command is available. ' .
64                'If it is, the binary is executed using *nice*. This assigns low priority to the process and ' .
65                'will save system resources - but result in slower conversion.',
66            'default' => true,
67            'ui' => [
68                'component' => 'checkbox',
69                'advanced' => true
70            ]
71        ]];
72    }
73
74    /**
75     * Logs output from the exec call.
76     *
77     * @param  array  $output
78     *
79     * @return  void
80     */
81    protected function logExecOutput($output)
82    {
83        if (is_array($output) && count($output) > 0) {
84            $this->logLn('');
85            $this->logLn('Output:', 'italic');
86            foreach ($output as $line) {
87                $this->logLn(print_r($line, true));
88            }
89            $this->logLn('');
90        }
91    }
92
93    /**
94     * Check basic operationality of exec converters (that the "exec" or similar function is available)
95     *
96     * @throws  WebPConvert\Convert\Exceptions\ConversionFailed\ConverterNotOperational\SystemRequirementsNotMetException
97     * @return  void
98     */
99    public function checkOperationalityExecTrait()
100    {
101        if (!ExecWithFallback::anyAvailable()) {
102            throw new SystemRequirementsNotMetException(
103                'exec() is not enabled (nor is alternative methods, such as proc_open())'
104            );
105        }
106    }
107}