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.

302 lines
11 KiB

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