Skip to content

Commit cbe80be

Browse files
committed
Removed the Namespace dependency. GetUsed can now be used on every PHP script.
1 parent 604232b commit cbe80be

11 files changed

+1053
-683
lines changed

Filesystem.php

Lines changed: 739 additions & 0 deletions
Large diffs are not rendered by default.

GetUsed.config.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,7 @@
88

99
'config' => [
1010
'args' => [
11-
'project_dir',
12-
'autoload',
13-
'include',
1411
'file',
15-
'class',
1612
'return',
1713
'comment_out',
1814
],
@@ -25,11 +21,7 @@
2521

2622
'help' => [
2723
'args' => [
28-
'project_dir' => 'Path to project',
29-
'autoload' => "Path to './vendor/autoload.php' file",
30-
'include' => 'Comma separated paths to include additionally files',
3124
'file' => 'File to get the use keywords for',
32-
'class' => 'Set a Namespace to select a Class from availables',
3325
'return' => 'Set return content type',
3426
'comment_out' => 'Avoid commenting out already defined use keywords found in the Class',
3527
],

GetUsed.php

Lines changed: 27 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -6,124 +6,53 @@
66
$base = dirname(__FILE__);
77
require_once "{$base}/src/Used.php";
88
$getCnf = require_once "{$base}/GetUsed.config.php";
9-
if (is_file($usrCnf = "{$base}/user.config.php"))
10-
$getCnf = array_replace_recursive($getCnf, require_once $usrCnf);
119

1210
/** @var mixed parsed arguments */
1311
parse_str(implode('&', $argv), $argv);
1412

15-
/**
16-
* @var array check if shell options are called and execute if ['-h', ...]
17-
*/
18-
foreach($getCnf['config']['options'] as $key => $shortOpt)
19-
if (isset($argv[$shortOpt]) AND isset($getCnf[$key]))
20-
exit(json_encode([$key => $getCnf[$key]], JSON_PRETTY_PRINT) . PHP_EOL);
21-
2213
/**
2314
* @var mixed If "file=" is left out in command
2415
*/
2516
if (!isset($argv['file']) AND $pFilename = ($_SERVER['argv'][1] ?? false))
26-
if (file_exists($pFilename))
17+
if (is_file($pFilename))
2718
$argv['file'] = $pFilename;
2819

2920
/**
30-
* @var mixed sets & args
21+
* @var mixed set args
3122
*/
32-
$autoloadFile = '/vendor/autoload.php';
33-
$projectDir = $argv['project_dir'] ?? null;
34-
$argv['include'] = $argv['include'] ?? null;
35-
$argv['file'] = $argv['file'] ?? null ? "{$projectDir}{$argv['file']}" : null;
23+
$argv['file'] = $argv['file'] ?? null;
3624
$argv['comment_out'] = ('false' === ($argv['comment_out'] ?? false)) ? false : true;
37-
$classes = get_declared_classes();
38-
$isIncluded = [];
39-
40-
/**
41-
* @var array Set config
42-
*/
43-
Used::setConfig(['comment_out_existing' => $argv['comment_out'] ? true : false]);
44-
45-
/**
46-
* @var mixed Search path to "/vendor/autoload.php" in the file environment iteratively. Sets "/vendor/autoload.php" as first file to include
47-
*/
48-
if ($argv['file'] AND $getAutoloader = Used::getAutoloadPath($argv['file']))
49-
$argv['include'] = "{$getAutoloader},{$argv['include']},{$argv['file']}";
5025

5126
/**
52-
* @var mixed Set all files to include
27+
* @var array check if shell options are called and execute if ['-c', ...]
5328
*/
54-
if ($argv['include'] ?? null)
55-
$argv['include'] = trim($argv['include'], ',') . ",{$argv['file']}";
56-
else $argv['include'] = $argv['file'];
57-
58-
/**
59-
* @var mixed include all
60-
*/
61-
if ($includeAll = ($argv['include'] ?? null))
62-
foreach(explode(',', $includeAll) as $inc)
63-
if ($inc AND !in_array($inc, $isIncluded) AND is_file($inc) AND $isIncluded[] = $inc)
64-
include_once $inc;
65-
66-
/**
67-
* @var mixed check included files
68-
*/
69-
if ($isIncluded AND $diff = array_diff(get_declared_classes(), $classes))
70-
$class = end($diff);
71-
else $diff = false;
72-
73-
/**
74-
* @var string get Used-Statements for a specific class through the "class"-parameter
75-
*/
76-
$class = $argv['class'] ?? $class ?? null;
77-
78-
/**
79-
* @var mixed Set the right class to load, if nothing is specified
80-
*/
81-
if ($diff AND !($argv['class'] ?? null)) {
82-
foreach($diff as $id => $namespace) {
83-
$namespace = str_replace('\\', '/', $namespace);
84-
if (str_contains($argv['file'], $namespace)) {
85-
$class = $diff[$id];
86-
break;
87-
}
88-
}
89-
}
29+
foreach($getCnf['config']['options'] as $key => $shortOpt)
30+
if (isset($argv[$shortOpt]) AND isset($getCnf[$key]))
31+
exit(json_encode([$key => $getCnf[$key]], JSON_PRETTY_PRINT) . PHP_EOL);
9032

9133
/**
92-
* @var mixed Fix class if the given class still don't match with available
34+
* @var string get use Keywords
9335
*/
94-
if ($diff AND !in_array($class, $diff)) {
95-
foreach($diff as $id => $namespace) {
96-
$namespace = str_replace('\\', '/', $namespace);
97-
if ($checkIterate = Used::searchPathLeftToRight($argv['file'], $namespace, '/')) {
98-
$class = str_replace('/', '\\', $checkIterate);
99-
break;
100-
}
101-
}
102-
}
36+
$getUsed = (new Used)->get($argv['file'], $argv);
10337

10438
/**
105-
* @var string print Used-Statements or Error
39+
* @var string response
10640
*/
107-
if ($class) {
108-
if ($getUsed = ((new Used)->getClassUseList($class))) {
109-
$r = [
110-
'file' => $getUsed['filename'] ?? $argv['file'] ?? null,
111-
'use_for_class' => $class,
112-
'start' => $_SERVER['REQUEST_TIME_FLOAT'] ?? null,
113-
'end' => microtime(true),
114-
'print' => $getUsed['print'] ?? null,
115-
];
116-
if ('json' === ($argv['return'] ?? null)) {
117-
$r = json_encode(array_merge($r, ['included' => $diff, 'response' => $getUsed,]), JSON_PRETTY_PRINT);
118-
} else {
119-
$rPrint = [];
120-
foreach($r as $k => $v)
41+
if ($argv['file'] AND $getUsed) {
42+
$r = [
43+
'file' => trim($argv['file']),
44+
'start' => $_SERVER['REQUEST_TIME_FLOAT'] ?? null,
45+
'end' => microtime(true),
46+
'print' => $getUsed['print'] ?? null,
47+
];
48+
if ('json' === ($argv['return'] ?? null)) {
49+
$r = json_encode($r, JSON_PRETTY_PRINT);
50+
} else {
51+
$rPrint = [];
52+
foreach($r as $k => $v)
53+
if (!in_array($k, ['class', 'function', 'constant', 'file_content']))
12154
$rPrint[] = 'print' === $k ? "\n{$v}" : "// {$k} = {$v}";
122-
$r = PHP_EOL . implode(PHP_EOL, $rPrint) . PHP_EOL . PHP_EOL;
123-
}
124-
exit($r);
125-
} else exit("Error processing the Class: {$class}");
126-
}
127-
128-
// if script somehow ends up here, exit anyway
129-
exit("No file defined, set ?file=/path/to/file.php\n");
55+
$r = sprintf('%1$s%2$s%1$s%1$s', PHP_EOL, implode(PHP_EOL, $rPrint));
56+
}
57+
exit($r);
58+
} else exit(sprintf('Error processing the file: %s', $argv['file']));

README.md

Lines changed: 40 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
1-
# Namespace helper - get use Namespace\Keywords;
1+
# Namespace helper - use Namespace\Keywords;
22

3-
This package can create `use Keywords;` with all the names of Classes, Functions and Constants used within a Class. It can be used from the Terminal or alternatively from a simple [Web interface](./www/used/).
3+
This package can parse PHP scripts to extract the names of PHPs internal Classes, Functions and Constants used within a Class. The parsed Names can then be used to define `use Namespace\Keywords;` in namespaced Classes.
44

5-
The `use` keyword can be used within Classes to tell PHP, which Classes, Functions and Constants to use internally for Functions used in your Class. It also can boost your scripts by pointing PHP to the right Namespace to use. For example, if you call `json_encode()` within a Class, PHP searches in the calling Class for a Function with the name `json_encode()` first, before searching it in the global Namespace (if at all). You can speed up the process with a Backslash before the function name, like: `\json_encode()`, but IDK, it looks awful. To make it a bit prettier, you can define any used Functions, Classes and Constants at the very top of your Class with the 'use'-keyword and continue programming like a Boss.
65

7-
It also allows you to Alias your scripts quick and easy.
6+
__VSCode Screenshot__
7+
8+
It's pretty hard to talk about this Topic, because it's just called "Namespace" ...
9+
10+
![Visual Studio Code Example Response](/www/used/assets/screenshot-vscode.png)
11+
12+
The `use` keyword can be used within Classes to tell PHP, which Classes, Functions and Constants to use internally for Functions used in your Class. It also can boost your scripts by pointing PHP to the right Namespace to use. For example, if you call `json_encode()` within a Class, PHP searches in the calling Class for a Function with the name `json_encode()` first, before searching it in the global Namespace (if at all). You can speed up the process with a Backslash before the function name, like: `\json_encode()`, but IDK, it looks awful. An alternate is to define any used Function, Class and Constant at the very top of your Class with the 'use'-keyword. And that's for what this Package is meant to be.
13+
14+
The `use` keyword also allows you to Alias and change your scripts quick and easy.
815

916
```php
10-
use function needToChangeHtmlspecialchars as htmlspecialchars;
11-
# if you now call "htmlspecialchars()" in your class, PHP will
12-
# use "needToChangeHtmlspecialchars()" to execute it.
17+
use function myOwnJsonEcode as json_encode;
18+
# if you now call "json_encode()" in your class,
19+
# PHP will use "myOwnJsonEcode()" to execute it.
1320
```
1421

15-
This Package searches in the parent directory of the given file for `./vendor/autoload.php` iteratively and includes it, if found. It's required, if the Environment is namespaced and Classes are depending on other Classes within the Project.
16-
1722
## Install Many\Dev\Used
1823

1924
```sh
@@ -30,56 +35,46 @@ git clone https://github.com/eypsilon/get-used.git .
3035
chmod -v 770 ~/bin/many/get-used/GetUsed.php
3136
```
3237

33-
Set an Alias (feel free to set one you feel comfortable with)
38+
__Usage from Terminal__
3439

3540
```sh
36-
~$ sudo gedit ~/.bash_aliases
37-
# put
38-
alias GetUsed='~/bin/many/get-used/GetUsed.php'
39-
# refresh aliases
40-
~$ source ~/.bash_aliases
41+
~/bin/many/get-used/GetUsed.php file="/path/to/src/AnyClass.php"
4142
```
4243

43-
and use it from the Terminal
44+
__Via [Web interface](./www/used/) using PHPs dev-server__
4445

4546
```sh
46-
GetUsed file="/path/to/src/AnyClass.php"
47-
# or
48-
GetUsed /path/to/src/AnyClass.php
49-
# Get help -h | info -i | config -c
50-
GetUsed -h
47+
cd ~/bin/many/get-used/www/used
48+
php -S localhost:8000
5149
```
5250

51+
and open [localhost:8000](http://localhost:8000)
52+
5353
---
5454

55-
### Via Browser as a Web Service
55+
## Set an Alias (optional)
5656

57-
You can use PHPs dev-server for temporary usage or configure a vhost and point the __DocumentRoot__ to `~/bin/many/get-used/www/used`.
57+
Feel free to set one you feel comfortable with.
5858

5959
```sh
60-
# using PHPs dev-server
61-
cd ~/bin/many/get-used/www/used
62-
php -S localhost:8000
60+
~$ sudo gedit ~/.bash_aliases
61+
# put
62+
alias GetUsed='~/bin/many/get-used/GetUsed.php'
63+
# refresh aliases
64+
~$ source ~/.bash_aliases
6365
```
64-
65-
and open [localhost:8000](http://localhost:8000)
66-
67-
Or, set an Alias, that can start the dev-server and a Browser with the dev-url at once. You can still feel free to set any Alias you feel comfortable with, always.
68-
6966
```sh
70-
# set alias in ~/.bash_aliases
71-
alias phpserver='firefox http://localhost:8000; php -S localhost:8000'
72-
73-
# and whenever you need a dev-server
74-
cd ~/any/dir/app/public
75-
phpserver
67+
GetUsed file="/path/to/src/AnyClass.php"
68+
# or
69+
GetUsed /path/to/src/AnyClass.php
70+
# Get help -h | info -i | config -c
71+
GetUsed -h
7672
```
77-
7873
---
7974

80-
### Used in Visual Studio Code
75+
### Used Keywords in Visual Studio Code
8176

82-
You can use this Lib also in VSCode. Set a key combination in `~/.config/Code/User/keybindings.json`
77+
You can use this Package also in VSCode. Set a key combination in `~/.config/Code/User/keybindings.json`
8378

8479
```json
8580
{
@@ -103,9 +98,10 @@ GetUsed /path/to/src/Http/Curler.php
10398

10499
```php
105100
// file = /path/to/src/Http/Curler.php
106-
// use_for_class = Many\Http\Curler
101+
// start = 1661266197.6779
102+
// end = 1661266197.7762
107103

108-
/** class(2), function(2), constant(2) */
104+
/** lines(-), defined(0), taken(0), constant(2), class(2), function(2), total(6) */
109105

110106
use DateTime;
111107
use DateTimeZone;
@@ -117,8 +113,8 @@ use const JSON_UNESCAPED_SLASHES;
117113

118114
#### Screenshots
119115

120-
__Web Interface__
121-
A Web-based GUI for `GetUsed`, because Terminal. It burns in my eyes when i look at it ...
116+
__Web Interface for `GetUsed`__
117+
122118

123119
![Web Interface Example Response](/www/used/assets/screenshot-sw.png)
124120

0 commit comments

Comments
 (0)