diff options
Diffstat (limited to '.emacs.d/lisp/php-project.el')
| -rw-r--r-- | .emacs.d/lisp/php-project.el | 143 |
1 files changed, 143 insertions, 0 deletions
diff --git a/.emacs.d/lisp/php-project.el b/.emacs.d/lisp/php-project.el new file mode 100644 index 0000000..cb04341 --- /dev/null +++ b/.emacs.d/lisp/php-project.el | |||
| @@ -0,0 +1,143 @@ | |||
| 1 | ;;; php-project.el --- Project support for PHP application -*- lexical-binding: t; -*- | ||
| 2 | |||
| 3 | ;; Copyright (C) 2018 Friends of Emacs-PHP development | ||
| 4 | |||
| 5 | ;; Author: USAMI Kenta <tadsan@zonu.me> | ||
| 6 | ;; Keywords: tools, files | ||
| 7 | ;; URL: https://github.com/ejmr/php-mode | ||
| 8 | ;; Version: 1.19.0 | ||
| 9 | ;; Package-Requires: ((emacs "24") (cl-lib "0.5")) | ||
| 10 | ;; License: GPL-3.0-or-later | ||
| 11 | |||
| 12 | ;; This program is free software; you can redistribute it and/or modify | ||
| 13 | ;; it under the terms of the GNU General Public License as published by | ||
| 14 | ;; the Free Software Foundation, either version 3 of the License, or | ||
| 15 | ;; (at your option) any later version. | ||
| 16 | |||
| 17 | ;; This program is distributed in the hope that it will be useful, | ||
| 18 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 20 | ;; GNU General Public License for more details. | ||
| 21 | |||
| 22 | ;; You should have received a copy of the GNU General Public License | ||
| 23 | ;; along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
| 24 | |||
| 25 | ;;; Commentary: | ||
| 26 | |||
| 27 | ;; Define project specific functions and variables for PHP application. | ||
| 28 | ;; | ||
| 29 | ;; ## API | ||
| 30 | ;; | ||
| 31 | ;; ### `php-project-get-root-dir()' | ||
| 32 | ;; | ||
| 33 | ;; Return root directory of current buffer file. The root directory is | ||
| 34 | ;; determined by several marker file or directory. | ||
| 35 | ;; | ||
| 36 | ;; ### `php-project-get-bootstrap-scripts()' | ||
| 37 | ;; | ||
| 38 | ;; Return list of path to bootstrap script file. | ||
| 39 | ;; | ||
| 40 | ;; ## `.dir-locals.el' support | ||
| 41 | ;; | ||
| 42 | ;; - `php-project-coding-style' | ||
| 43 | ;; - Symbol value of the coding style. (ex. `pear', `psr2') | ||
| 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"))) | ||
| 50 | ;; | ||
| 51 | |||
| 52 | ;;; Code: | ||
| 53 | (require 'cl-lib) | ||
| 54 | |||
| 55 | ;; Constants | ||
| 56 | (defconst php-project-composer-autoloader "vendor/autoload.php") | ||
| 57 | |||
| 58 | ;; Variables | ||
| 59 | (defvar php-project-available-root-files | ||
| 60 | '((projectile ".projectile") | ||
| 61 | (composer "composer.json" "composer.lock") | ||
| 62 | (git ".git") | ||
| 63 | (mercurial ".hg") | ||
| 64 | (subversion ".svn") | ||
| 65 | ;; NOTICE: This method does not detect the top level of .editorconfig | ||
| 66 | ;; However, we can integrate it by adding the editorconfig.el's API. | ||
| 67 | ;;(editorconfig . ".editorconfig") | ||
| 68 | )) | ||
| 69 | |||
| 70 | ;; Buffer local variables | ||
| 71 | |||
| 72 | ;;;###autoload | ||
| 73 | (progn | ||
| 74 | (defvar php-project-root 'auto | ||
| 75 | "Method of searching for the top level directory. | ||
| 76 | |||
| 77 | `auto' (default) | ||
| 78 | Try to search file in order of `php-project-available-root-files'. | ||
| 79 | |||
| 80 | SYMBOL | ||
| 81 | Key of `php-project-available-root-files'.") | ||
| 82 | (make-variable-buffer-local 'php-project-root) | ||
| 83 | (put 'php-project-root 'safe-local-variable | ||
| 84 | #'(lambda (v) (assq v php-project-available-root-files)))) | ||
| 85 | |||
| 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 | |||
| 96 | ;;;###autoload | ||
| 97 | (progn | ||
| 98 | (defvar php-project-coding-style nil | ||
| 99 | "Symbol value of the coding style of the project that PHP major mode refers to. | ||
| 100 | |||
| 101 | Typically it is `pear', `drupal', `wordpress', `symfony2' and `psr2'.") | ||
| 102 | (make-variable-buffer-local 'php-project-coding-style) | ||
| 103 | (put 'php-project-coding-style 'safe-local-variable #'symbolp)) | ||
| 104 | |||
| 105 | |||
| 106 | ;; Functions | ||
| 107 | |||
| 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 | |||
| 129 | ;;;###autoload | ||
| 130 | (defun php-project-get-root-dir () | ||
| 131 | "Return path to current PHP project." | ||
| 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)))))) | ||
| 139 | (cl-loop for m in detect-method | ||
| 140 | thereis (locate-dominating-file default-directory m)))) | ||
| 141 | |||
| 142 | (provide 'php-project) | ||
| 143 | ;;; php-project.el ends here | ||
