-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathphpinspect-autoload.el
545 lines (462 loc) · 23.2 KB
/
phpinspect-autoload.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
;;; phpinspect-autoload.el --- PHP parsing and completion package -*- lexical-binding: t; -*-
;; Copyright (C) 2021-2023 Free Software Foundation, Inc
;; Author: Hugo Thunnissen <[email protected]>
;; Keywords: php, languages, tools, convenience
;; Version: 2.1.0
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;; Code:
(require 'cl-lib)
(require 'phpinspect-fs)
(require 'phpinspect-util)
(require 'phpinspect-type)
(require 'phpinspect-pipeline)
(require 'phpinspect-typedef)
(require 'json)
(defcustom phpinspect-autoload-classmaps nil
"Enable support for classmap auoload directives.
The classmap autoload directive is a key in the composer.json
which allows package authors to define directories which should
be parsed wholesale to discover available classes and functions.
Enabling this feature can make indexation take considerably
longer. It can also make the in-memory index larger as all files
in the classmap directories are indexed in their entirety.
On low powered systems, you may want to keep this feature
disabled unless your projects depend heavily on code which uses
the classmap directive.
Note: A prominent package which uses the classmap directive is
PHPUnit. Users of PHPUnit will want to have this feature
enabled.
As of [2024-09-28] this is a new feature and therefore subject to
change and not enabled by default."
:type 'boolean
:group 'phpinspect)
(defcustom phpinspect-autoload-progress-interval 10
"Report indexation progress every X amount of discovered types."
:type 'integer
:group 'phpinspect)
(cl-defstruct (phpinspect-psrX
(:constructor phpinspect-make-psrX-generated))
"A base structure to be included in PSR autoload strategy
structures."
(autoloader nil
:type phpinspect-autoloader)
(own nil
:type boolean)
(fs nil
:type phpinspect-fs)
(directories nil
:type list
:documentation
"The directories that this autoloader finds code in.")
(types-found
nil
:type list
:documentation
"List of fully qualified type names that was found during the last
execution of this strategy."))
(cl-defstruct (phpinspect-psr0
(:constructor phpinspect-make-psr0-generated)
(:include phpinspect-psrX))
"PSR0 autoload strategy.")
(cl-defstruct (phpinspect-psr4
(:constructor phpinspect-make-psr4-generated)
(:include phpinspect-psrX))
"PSR4 autoload strategy."
(prefix nil
:type string
:documentation "The namespace prefix for which the directories contain code."))
(cl-defmethod phpinspect-psrX-file-name-to-type-name ((psr4 phpinspect-psr4) directory file-name)
(phpinspect-file-name-to-type-name directory file-name (phpinspect-psr4-prefix psr4)))
(cl-defmethod phpinspect-psrX-file-name-to-type-name ((_psr0 phpinspect-psr0) directory file-name)
(phpinspect-file-name-to-type-name directory file-name))
(cl-defstruct (phpinspect-files (:constructor phpinspect-make-files))
(autoloader nil)
(list nil
:type list
:documentation
"List of files to be indexed")
(own nil
:type boolean))
(cl-defstruct (phpinspect-classmap (:constructor phpinspect-make-classmap)
(:include phpinspect-files)))
(cl-defgeneric phpinspect-al-strategy-request-type-name (_strategy _file-name)
"Returns FQN when STRATEGY is responsible for autoloading FILE-NAME.
STRATEGY is responsible for autoloading FILE-NAME when FILE-NAME
is in a (sub)directory that STRATEGY resolves types for.
This function returns a fully qualified type name regardless of
whether the type name is currently known to the autoloader or
not. To check if the type name is known to the autoloader, use
`phpinspect-autoloader-resolve'. Returned type names are inferred
from FILE-NAME.
Note: at the moment, this function only returns type names for
PSR autoload strategies. The nature of \"files\" and \"classmap\"
autoload strategies makes it impossible to guess what the fully
qualified name of a type should be based on the file name alone."
nil)
(cl-defmethod phpinspect-al-strategy-request-type-name ((strategy phpinspect-psrX) (file-name string))
(let ((file-name (expand-file-name file-name)))
(catch 'phpinspect--return
(dolist (dir (phpinspect-psrX-directories strategy))
(when (string-prefix-p (expand-file-name dir) file-name)
(throw 'phpinspect--return
(phpinspect-psrX-file-name-to-type-name strategy dir file-name)))))))
(defun phpinspect-autoloader-request-type-name (autoloader file-name)
(catch 'phpinspect--return
(dolist (strat (phpinspect-autoloader-local-strategies autoloader))
(when-let ((type-name (phpinspect-al-strategy-request-type-name strat file-name)))
(throw 'phpinspect--return type-name)))))
(defun phpinspect-autoloader-ensure-file-indexed (autoloader file-name)
(catch 'phpinspect--break
(dolist (strat (phpinspect-autoloader-local-strategies autoloader))
(when-let ((type-name (phpinspect-al-strategy-request-type-name strat file-name)))
(unless (phpinspect-autoloader-resolve autoloader type-name)
;; File is not known to autoloader, re-index is required
(phpinspect-al-strategy-re-execute strat)
(throw 'phpinspect--break t))))))
(cl-defstruct (phpinspect-autoloader
(:constructor phpinspect-make-autoloader))
(-progress-reporter nil
:type progress-reporter)
(-type-counter 0
:type integer)
(refresh-thread nil
:type thread)
(fs nil
:type phpinspect-fs)
(file-indexer nil
:type function)
(project-root-resolver nil
:type function)
(own-types
(make-hash-table :test 'eq :size 10000 :rehash-size 10000)
:type hash-table
:documentation
"The types that are local to the project that this autoloader belongs to.")
(types
(make-hash-table :test 'eq :size 10000 :rehash-size 10000)
:type hash-table
:documentation
"All types that are avaibale in the project that this autoloader
belongs to.")
(type-name-fqn-bags
(make-hash-table :test 'eq :size 3000 :rehash-size 3000)
:type hash-table
:documentation
"Hash table that contains lists of fully qualified names that
share a common bare name (see `phpinspect--type-base-name').
For example, The key \"Response\" could contain:
- \\App\\Response
- \\FrameWork\\Response
A common use for this table is to find fully qualified names that
could be added as imports for an ambiguous bare type name.")
(local-strategies
nil
:documentation
"List of autoload strategies local to the project."))
(defun phpinspect-autoloader-get-types-in-namespace (al namespace &optional exclude-vendor)
"Find types known to AL, defined in NAMESPACE.
If EXCLUDE-VENDOR is nil, all known namespaces are queried. If it
is non-nil, only project-local namespaces are queried.
NAMESPACE must be a string. It will be resolved using
`phpinspect--resolve-type-name' to ensure that it is a FQN.
Return value is a list containing instances of
`phpinspect--type'."
(cl-assert (stringp namespace))
(let ((namespace-fqn (phpinspect-intern-name
(phpinspect--resolve-type-name nil nil namespace)))
types)
(maphash (lambda (key _ignored)
(when (eq namespace-fqn (phpinspect-name-namespace key))
(push (phpinspect--make-type-generated :name key :fully-qualified t) types)))
(if exclude-vendor
(phpinspect-autoloader-own-types al)
(phpinspect-autoloader-types al)))
types))
(defun phpinspect-autoloader-get-own-types-in-namespace (al namespace)
"Find types known to AL, defined in project-local NAMESPACE."
(phpinspect-autoloader-get-types-in-namespace al namespace 'exclude-vendor))
(cl-defmethod phpinspect--read-json-file (fs file)
(with-temp-buffer
(phpinspect-fs-insert-file-contents fs file)
(goto-char 0)
(phpinspect-json-preset (json-read))))
(define-inline phpinspect-file-name-to-type-name (dir file-name &optional prefix)
(inline-letevals ((dir (inline-quote (apply #'file-name-concat (file-name-split ,dir))))
(file-name (inline-quote (apply #'file-name-concat (file-name-split ,file-name)))))
(inline-quote
(phpinspect-intern-name
(replace-regexp-in-string
"\\\\[\\]+"
"\\\\"
(concat "\\"
(or ,prefix "")
(replace-regexp-in-string
"/" "\\\\"
(string-remove-suffix
".php"
(string-remove-prefix ,dir ,file-name)))))))))
(defun phpinspect-find-composer-json-files (fs project-root)
"Find all composer.json files that are relevant for a project.
Usually, the relevant files are the current project's
composer.json and the composer.json files of all dependencies in
the vendor directory."
(let ((cj-path (concat project-root "/composer.json"))
(vendor-dir (concat project-root "/vendor"))
files)
(when (phpinspect-fs-file-exists-p fs cj-path)
(push `(local . ,cj-path) files))
(when (phpinspect-fs-file-directory-p fs vendor-dir)
(dolist (author-dir (phpinspect-fs-directory-files fs vendor-dir))
(when (and (phpinspect-fs-file-directory-p fs author-dir)
;; Exclude current/parent directory
(not (member (file-name-base author-dir) (list ".." "."))))
(dolist (dependency-dir (phpinspect-fs-directory-files fs author-dir))
(setq cj-path (concat dependency-dir "/composer.json"))
(when (and (phpinspect-fs-file-directory-p fs dependency-dir)
(phpinspect-fs-file-exists-p fs cj-path))
(push `(vendor . ,cj-path) files))))))
files))
(cl-defgeneric phpinspect-al-strategy-re-execute (strat)
"Execute STRAT again after a previous execution.
For some strategies, this will entail the removal of types
discovered during the previous execution of STRAT before
executing it again."
(phpinspect-al-strategy-execute strat))
(defun phpinspect-autoloader-delete-type (autoloader type-name)
"Remove all knowledge of TYPE-NAME from AUTOLOADER."
(let* ((base-name (phpinspect-name-base type-name))
(type-bag (phpinspect-autoloader-get-type-bag autoloader base-name)))
(puthash base-name
(delq type-name type-bag)
(phpinspect-autoloader-type-name-fqn-bags autoloader))
(remhash type-name (phpinspect-autoloader-types autoloader))
(remhash type-name (phpinspect-autoloader-own-types autoloader))))
(cl-defmethod phpinspect-al-strategy-re-execute ((strat phpinspect-psrX))
"Remove STRAT's found types from the autoloader and execute it again.
This function removes all types discovered during the previous
execution of the strategy from the autoloader and then
re-executes the strategy."
(let ((autoloader (phpinspect-psrX-autoloader strat)))
(dolist (fqn (phpinspect-psrX-types-found strat))
(phpinspect-autoloader-delete-type autoloader fqn))
(setf (phpinspect-psrX-types-found strat) nil)
(phpinspect-al-strategy-execute strat)))
(cl-defmethod phpinspect-al-strategy-execute ((strat phpinspect-psrX))
(let* ((fs (phpinspect-psrX-fs strat))
(al (phpinspect-psrX-autoloader strat))
(own (phpinspect-psrX-own strat))
(own-typehash (phpinspect-autoloader-own-types al))
(typehash (phpinspect-autoloader-types al))
types-found type-fqn)
(dolist (dir (phpinspect-psrX-directories strat))
(dolist (file (phpinspect-fs-directory-files-recursively fs dir "\\.php$"))
(setq type-fqn (phpinspect-psrX-file-name-to-type-name strat dir file))
(push type-fqn types-found)
(phpinspect-autoloader-put-type-bag al type-fqn)
(puthash type-fqn file typehash)
(when own
(puthash type-fqn file own-typehash))))
(setf (phpinspect-psrX-types-found strat) types-found)
(when own
(push strat (phpinspect-autoloader-local-strategies al)))))
(cl-defmethod phpinspect-files-compute-list ((strat phpinspect-files))
(phpinspect-files-list strat))
(defun phpinspect-php-file-extension-p (file-name)
(equal (file-name-extension file-name) "php"))
(defun phpinspect--file-expand-wildcards (file-name)
"Like `file-expand-wildcards', but returns single element list if
FILE-NAME does not contain any wildcards, instead of nil."
(or (file-expand-wildcards file-name t) (list file-name)))
(cl-defmethod phpinspect-files-compute-list ((strat phpinspect-classmap))
"Generate file list for classmap directories."
(let* ((fs (phpinspect-autoloader-fs (phpinspect-files-autoloader strat)))
(directories
(thread-last (phpinspect-files-list strat)
;; Handle wildcards
;; (https://getcomposer.org/doc/04-schema.md#classmap)
(mapcar #'phpinspect--file-expand-wildcards)
(apply #'append)))
files)
(dolist (dir directories)
(pcase dir
((pred (phpinspect-fs-file-directory-p fs))
(setq files
(nconc files (phpinspect-fs-directory-files-recursively fs dir "\\.php$"))))
((pred phpinspect-php-file-extension-p)
(push dir files))
(_ (phpinspect-message
"Unexpected file in classmap directive: %s (not a directory nor a PHP file)" dir))))
files))
(cl-defmethod phpinspect-al-strategy-execute ((strat phpinspect-files))
(let* ((list (phpinspect-files-compute-list strat))
(al (phpinspect-files-autoloader strat))
(types (phpinspect-autoloader-types al))
(own-types (phpinspect-autoloader-own-types al))
(own (phpinspect-files-own strat))
(indexer (phpinspect-autoloader-file-indexer al))
(wrapped-indexer (lambda (file)
(condition-case-unless-debug err
(let ((result (funcall indexer file)))
(dolist (index result)
(when (phpinspect-typedef-p index)
;; Make autoloader aware of indexed types.
(let ((name (phpinspect--type-name (phpi-typedef-name index))))
(puthash name file types)
(when own
(puthash name file own-types))
(phpinspect-autoloader-put-type-bag al name)))))
(t (phpinspect--log "Error indexing file %s: %s" file err))))))
(phpinspect--log "indexing files list: %s" list)
(phpinspect-pipeline list
:into (funcall :with-context wrapped-indexer))))
(cl-defmethod phpinspect-autoloader-put-type-bag ((al phpinspect-autoloader) (type-fqn (head phpinspect-name)))
(let* ((base-name (phpinspect-name-base type-fqn))
(bag (gethash base-name (phpinspect-autoloader-type-name-fqn-bags al))))
(when-let ((pr (phpinspect-autoloader--progress-reporter al))
(i (cl-incf (phpinspect-autoloader--type-counter al)))
((= 0 (mod i phpinspect-autoload-progress-interval))))
(progress-reporter-update pr i (format "%d types found" i)))
(if bag
(setcdr bag (cons type-fqn (cdr bag)))
(push type-fqn bag)
(puthash base-name bag (phpinspect-autoloader-type-name-fqn-bags al)))))
(defun phpinspect-autoloader-get-type-names (al)
(hash-table-keys (phpinspect-autoloader-type-name-fqn-bags al)))
(cl-defmethod phpinspect-autoloader-get-type-bag ((al phpinspect-autoloader) (type-name (head phpinspect-name)))
(gethash type-name (phpinspect-autoloader-type-name-fqn-bags al)))
(cl-defmethod phpinspect-iterate-composer-jsons
((al phpinspect-autoloader) file)
(let* ((fs (phpinspect-autoloader-fs al))
(project-root (file-name-directory (cdr file)))
json batch)
(condition-case err
(setq json (phpinspect--read-json-file fs (cdr file)))
(t (phpinspect-message "Error parsing composer json at %s : %s " (cdr file) err)))
(when json
(dolist (autoload (list (gethash "autoload" json)
(when (eq 'local (car file)) (gethash "autoload-dev" json))))
(when (hash-table-p autoload)
(maphash
(lambda (type prefixes)
(let ((strategy))
(pcase type
("psr-0"
(maphash
(lambda (_prefix directory-paths)
(when (stringp directory-paths)
(setq directory-paths (list directory-paths)))
(setq strategy (phpinspect-make-psr0-generated
:autoloader al
:fs fs
:own (eq 'local (car file))))
(dolist (path directory-paths)
(push (file-name-concat project-root path)
(phpinspect-psr0-directories strategy)))
(push strategy batch))
prefixes))
("psr-4"
(maphash
(lambda (prefix directory-paths)
(when (stringp directory-paths)
(setq directory-paths (list directory-paths)))
(setq strategy (phpinspect-make-psr4-generated
:fs fs
:autoloader al
:prefix prefix
:own (eq 'local (car file))))
(dolist (path directory-paths)
(push (file-name-concat project-root path)
(phpinspect-psr4-directories strategy)))
(push strategy batch))
prefixes))
("files"
(push (phpinspect-make-files
:list (mapcar
(lambda (file) (file-name-concat project-root file))
prefixes)
:autoloader al
:own (eq 'local (car file)))
batch))
("classmap"
(when phpinspect-autoload-classmaps
(push (phpinspect-make-classmap
:list (mapcar
(lambda (dir) (file-name-concat project-root dir))
prefixes)
:autoloader al
:own (eq 'local (car file)))
batch)))
(_ (phpinspect--log "Unsupported autoload strategy \"%s\" encountered" type)))))
autoload)))
(phpinspect--log "Number of autoload strategies in batch: %s" (length batch))
(phpinspect-pipeline-emit-all batch))))
(cl-defmethod phpinspect-autoloader-resolve ((autoloader phpinspect-autoloader)
(type-name (head phpinspect-name)))
;; Wait for pending refresh if not running in main thread.
(unless (eq main-thread (current-thread))
(when (and (phpinspect-autoloader-refresh-thread autoloader)
(thread-live-p (phpinspect-autoloader-refresh-thread autoloader)))
(phpinspect--log
"Pausing thread %s to await autoload refresh completion"
(thread-name (current-thread)))
(thread-join (phpinspect-autoloader-refresh-thread autoloader))
(phpinspect--log "Autoload refresh completed, continuing waiting thread %s"
(thread-name (current-thread)))))
(or (gethash type-name (phpinspect-autoloader-own-types autoloader))
(gethash type-name (phpinspect-autoloader-types autoloader))))
(defun phpinspect-autoloader-refreshing-p (autoloader)
(and-let* ((thread (phpinspect-autoloader-refresh-thread autoloader))
((thread-live-p thread)))))
(cl-defmethod phpinspect-autoloader-refresh ((autoloader phpinspect-autoloader) &optional async-callback report-progress)
"Refresh autoload definitions by reading composer.json files
from the project and vendor folders."
(if (phpinspect-autoloader-refreshing-p autoloader)
(if async-callback
(funcall async-callback nil '(error "Autoloader already refreshing"))
(phpinspect-message "Autoloader already refreshing"))
(let* ((project-root (funcall (phpinspect-autoloader-project-root-resolver autoloader)))
(fs (phpinspect-autoloader-fs autoloader)))
(setf (phpinspect-autoloader-type-name-fqn-bags autoloader)
(make-hash-table :test 'eq :size 3000 :rehash-size 3000))
(setf (phpinspect-autoloader-own-types autoloader)
(make-hash-table :test 'eq :size 10000 :rehash-size 10000))
(setf (phpinspect-autoloader-types autoloader)
(make-hash-table :test 'eq :size 10000 :rehash-size 10000))
(when report-progress
(setf (phpinspect-autoloader--type-counter autoloader) 0
(phpinspect-autoloader--progress-reporter autoloader)
(make-progress-reporter
(format "[phpinspect] indexing %s" (file-name-base project-root)))))
(let ((time-start (current-time)))
(setf (phpinspect-autoloader-refresh-thread autoloader)
(phpinspect-pipeline (phpinspect-find-composer-json-files fs project-root)
:async (lambda (result error)
(when report-progress
(progress-reporter-done (phpinspect-autoloader--progress-reporter autoloader))
(setf (phpinspect-autoloader--progress-reporter autoloader) nil))
(funcall
(or async-callback
(lambda (_result error)
(if error
(phpinspect-message "Error during autoloader refresh: %s" error)
(phpinspect-message
(concat "Refreshed project autoloader. Found %d types within project,"
" %d types total. (finished in %d ms)")
(hash-table-count (phpinspect-autoloader-own-types autoloader))
(hash-table-count (phpinspect-autoloader-types autoloader))
(string-to-number (format-time-string "%s%3N" (time-since time-start)))))))
result error))
:into (phpinspect-iterate-composer-jsons :with-context autoloader)
:into phpinspect-al-strategy-execute))))))
(provide 'phpinspect-autoload)
;;; phpinspect-autoload.el ends here