Skip to content

Commit a367a79

Browse files
authored
Merge pull request #3 from adhocore/develop
Develop
2 parents c3819c9 + 58ef1bc commit a367a79

File tree

7 files changed

+67
-14
lines changed

7 files changed

+67
-14
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ coverage.xml
88
clover.xml
99
test.php
1010
*.ttf.png
11+
*.phar

bin/clish

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ if (\Phar::running() && !\extension_loaded('zlib')) {
1313
exit(1);
1414
}
1515

16+
if (!\extension_loaded('gd')) {
17+
echo "clish requires gd extension for exporting highlighted code to png image\n";
18+
19+
exit(1);
20+
}
21+
1622
if (\file_exists(__DIR__ . '/../../../autoload.php')) {
1723
require __DIR__ . '/../../../autoload.php';
1824
} elseif (\file_exists(__DIR__ . '/../../autoload.php')) {

box.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"chmod": "0755",
3+
4+
"check-requirements": false,
5+
6+
"banner": [
7+
"Clish is a PHP code highlight and/or export tool."
8+
],
9+
10+
"files": [
11+
"VERSION"
12+
],
13+
14+
"directories-bin": [
15+
"font"
16+
],
17+
18+
"compression": "GZ",
19+
"datetime": "release-date"
20+
}

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"php": ">=7.0.0",
2828
"ext-dom": "*",
2929
"ext-gd": "*",
30-
"adhocore/cli": "^0.6.0"
30+
"adhocore/cli": "^0.7.0"
3131
},
3232
"require-dev": {
3333
"phpunit/phpunit": "^6.5 || ^7.5"

src/Console/ClishCommand.php

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,17 @@ public function __construct()
2525
parent::__construct('clish', 'PHP CLI syntax highlight and/or export.');
2626

2727
$this
28-
->option('-o --output', 'Output filepath where PNG image is exported')
29-
->option('-e --echo', 'Forces echo to STDOUT when --output is passed')
28+
->option('-o --output', 'Output filepath where PNG image is exported', null, '')
29+
->option('-e --echo', 'Forces echo to STDOUT when --output is passed', null, '')
3030
->option('-f --file', \implode("\n", [
3131
'Input PHP file to highlight and/or export',
3232
'(will read from piped input if file not given)',
33-
]))
34-
->option('-F --font', 'Font to use for export to png')
33+
]), null, '')
34+
->option('-F --font', 'Font to use for export to png', null, '')
3535
->usage(
3636
'<bold> $0</end> <comment>--file file.php</end> ## print<eol/>'
37-
. '<bold> cat file.php | $0</end> ## from piped stream<eol/>'
38-
. '<bold> $0</end> <comment>< file.php ## from redirected stdin<eol/>'
37+
. '<comment> cat file.php |</end> <bold>$0</end> ## from piped stream<eol/>'
38+
. '<bold> $0</end> <comment>< file.php</end> ## from redirected stdin<eol/>'
3939
. '<bold> $0</end> <comment>--file file.php --output file.png</end> ## export<eol/>'
4040
. '<bold> $0</end> <comment>--file file.php --output file.png --echo</end> ## print + export<eol/>'
4141
. '<bold> $0</end> <comment>-f file.php -o file.png -F dejavu</end> ## export in dejavu font<eol/>'
@@ -44,7 +44,7 @@ public function __construct()
4444

4545
public function interact(Interactor $io)
4646
{
47-
if (\is_string($this->file)) {
47+
if ($this->file) {
4848
return;
4949
}
5050

@@ -92,6 +92,23 @@ protected function doExport(string $code = null)
9292
\mkdir(\dirname($this->output), 0755, true);
9393
}
9494

95-
(new Exporter($code))->export($this->output, \array_filter(['font' => $this->font]));
95+
$options = ['font' => $this->fixFont($this->font ?: 'ubuntu')];
96+
97+
(new Exporter($code))->export($this->output, $options);
98+
}
99+
100+
/** @codeCoverageIgnore */
101+
protected function fixFont(string $font): string
102+
{
103+
if (\Phar::running()) {
104+
$basename = \basename($font, '.ttf');
105+
$pharfont = __DIR__ . "/../../font/$basename.ttf";
106+
107+
if (!\is_file($font) && \is_file($pharfont)) {
108+
\copy($pharfont, $font = sys_get_temp_dir() . '/clish.ttf');
109+
}
110+
}
111+
112+
return $font;
96113
}
97114
}

src/Exporter.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,14 @@ protected function setOptions(array $options)
6868
return;
6969
}
7070

71-
$font = $options['font'];
72-
if (!\is_file($font)) {
73-
$basename = \basename($font, '.ttf');
74-
$font = __DIR__ . "/../font/$basename.ttf";
71+
if (\is_file($options['font'])) {
72+
$this->font = $options['font'];
73+
74+
return;
7575
}
7676

77-
if (!\is_file($font)) {
77+
$base = \basename($options['font'], '.ttf');
78+
if (!\is_file($font = __DIR__ . "/../font/$base.ttf")) {
7879
throw new \InvalidArgumentException('The given font doesnot exist.');
7980
}
8081

tests/ExporterTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ public function testSetOptions()
5151
// $this->assertSame(\file_get_contents($this->ref), \file_get_contents($this->out));
5252
}
5353

54+
public function testSetOptionsFont()
55+
{
56+
Exporter::for(__DIR__ . '/../example.php')->export($this->out, ['font' => __DIR__ . '/../font/dejavu.ttf']);
57+
58+
$this->assertFileExists($this->out, 'It should export with given font');
59+
// $this->assertSame(\file_get_contents($this->ref), \file_get_contents($this->out));
60+
}
61+
5462
/**
5563
* @expectedException \InvalidArgumentException
5664
* @expectedExceptionMessage The given font doesnot exist.

0 commit comments

Comments
 (0)