Skip to content

Commit 7177251

Browse files
authored
Merge pull request #433 from zonuexe/feature/project-variables
Add php-project-get-bootstrap-scripts functions
2 parents cbd5cf9 + ec1aa7f commit 7177251

File tree

1 file changed

+51
-7
lines changed

1 file changed

+51
-7
lines changed

php-project.el

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,28 @@
3333
;; Return root directory of current buffer file. The root directory is
3434
;; determined by several marker file or directory.
3535
;;
36+
;; ### `php-project-get-bootstrap-scripts()'
37+
;;
38+
;; Return list of path to bootstrap script file.
39+
;;
3640
;; ## `.dir-locals.el' support
3741
;;
3842
;; - `php-project-coding-style'
3943
;; - Symbol value of the coding style. (ex. `pear', `psr2')
40-
;;
44+
;; - `php-project-root'
45+
;; - Symbol of marker file of project root. (ex. `git', `composer')
46+
;; - Full path to project root directory. (ex. "/path/to/your-project")
47+
;; - `php-project-bootstrap-scripts'
48+
;; - List of path to bootstrap file of project.
49+
;; (ex. (((root . "vendor/autoload.php") (root . "inc/bootstrap.php")))
4150
;;
4251

4352
;;; Code:
4453
(require 'cl-lib)
4554

55+
;; Constants
56+
(defconst php-project-composer-autoloader "vendor/autoload.php")
57+
4658
;; Variables
4759
(defvar php-project-available-root-files
4860
'((projectile ".projectile")
@@ -71,6 +83,16 @@ SYMBOL
7183
(put 'php-project-root 'safe-local-variable
7284
#'(lambda (v) (assq v php-project-available-root-files))))
7385

86+
;;;###autoload
87+
(progn
88+
(defvar php-project-bootstrap-scripts nil
89+
"List of path to bootstrap php script file.
90+
91+
The ideal bootstrap file is silent, it only includes dependent files,
92+
defines constants, and sets the class loaders.")
93+
(make-variable-buffer-local 'php-project-bootstrap-scripts)
94+
(put 'php-project-bootstrap-scripts 'safe-local-variable #'php-project--eval-bootstrap-scripts))
95+
7496
;;;###autoload
7597
(progn
7698
(defvar php-project-coding-style nil
@@ -83,15 +105,37 @@ Typically it is `pear', `drupal', `wordpress', `symfony2' and `psr2'.")
83105

84106
;; Functions
85107

108+
(defun php-project--eval-bootstrap-scripts (val)
109+
"Return T when `VAL' is valid list of safe bootstrap php script."
110+
(cond
111+
((stringp val) (and (file-exists-p val) val))
112+
((eq 'composer val)
113+
(let ((path (expand-file-name php-project-composer-autoloader (php-project-get-root-dir))))
114+
(and (file-exists-p path) path)))
115+
((and (consp val) (eq 'root (car val)) (stringp (cdr val)))
116+
(let ((path (expand-file-name (cdr val) (php-project-get-root-dir))))
117+
(and (file-exists-p path) path)))
118+
((null val) nil)
119+
((listp val)
120+
(cl-loop for v in val collect (php-project--eval-bootstrap-scripts v)))
121+
(t nil)))
122+
123+
;;;###autoload
124+
(defun php-project-get-bootstrap-scripts ()
125+
"Return list of bootstrap script."
126+
(let ((scripts (php-project--eval-bootstrap-scripts php-project-bootstrap-scripts)))
127+
(if (stringp scripts) (list scripts) scripts)))
128+
86129
;;;###autoload
87130
(defun php-project-get-root-dir ()
88131
"Return path to current PHP project."
89-
(let ((detect-method (if (stringp php-project-root)
90-
(list php-project-root)
91-
(if (eq php-project-root 'auto)
92-
(cl-loop for m in php-project-available-root-files
93-
append (cdr m))
94-
(cdr-safe (assq php-project-root php-project-available-root-files))))))
132+
(let ((detect-method
133+
(cond
134+
((stringp php-project-root) (list php-project-root))
135+
((eq php-project-root 'auto)
136+
(cl-loop for m in php-project-available-root-files
137+
append (cdr m)))
138+
(t (cdr-safe (assq php-project-root php-project-available-root-files))))))
95139
(cl-loop for m in detect-method
96140
thereis (locate-dominating-file default-directory m))))
97141

0 commit comments

Comments
 (0)