Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
81.82% covered (success)
81.82%
9 / 11
50.00% covered (warning)
50.00%
2 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Composer
81.82% covered (success)
81.82%
9 / 11
50.00% covered (warning)
50.00%
2 / 4
7.29
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 isAvailable
75.00% covered (success)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
3.14
 dumpAutoload
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 run
75.00% covered (success)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
1<?php
2declare(strict_types=1);
3/**
4 * Pop PHP Framework (https://www.popphp.org/)
5 *
6 * @link       https://github.com/popphp/pop-bootstrap
7 * @author     Nick Sagona, III <nick@popphp.org>
8 * @copyright  Copyright (c) 2009-2026 Nick Sagona, III
9 * @license    https://www.popphp.org/license     New BSD License
10 */
11
12/**
13 * @namespace
14 */
15namespace Pop\Kettle\Model;
16
17use Pop\Utils\AbstractModel;
18
19/**
20 * Console composer model class
21 *
22 * @category   Pop\Kettle
23 * @package    Pop\Kettle
24 * @author     Nick Sagona, III <nick@popphp.org>
25 * @copyright  Copyright (c) 2009-2026 Nick Sagona, III
26 * @license    https://www.popphp.org/license     New BSD License
27 * @version    3.0.0
28 */
29class Composer extends AbstractModel
30{
31
32    /**
33     * composer binary to invoke
34     * @var string
35     */
36    protected string $composerBinary = 'composer';
37
38    /**
39     * Constructor
40     *
41     * @param string $composerBinary
42     */
43    public function __construct(string $composerBinary = 'composer')
44    {
45        parent::__construct();
46        $this->composerBinary = $composerBinary;
47    }
48
49    /**
50     * Whether the configured composer binary is available on the PATH
51     *
52     * @return bool
53     */
54    public function isAvailable(): bool
55    {
56        if (PHP_OS_FAMILY === 'Windows') {
57            exec('where ' . escapeshellarg($this->composerBinary), $output, $exitCode);
58        } else {
59            exec('command -v ' . escapeshellarg($this->composerBinary) . ' 2>/dev/null', $output, $exitCode);
60        }
61
62        return ($exitCode === 0) && !empty($output);
63    }
64
65    /**
66     * Run `composer dump-autoload` at the given location
67     *
68     * @param  string $location
69     * @return int
70     */
71    public function dumpAutoload(string $location): int
72    {
73        return $this->run([$this->composerBinary, 'dump-autoload'], $location);
74    }
75
76    /**
77     * Run a command with inherited stdio, returning its exit code
78     *
79     * @param  array  $command
80     * @param  string $cwd
81     * @return int
82     */
83    protected function run(array $command, string $cwd): int
84    {
85        $process = proc_open($command, [0 => STDIN, 1 => STDOUT, 2 => STDERR], $pipes, $cwd);
86        if (!is_resource($process)) {
87            return 1;
88        }
89        return proc_close($process);
90    }
91
92}