Personal emacs config
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

301 lines
11 KiB

5 years ago
  1. ;;; php-project.el --- Project support for PHP application -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2020 Friends of Emacs-PHP development
  3. ;; Author: USAMI Kenta <tadsan@zonu.me>
  4. ;; Keywords: tools, files
  5. ;; URL: https://github.com/emacs-php/php-mode
  6. ;; Version: 1.24.0
  7. ;; License: GPL-3.0-or-later
  8. ;; This program is free software; you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; This program is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; Define project specific functions and variables for PHP application.
  20. ;;
  21. ;; ## API
  22. ;;
  23. ;; ### `php-project-get-root-dir()'
  24. ;;
  25. ;; Return root directory of current buffer file. The root directory is
  26. ;; determined by several marker file or directory.
  27. ;;
  28. ;; ### `php-project-get-bootstrap-scripts()'
  29. ;;
  30. ;; Return list of path to bootstrap script file.
  31. ;;
  32. ;; ### `php-project-get-php-executable()'
  33. ;;
  34. ;; Return path to PHP executable file with the project settings overriding.
  35. ;;
  36. ;; ### `php-project-get-phan-executable()'
  37. ;;
  38. ;; Return path to Phan executable file with the project settings overriding.
  39. ;; Phan is a static analyzer and LSP server implementation for PHP.
  40. ;; See https://github.com/phan/phan
  41. ;;
  42. ;; ## `.dir-locals.el' support
  43. ;;
  44. ;; - `php-project-coding-style'
  45. ;; - Symbol value of the coding style. (ex. `pear', `psr2')
  46. ;; - `php-project-root'
  47. ;; - Symbol of marker file of project root. (ex. `git', `composer')
  48. ;; - Full path to project root directory. (ex. "/path/to/your-project")
  49. ;; - `php-project-bootstrap-scripts'
  50. ;; - List of path to bootstrap file of project.
  51. ;; (ex. (((root . "vendor/autoload.php") (root . "inc/bootstrap.php")))
  52. ;; - `php-project-php-executable'
  53. ;; - Path to project specific PHP executable file.
  54. ;; - If you want to use a file different from the system wide `php' command.
  55. ;; - `php-project-phan-executable'
  56. ;; - Path to project specific Phan executable file.
  57. ;; - When not specified explicitly, it is automatically searched from
  58. ;; Composer's dependency of the project and `exec-path'.
  59. ;;
  60. ;;; Code:
  61. (require 'cl-lib)
  62. (require 'projectile nil t)
  63. ;; Constants
  64. (defconst php-project-composer-autoloader "vendor/autoload.php")
  65. ;; Custom variables
  66. (defgroup php-project nil
  67. "Major mode for editing PHP code."
  68. :tag "PHP Project"
  69. :prefix "php-project-"
  70. :group 'php)
  71. (defcustom php-project-auto-detect-etags-file nil
  72. "If `T', automatically detect etags file when file is opened."
  73. :tag "PHP Project Auto Detect Etags File"
  74. :group 'php-project
  75. :type 'boolean)
  76. (defcustom php-project-use-projectile-to-detect-root nil
  77. "If `T' and projectile-mode is activated, use Projectile for root detection."
  78. :tag "PHP Project Use Projectile To Detect Root"
  79. :group 'php-project
  80. :type 'boolean)
  81. ;; Variables
  82. (defvar php-project-available-root-files
  83. '((projectile ".projectile")
  84. (composer "composer.json" "composer.lock")
  85. (git ".git")
  86. (mercurial ".hg")
  87. (subversion ".svn")
  88. ;; NOTICE: This method does not detect the top level of .editorconfig
  89. ;; However, we can integrate it by adding the editorconfig.el's API.
  90. ;;(editorconfig . ".editorconfig")
  91. ))
  92. ;; Buffer local variables
  93. ;;;###autoload
  94. (progn
  95. (defvar-local php-project-root 'auto
  96. "Method of searching for the top level directory.
  97. `auto' (default)
  98. Try to search file in order of `php-project-available-root-files'.
  99. SYMBOL
  100. Key of `php-project-available-root-files'.
  101. STRING
  102. A file/directory name of top level marker.
  103. If the string is an actual directory path, it is set as the absolute path
  104. of the root directory, not the marker.")
  105. (put 'php-project-root 'safe-local-variable
  106. #'(lambda (v) (or (stringp v) (assq v php-project-available-root-files))))
  107. (defvar-local php-project-etags-file nil)
  108. (put 'php-project-etags-file 'safe-local-variable
  109. #'(lambda (v) (or (functionp v)
  110. (eq v t)
  111. (php-project--eval-bootstrap-scripts v))))
  112. (defvar-local php-project-bootstrap-scripts nil
  113. "List of path to bootstrap php script file.
  114. The ideal bootstrap file is silent, it only includes dependent files,
  115. defines constants, and sets the class loaders.")
  116. (put 'php-project-bootstrap-scripts 'safe-local-variable #'php-project--eval-bootstrap-scripts)
  117. (defvar-local php-project-php-executable nil
  118. "Path to php executable file.")
  119. (put 'php-project-php-executable 'safe-local-variable
  120. #'(lambda (v) (and (stringp v) (file-executable-p v))))
  121. (defvar-local php-project-phan-executable nil
  122. "Path to phan executable file.")
  123. (put 'php-project-phan-executable 'safe-local-variable #'php-project--eval-bootstrap-scripts)
  124. (defvar-local php-project-coding-style nil
  125. "Symbol value of the coding style of the project that PHP major mode refers to.
  126. Typically it is `pear', `drupal', `wordpress', `symfony2' and `psr2'.")
  127. (put 'php-project-coding-style 'safe-local-variable #'symbolp)
  128. (defvar-local php-project-align-lines t
  129. "If T, automatically turn on `php-align-mode' by `php-align-setup'.")
  130. (put 'php-project-align-lines 'safe-local-variable #'booleanp)
  131. (defvar-local php-project-php-file-as-template 'auto
  132. "
  133. `auto' (default)
  134. Automatically switch to mode for template when HTML tag detected in file.
  135. `t'
  136. Switch all PHP files in that directory to mode for HTML template.
  137. `nil'
  138. Any .php in that directory is just a PHP script.
  139. \(\(PATTERN . SYMBOL))
  140. Alist of file name pattern regular expressions and the above symbol pairs.
  141. PATTERN is regexp pattern.
  142. ")
  143. (put 'php-project-php-file-as-template 'safe-local-variable #'php-project--validate-php-file-as-template)
  144. (defvar-local php-project-repl nil
  145. "Function name or path to REPL (interactive shell) script.")
  146. (put 'php-project-repl 'safe-local-variable
  147. #'(lambda (v) (or (functionp v)
  148. (php-project--eval-bootstrap-scripts v))))
  149. (defvar-local php-project-unit-test nil
  150. "Function name or path to unit test script.")
  151. (put 'php-project-unit-test 'safe-local-variable
  152. #'(lambda (v) (or (functionp v)
  153. (php-project--eval-bootstrap-scripts v))))
  154. (defvar-local php-project-deploy nil
  155. "Function name or path to deploy script.")
  156. (put 'php-project-deploy 'safe-local-variable
  157. #'(lambda (v) (or (functionp v)
  158. (php-project--eval-bootstrap-scripts v))))
  159. (defvar-local php-project-build nil
  160. "Function name or path to build script.")
  161. (put 'php-project-build 'safe-local-variable
  162. #'(lambda (v) (or (functionp v)
  163. (php-project--eval-bootstrap-scripts v))))
  164. (defvar-local php-project-server-start nil
  165. "Function name or path to server-start script.")
  166. (put 'php-project-server-start 'safe-local-variable
  167. #'(lambda (v) (or (functionp v)
  168. (php-project--eval-bootstrap-scripts v)))))
  169. ;; Functions
  170. (defun php-project--validate-php-file-as-template (val)
  171. "Return T when `VAL' is valid list of safe ."
  172. (cond
  173. ((null val) t)
  174. ((memq val '(t auto)) t)
  175. ((listp val)
  176. (cl-loop for v in val
  177. always (and (consp v)
  178. (stringp (car v))
  179. (php-project--validate-php-file-as-template (cdr v)))))
  180. (t nil)))
  181. (defun php-project--eval-bootstrap-scripts (val)
  182. "Return T when `VAL' is valid list of safe bootstrap php script."
  183. (cond
  184. ((stringp val) (and (file-exists-p val) val))
  185. ((eq 'composer val)
  186. (let ((path (expand-file-name php-project-composer-autoloader (php-project-get-root-dir))))
  187. (and (file-exists-p path) path)))
  188. ((and (consp val) (eq 'root (car val)) (stringp (cdr val)))
  189. (let ((path (expand-file-name (cdr val) (php-project-get-root-dir))))
  190. (and (file-exists-p path) path)))
  191. ((null val) nil)
  192. ((listp val)
  193. (cl-loop for v in val collect (php-project--eval-bootstrap-scripts v)))
  194. (t nil)))
  195. (defun php-project-get-php-executable ()
  196. "Return path to PHP executable file."
  197. (cond
  198. ((and (stringp php-project-php-executable)
  199. (file-executable-p php-project-php-executable))
  200. php-project-php-executable)
  201. ((boundp 'php-executable) php-executable)
  202. (t (executable-find "php"))))
  203. (defun php-project-get-phan-executable ()
  204. "Return path to phan executable file."
  205. (or (car-safe (php-project--eval-bootstrap-scripts
  206. (list php-project-phan-executable
  207. (cons 'root "vendor/bin/phan"))))
  208. (executable-find "phan")))
  209. (defun php-project-get-file-html-template-type (filename)
  210. "Return symbol T, NIL or `auto' by `FILENAME'."
  211. (cond
  212. ((not php-project-php-file-as-template) nil)
  213. ((eq t php-project-php-file-as-template) t)
  214. ((eq 'auto php-project-php-file-as-template) 'auto)
  215. ((listp php-project-php-file-as-template)
  216. (assoc-default filename php-project-php-file-as-template #'string-match-p))
  217. (t (prog1 nil
  218. (warn "php-project-php-file-as-template is unexpected format")))))
  219. (defun php-project-apply-local-variables ()
  220. "Apply php-project variables to local variables."
  221. (when (null tags-file-name)
  222. (when (or (and php-project-auto-detect-etags-file
  223. (null php-project-etags-file))
  224. (eq php-project-etags-file t))
  225. (let ((tags-file (expand-file-name "TAGS" (php-project-get-root-dir))))
  226. (when (file-exists-p tags-file)
  227. (setq-local php-project-etags-file tags-file))))
  228. (when php-project-etags-file
  229. (setq-local tags-file-name (php-project--eval-bootstrap-scripts php-project-etags-file)))))
  230. ;;;###autoload
  231. (defun php-project-get-bootstrap-scripts ()
  232. "Return list of bootstrap script."
  233. (let ((scripts (php-project--eval-bootstrap-scripts php-project-bootstrap-scripts)))
  234. (if (stringp scripts) (list scripts) scripts)))
  235. ;;;###autoload
  236. (defun php-project-get-root-dir ()
  237. "Return path to current PHP project."
  238. (if (and (stringp php-project-root) (file-directory-p php-project-root))
  239. php-project-root
  240. (php-project--detect-root-dir)))
  241. (defun php-project--detect-root-dir ()
  242. "Return detected project root."
  243. (if (and php-project-use-projectile-to-detect-root
  244. (bound-and-true-p projectile-mode)
  245. (fboundp 'projectile-project-root))
  246. (projectile-project-root default-directory)
  247. (let ((detect-method
  248. (cond
  249. ((stringp php-project-root) (list php-project-root))
  250. ((eq php-project-root 'auto)
  251. (cl-loop for m in php-project-available-root-files
  252. append (cdr m)))
  253. (t (cdr-safe (assq php-project-root php-project-available-root-files))))))
  254. (cl-loop for m in detect-method
  255. thereis (locate-dominating-file default-directory m)))))
  256. (provide 'php-project)
  257. ;;; php-project.el ends here