Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
75.76% covered (warning)
75.76%
50 / 66
44.44% covered (danger)
44.44%
4 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
Cli
75.76% covered (warning)
75.76%
50 / 66
44.44% covered (danger)
44.44%
4 / 9
33.90
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 run
81.25% covered (warning)
81.25%
13 / 16
0.00% covered (danger)
0.00%
0 / 1
6.24
 runGet
60.00% covered (warning)
60.00%
6 / 10
0.00% covered (danger)
0.00%
0 / 1
3.58
 runList
84.62% covered (warning)
84.62%
11 / 13
0.00% covered (danger)
0.00%
0 / 1
6.13
 runMatch
69.23% covered (warning)
69.23%
9 / 13
0.00% covered (danger)
0.00%
0 / 1
5.73
 help
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 error
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 output
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getExitCode
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 */
18
19namespace Cdb;
20
21use Throwable;
22
23/**
24 * @internal For use by bin/cdb only.
25 */
26final class Cli {
27    /** @var int */
28    private $exitCode = 0;
29
30    /** @var resource */
31    private $out;
32
33    /** @var string */
34    private $self;
35
36    /** @var string */
37    private $filepath;
38
39    /** @var string */
40    private $action;
41
42    /** @var string[] */
43    private $params;
44
45    /**
46     * @param resource $out An open output handle for fwrite()
47     * @param string[] $args
48     */
49    public function __construct( $out, array $args ) {
50        $this->out = $out;
51        $this->self = $args[0] ?? './bin/cdb';
52        $this->filepath = $args[1] ?? '';
53        $this->action = $args[2] ?? '';
54        $this->params = array_slice( $args, 3 );
55    }
56
57    /** Main method. */
58    public function run() {
59        try {
60            switch ( $this->action ) {
61                case 'get':
62                    $this->runGet();
63                    break;
64                case 'list':
65                    $this->runList();
66                    break;
67                case 'match':
68                    $this->runMatch();
69                    break;
70                default:
71                    $this->exitCode = 1;
72                    $this->help();
73                    break;
74            }
75        } catch ( Throwable $e ) {
76            $this->exitCode = 1;
77            $this->output( (string)$e );
78        }
79    }
80
81    private function runGet(): void {
82        if ( count( $this->params ) !== 1 ) {
83            $this->error( "The 'get' action requires one parameter." );
84            return;
85        }
86        $key = $this->params[0];
87
88        $dbr = Reader::open( $this->filepath );
89        $value = $dbr->get( $key );
90        if ( $value == false ) {
91            $this->error( "Key '$key' not found." );
92            return;
93        }
94        $this->output( $value );
95    }
96
97    private function runList(): void {
98        if ( count( $this->params ) > 1 ) {
99            $this->error( "The 'list' action accepts only one parameter." );
100            return;
101        }
102        $max = (int)( $this->params[0] ?? '100' );
103
104        $dbr = Reader::open( $this->filepath );
105        $key = $dbr->firstkey();
106        $count = 0;
107        while ( $key !== false && $count < $max ) {
108            $this->output( $key );
109            $count++;
110            $key = $dbr->nextkey();
111        }
112        if ( $count === $max && $key !== false ) {
113            $this->output( "\n(more keys exist…)" );
114        }
115    }
116
117    private function runMatch(): void {
118        if ( count( $this->params ) !== 1 ) {
119            $this->error( "The 'match' action requires one parameter." );
120            return;
121        }
122        $pattern = $this->params[0];
123        // @phan-suppress-next-line PhanParamSuspiciousOrder
124        if ( preg_match( $pattern, '' ) === false ) {
125            $this->error( 'Invalid regular expression pattern.' );
126            return;
127        }
128
129        $dbr = Reader::open( $this->filepath );
130        $key = $dbr->firstkey();
131        while ( $key !== false ) {
132            if ( preg_match( $pattern, $key ) ) {
133                $this->output( $key );
134            }
135            $key = $dbr->nextkey();
136        }
137    }
138
139    private function help(): void {
140        $this->output( <<<TEXT
141usage: {$this->self} <file> [<action>=list] [<parameters...>]
142
143actions:
144   get <key>         Get the value for a given key.
145   list [<max>=100]  List all keys in the file
146   match <pattern>   List keys matching a regular expression.
147                     The pattern must include delimiters (e.g. / or #).
148TEXT
149        );
150    }
151
152    private function error( string $text ): void {
153        $this->exitCode = 1;
154        $this->output( "\nerror: $text\n------\n" );
155        $this->help();
156    }
157
158    private function output( string $text ): void {
159        fwrite( $this->out, $text . "\n" );
160    }
161
162    /**
163     * Get exit status code for the process.
164     *
165     * @return int
166     */
167    public function getExitCode(): int {
168        return $this->exitCode;
169    }
170}