Skip to content

Commit 26e7221

Browse files
Added cli tool
1 parent f43c366 commit 26e7221

File tree

4 files changed

+316
-129
lines changed

4 files changed

+316
-129
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ completion for your IDE when encrypting your library with e.g.
1414
A basic installation via Composer could be done this way:
1515

1616
```bash
17-
$ composer require setasign/php-stub-generator ^0.1.1-alpha
17+
$ composer require setasign/php-stub-generator ^0.2.0-alpha
1818
```
1919

2020
Composer will install the library to your project's `vendor/setasign/php-stub-generator` directory.
@@ -40,6 +40,10 @@ $output = $generator->generate();
4040
file_put_contents(__DIR__ . '/setapdf-core-stub.php', $output);
4141
```
4242

43+
Alternatively you could just call the cli helper.
44+
45+
```bash
46+
vendor/bin/php-stub-generator generate vendor/setasign/setapdf-core/library setapdf-core-stub.php
47+
4348
## TODO
4449
- Traits are not supported yet
45-
- Command line tool: bin/generate-stub vendor/setasign/setapdf-core/library setapdf-core-stub.php

bin/php-stub-generator

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
use Symfony\Component\Console\Application;
5+
use Symfony\Component\Console\Input\InputArgument;
6+
use Symfony\Component\Console\Input\InputOption;
7+
8+
if (version_compare('7.1.0', PHP_VERSION, '>=')) {
9+
fwrite(
10+
STDERR,
11+
sprintf(
12+
'This version of php-stub-generator is supported on PHP 7.1.' . PHP_EOL .
13+
'You are using PHP %s (%s).' . PHP_EOL,
14+
PHP_VERSION,
15+
PHP_BINARY
16+
)
17+
);
18+
19+
die(1);
20+
}
21+
22+
if (!ini_get('date.timezone')) {
23+
ini_set('date.timezone', 'UTC');
24+
}
25+
26+
foreach ([__DIR__ . '/../../autoload.php', __DIR__ . '/../vendor/autoload.php', __DIR__ . '/vendor/autoload.php'] as $file) {
27+
if (file_exists($file)) {
28+
define('PHP_STUB_GENERATOR_COMPOSER_INSTALL', $file);
29+
break;
30+
}
31+
}
32+
33+
if (!defined('PHP_STUB_GENERATOR_COMPOSER_INSTALL')) {
34+
fwrite(
35+
STDERR,
36+
'You need to set up the project dependencies using Composer:' . PHP_EOL . PHP_EOL .
37+
' composer install' . PHP_EOL . PHP_EOL .
38+
'You can learn all about Composer on https://getcomposer.org/.' . PHP_EOL
39+
);
40+
41+
die(1);
42+
}
43+
44+
require PHP_STUB_GENERATOR_COMPOSER_INSTALL;
45+
46+
(new Application('setasign php-stub-generator', 'v0.2.0-alpha'))
47+
->register('generate')
48+
->setDescription('Build the stub-file')
49+
->addArgument(
50+
'source',
51+
InputArgument::REQUIRED,
52+
'The root directory of your library'
53+
)
54+
->addArgument(
55+
'output',
56+
InputArgument::REQUIRED,
57+
'The output file'
58+
)
59+
->addOption(
60+
'exclude',
61+
null,
62+
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED,
63+
'Exclude any directories'
64+
)
65+
->setCode(function (
66+
\Symfony\Component\Console\Input\InputInterface $input,
67+
\Symfony\Component\Console\Output\OutputInterface $output
68+
) {
69+
$sourceDirectory = $input->getArgument('source');
70+
$outputPath = $input->getArgument('output');
71+
$excludes = $input->getOption('exclude');
72+
73+
if (!is_dir($sourceDirectory)) {
74+
throw new \InvalidArgumentException('Invalid source directory!');
75+
}
76+
77+
$generator = new \setasign\PhpStubGenerator\PhpStubGenerator();
78+
$generator->addSource(
79+
'setapdf-core',
80+
new \setasign\PhpStubGenerator\Reader\AllFiles($sourceDirectory, $excludes)
81+
);
82+
$stubs = $generator->generate();
83+
file_put_contents($outputPath, $stubs, LOCK_EX);
84+
$output->write('The stubs were successfully generated to: ' . realpath($outputPath));
85+
})
86+
->getApplication()
87+
->run();

composer.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@
1212

1313
"require": {
1414
"php": "^7.1",
15-
"ext-mbstring": "*",
1615
"ext-pcre": "*",
1716

18-
"goaop/parser-reflection": "^1.2"
17+
"symfony/polyfill-mbstring": "^1.5",
18+
"goaop/parser-reflection": "^1.2",
19+
"symfony/console": "^3.3"
1920
},
2021

2122
"require-dev": {
@@ -32,5 +33,9 @@
3233
"psr-4": {
3334
"setasign\\PhpStubGenerator\\Tests\\": "tests/"
3435
}
35-
}
36+
},
37+
38+
"bin": [
39+
"./bin/php-stub-generator"
40+
]
3641
}

0 commit comments

Comments
 (0)