├── .ert-runner ├── .github ├── dependabot.yml └── workflows │ └── test.yml ├── .gitignore ├── .travis.yml ├── CHANGELOG.md ├── Cask ├── Eask ├── GPL ├── Makefile ├── README.org ├── TODO.org ├── doc ├── how-to-create-a-new-class-loader.org └── how-to-extend-composer-autoloads.org ├── ede-php-autoload-composer.el ├── ede-php-autoload-mode.el ├── ede-php-autoload-semanticdb.el ├── ede-php-autoload.el ├── ede-php-autoload ├── class-loader.el └── class-loader │ ├── aggregate.el │ ├── classmap.el │ ├── core.el │ ├── psr0.el │ └── psr4.el ├── features ├── ede-php-autoload-completion.feature ├── ede-php-autoload-composer.feature ├── ede-php-autoload.feature ├── step-definitions │ └── ede-php-autoload-steps.el └── support │ └── env.el └── test ├── ede-php-autoload-composer-test.el ├── projects ├── with-composer │ ├── composer.json │ ├── composer.lock │ ├── default-composer.json │ ├── new-composer.json │ ├── src │ │ ├── AutoloadDev │ │ │ └── TestClass.php │ │ ├── Fallback │ │ │ ├── Psr0 │ │ │ │ └── Psr0Fallback │ │ │ │ │ └── MyClass.php │ │ │ └── Psr4 │ │ │ │ └── Psr4Fallback │ │ │ │ └── MyClass.php │ │ ├── MultiDirNs1 │ │ │ └── TheClass1.php │ │ ├── MultiDirNs2 │ │ │ └── TheClass2.php │ │ ├── NewNs │ │ │ └── MyClass.php │ │ ├── Psr0Ns │ │ │ └── TheClass.php │ │ └── Psr4Ns │ │ │ └── TheClass.php │ └── vendor │ │ ├── target-dir │ │ └── target-dir │ │ │ └── TargetDir │ │ │ └── Component │ │ │ ├── TheClass.php │ │ │ └── composer.json │ │ └── third-party │ │ ├── dev-dependency │ │ ├── composer.json │ │ └── src │ │ │ └── TestClass.php │ │ └── third-party │ │ ├── composer.json │ │ └── src │ │ └── ThirdClass.php └── without-composer │ ├── project │ └── src │ ├── ClassMapNs │ └── MyClass.php │ ├── Fallback │ ├── Psr0 │ │ └── Psr0Fallback │ │ │ └── MyClass.php │ └── Psr4 │ │ └── Psr4Fallback │ │ └── MyClass.php │ ├── MultiDirNs1 │ └── TheClass1.php │ ├── MultiDirNs2 │ └── TheClass2.php │ ├── Psr0Ns │ ├── TheClass.php │ └── TheSubdir │ │ ├── TheClass1.php │ │ └── TheClass2.php │ ├── Psr0Split │ ├── Ns1 │ │ └── TheClass.php │ └── Ns2 │ │ └── TheClass.php │ ├── Psr4Ns │ ├── TheClass.php │ └── TheSubdir │ │ ├── TheClass1.php │ │ └── TheClass2.php │ ├── Psr4Split │ ├── Ns1 │ │ └── TheClass.php │ └── Ns2 │ │ └── TheClass.php │ └── main.php └── test-helper.el /.ert-runner: -------------------------------------------------------------------------------- 1 | -l test/test-helper.el 2 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: github-actions 4 | directory: / 5 | schedule: 6 | interval: daily 7 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | pull_request: 8 | workflow_dispatch: 9 | 10 | concurrency: 11 | group: ${{ github.workflow }}-${{ github.ref }} 12 | cancel-in-progress: true 13 | 14 | jobs: 15 | test: 16 | runs-on: ${{ matrix.os }} 17 | continue-on-error: ${{ matrix.experimental }} 18 | strategy: 19 | fail-fast: false 20 | matrix: 21 | os: [ubuntu-latest, macos-latest, windows-latest] 22 | emacs-version: 23 | - 26.3 24 | - 27.2 25 | - 28.2 26 | - 29.3 27 | experimental: [false] 28 | include: 29 | - os: ubuntu-latest 30 | emacs-version: snapshot 31 | experimental: true 32 | - os: macos-latest 33 | emacs-version: snapshot 34 | experimental: true 35 | - os: windows-latest 36 | emacs-version: snapshot 37 | experimental: true 38 | 39 | steps: 40 | - uses: actions/checkout@v4 41 | 42 | - uses: jcs090218/setup-emacs@master 43 | with: 44 | version: ${{ matrix.emacs-version }} 45 | 46 | - uses: emacs-eask/setup-eask@master 47 | with: 48 | version: 'snapshot' 49 | 50 | - name: Run tests 51 | run: | 52 | eask package 53 | eask install 54 | eask compile 55 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | *.elc 3 | *.info 4 | *.texi 5 | .cask 6 | .eask 7 | dist 8 | 9 | ede-php-autoload-autoloads.el -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | # Config file copied from https://github.com/flycheck/emacs-travis 2 | language: emacs-lisp 3 | env: 4 | - EMACS_VERSION=emacs-24.3-travis 5 | - EMACS_VERSION=emacs-24.5-travis 6 | - EMACS_VERSION=emacs-25.3-travis 7 | - EMACS_VERSION=emacs-26.1-travis 8 | - EMACS_VERSION=emacs-git-snapshot-travis 9 | before_install: 10 | - export PATH="/home/travis/.evm/bin:/home/travis/.cask/bin:$PATH" 11 | - git clone https://github.com/rejeep/evm.git /home/travis/.evm 12 | - evm config path /tmp 13 | - evm install $EMACS_VERSION --use || true 14 | - curl -fsSL https://raw.githubusercontent.com/cask/cask/master/go | python 15 | install: 16 | # Install your dependencies 17 | - cask install 18 | script: 19 | # Run your tests 20 | - make test 21 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Next 2 | 3 | Features: 4 | - Automatically remove autoloads leading to non-existing dir 5 | 6 | Fixes: 7 | 8 | # 1.1.0 9 | 10 | Features: 11 | 12 | - Add the command `ede-php-autoload-reload-autoloads` to refresh the 13 | composer autoloads when the composer configuration changed 14 | 15 | Fixes: 16 | 17 | - Always load the root project when visiting a third-party source 18 | - Improve ecukes tests reliability 19 | -------------------------------------------------------------------------------- /Cask: -------------------------------------------------------------------------------- 1 | (source melpa) 2 | 3 | (package "ede-php-autoload" "1.1.0" "PHP autoloading implementation for Semantic") 4 | 5 | (files "*.el" "ede-php-autoload") 6 | 7 | (development 8 | (depends-on "ecukes") 9 | (depends-on "ert-runner") 10 | (depends-on "f")) 11 | -------------------------------------------------------------------------------- /Eask: -------------------------------------------------------------------------------- 1 | ;; -*- mode: eask; lexical-binding: t -*- 2 | 3 | (package "ede-php-autoload" 4 | "1.1.0" 5 | "PHP autoloading implementation for Semantic") 6 | 7 | (website-url "https://github.com/emacs-php/ede-php-autoload") 8 | (keywords "PHP" "project" "ede") 9 | 10 | (package-file "ede-php-autoload.el") 11 | (files 12 | "*.el" 13 | "ede-php-autoload") 14 | 15 | (script "test" "echo \"Error: no test specified\" && exit 1") 16 | 17 | (source 'melpa) 18 | 19 | (depends-on "emacs" "26.1") 20 | 21 | (development 22 | (depends-on "ecukes") 23 | (depends-on "ert-runner") 24 | (depends-on "f") 25 | ) 26 | 27 | (setq network-security-level 'low) ; see https://github.com/jcs090218/setup-emacs-windows/issues/156#issuecomment-932956432 28 | -------------------------------------------------------------------------------- /GPL: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | CASK=cask 2 | 3 | .PHONY: test test-simple elc package clean 4 | 5 | test-unit: 6 | $(CASK) exec ert-runner 7 | 8 | test-functional: 9 | $(CASK) exec ecukes 10 | 11 | test-all: 12 | $(MAKE) test-unit 13 | $(MAKE) test-functional 14 | 15 | test: 16 | $(MAKE) clean 17 | $(MAKE) test-all 18 | $(MAKE) elc 19 | $(MAKE) test-all 20 | 21 | elc: 22 | $(CASK) build 23 | 24 | clean: 25 | $(CASK) clean-elc 26 | 27 | package: 28 | $(CASK) package 29 | -------------------------------------------------------------------------------- /README.org: -------------------------------------------------------------------------------- 1 | #+TITLE: ede-php-autoload 2 | 3 | [[http://melpa.org/#/ede-php-autoload][file:http://melpa.org/packages/ede-php-autoload-badge.svg]] [[http://stable.melpa.org/#/ede-php-autoload][file:http://stable.melpa.org/packages/ede-php-autoload-badge.svg]] [[https://travis-ci.org/emacs-php/ede-php-autoload][file:https://travis-ci.org/emacs-php/ede-php-autoload.svg]] 4 | 5 | * Description 6 | 7 | This project simulates PHP autoloading system to use it in 8 | Semantic. It is composed of 3 parts: 9 | 10 | - An *EDE project*, =ede-php-autoload=, that defines autoloading 11 | configuration for a project. It is able to parse =composer.json= 12 | files to extract autoloading information from it 13 | - A *SemanticDB backend* that can find a tag by name using the 14 | autoload information of the current =ede-php-autoload= project 15 | - A *minor mode*, =ede-php-autoload-mode=, that enables the 16 | SemanticDB backend for a buffer. 17 | 18 | * Defining autoloads for a project 19 | 20 | Let's assume we have a project located at =/home/me/my-project=. It 21 | has 3 namespaces: 22 | 23 | - =MyFirstNs=, located at =src/MyFirstNs=, and uses the PSR-0 norm 24 | - =MySecondNs=, located at =src/MySecondNs=, and uses the PSR-4 norm 25 | - =ThirdPartyNs=, located at =vendor/third-party/src=, and uses the PSR-4 norm 26 | 27 | Defining this project and its autoload information is done like this: 28 | 29 | #+BEGIN_SRC emacs-lisp 30 | (ede-php-autoload-project "My project" 31 | :file "/home/me/my-project/main.php" 32 | :class-autoloads '(:psr-0 (("MyFirstNs" . "src/MyFirstNs")) 33 | :psr-4 (("MySecondNs" . "src/MySecondNs") 34 | ("ThirdPartyNs" . "vendor/third-party/src")))) 35 | #+END_SRC 36 | 37 | If you have a =composer.json= at the root of your project, its 38 | autoload information (and also the one in the composer dependencies) 39 | will be merged with the information you put in =:class-autoloads=. 40 | 41 | If your =composer.json= contains all the autoload information, and 42 | you have nothing to add in =:class-autoloads=, you don't have to 43 | define an EDE project by hand. It will be automatically created when 44 | you visit a file in your project. 45 | 46 | * Enabling the SemanticDB backend in php buffers 47 | 48 | #+BEGIN_SRC emacs-lisp 49 | (add-hook 'php-mode-hook #'ede-php-autoload-mode) 50 | #+END_SRC 51 | 52 | * Commands 53 | 54 | | Name | Description | 55 | |-----------------------------------------+----------------------------------------------------------------| 56 | | ~M-x ede-php-autoload-reload-autoloads~ | Similar to the reindexation in IDEs. Use it when your composer configuration changed to reload the autoloads. | 57 | 58 | * License 59 | 60 | This project is released under the GPL v3 license. See =GPL= for details. 61 | -------------------------------------------------------------------------------- /TODO.org: -------------------------------------------------------------------------------- 1 | #+TITLE: ede-php-autoload TODO List 2 | 3 | - Detect more project types 4 | - Use ~cl-lib~ 5 | -------------------------------------------------------------------------------- /doc/how-to-create-a-new-class-loader.org: -------------------------------------------------------------------------------- 1 | #+TITLE: How to create a new class loader 2 | 3 | * Examples 4 | 5 | To see class loader implementation examples, look at the standard 6 | class loaders defined in ~ede-php-autoload/class-loader~. The 7 | simplest one is ~ede-php-autoload-classmap-class-loader~. 8 | 9 | * Define the class loader 10 | 11 | A class loader is an EIEIO class. It is a subclass of 12 | ~ede-php-autoload-class-loader~. It should implement the following 13 | methods, even if it implies doing nothing in it: 14 | 15 | - ~ede-php-autoload-find-class-def-file~ that retrieves the file in 16 | which the given class is defined. 17 | 18 | - ~ede-php-autoload-get-class-name-for-file~ which returns the name 19 | of the class that should be defined in the given file. 20 | 21 | - ~ede-php-autoload-complete-type-name~ that return possible 22 | completions for the prefix of a fully qualified name. 23 | 24 | At this point, the class loader can be used by sending an instance 25 | of it to an ~ede-php-autoload-project~ like this: 26 | 27 | #+BEGIN_SRC emacs-lisp 28 | (ede-php-autoload-project "Custom project" :loaders (my-custom-class-loader "Custom loader" )) 29 | #+END_SRC 30 | 31 | * Define the factory 32 | 33 | Giving a class loader by instance to the EDE project is not really 34 | handy, and does not play nicely with other class loaders. It is 35 | necesary to define a factory to be able to use the new class loader 36 | with a user-friendly API like this: 37 | 38 | #+BEGIN_SRC emacs-lisp 39 | (ede-php-autoload-project "Custom project" 40 | :class-autoloads '(:custom )) 41 | #+END_SRC 42 | 43 | The factory can be defined using the macro ~ede-php-autoload-class-loader-define-factory~: 44 | 45 | #+BEGIN_SRC emacs-lisp 46 | (ede-php-autoload-class-loader-define-factory :custom (configuration) 47 | (my-custom-class-loader "Custom" configuration)) 48 | #+END_SRC 49 | -------------------------------------------------------------------------------- /doc/how-to-extend-composer-autoloads.org: -------------------------------------------------------------------------------- 1 | #+TITLE: How to extend composer autoloading system 2 | 3 | * Introduction 4 | 5 | It is possible to extend the way ede-php-autoload generates autoloads 6 | for a composer project. This can be used to add specific autoload 7 | system for some special frameworks. 8 | 9 | * Example 10 | 11 | The core composer autoloads are generated using this system. See for 12 | example ~ede-php-autoload-composer--merge-composer-data-autoloads~ in 13 | ~ede-php-autoload-composer.el~. It is in charge of generating 14 | autoloads for everything defined directly in the composer.json 15 | ~autoloads~ entry. 16 | 17 | * How to define your own visitor 18 | 19 | A visitor is a function that takes the project context and the current 20 | autoloads as parameters, appends its autoloads to the current ones, 21 | and returns this new autoloads definition. Here is an example of a 22 | visitor that always adds the same PSR-4 autoload with the namespace 23 | ~MyCustomNs~: 24 | 25 | #+BEGIN_SRC emacs-lisp 26 | (defun add-my-class-autoloads (context autoloads) 27 | (ede-php-autoload-composer-merge-autoloads 28 | autoloads 29 | `(:psr-4 (("MyCustomNs" . ,(concat 30 | (ede-php-autoload-composer-get-project-dir context) 31 | "src/MyCustomNs")))))) 32 | 33 | (ede-php-autoload-composer-define-visitor #'add-my-class-autoloads) 34 | #+END_SRC 35 | -------------------------------------------------------------------------------- /ede-php-autoload-composer.el: -------------------------------------------------------------------------------- 1 | ;;; ede-php-autoload-composer.el --- Composer projects detection and analysis -*- lexical-binding: t -*- 2 | 3 | ;; Copyright (C) 2015, 2017, Steven Rémot 4 | 5 | ;; Author: Steven Rémot 6 | ;; Keywords: PHP project ede 7 | ;; Homepage: https://github.com/emacs-php/ede-php-autoload 8 | 9 | ;; This file is not part of GNU Emacs. 10 | 11 | ;; This program is free software; you can redistribute it and/or 12 | ;; modify it under the terms of the GNU General Public License as 13 | ;; published by the Free Software Foundation; either version 2, or (at 14 | ;; your option) any later version. 15 | 16 | ;; This program is distributed in the hope that it will be useful, but 17 | ;; WITHOUT ANY WARRANTY; without even the implied warranty of 18 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | ;; General Public License for more details. 20 | 21 | ;; You should have received a copy of the GNU General Public License 22 | ;; along with this program; see the file COPYING. If not, write to 23 | ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 24 | ;; Boston, MA 02111-1307, USA. 25 | 26 | 27 | ;;; Commentary: 28 | ;; 29 | 30 | (require 'json) 31 | 32 | ;;; Code: 33 | 34 | ;;;###autoload 35 | (defconst ede-php-autoload-composer-file "composer.json" 36 | "File name for composer configuration.") 37 | 38 | (defconst ede-php-autoload-composer-lock-file "composer.lock" 39 | "File name for the composer dependecies lock file.") 40 | 41 | (defvar ede-php-autoload-composer--visitors nil 42 | "The visitors that will be executed to generate composer autoloads.") 43 | 44 | (defun ede-php-autoload--format-composer-single-dir (namespace path base-dir standard) 45 | "Format a composer autoload pair when the path is a single string. 46 | 47 | NAMESPACE is the autoloaded namespace. 48 | 49 | PATH is a string representing the relative directory. 50 | 51 | BASE-DIR is the directory PATH is relative to. 52 | 53 | STANDARD is the autoload standard (e.g. `psr-0')." 54 | 55 | (when base-dir 56 | (setq path (concat (file-name-as-directory base-dir) path))) 57 | 58 | (when (string= "psr-0" standard) 59 | (setq path (concat path (replace-regexp-in-string (rx "\\") "/" namespace) "/"))) 60 | 61 | (cons namespace path)) 62 | 63 | (defun ede-php-autoload--format-composer-multiple-dirs 64 | (namespace paths base-dir standard) 65 | "Format a composer autoload pair when the path is a single string. 66 | 67 | NAMESPACE is the autoloaded namespace. 68 | 69 | PATHS is a vector of strings representing the relative directories. 70 | 71 | BASE-DIR is the directory PATHS are relative to. 72 | 73 | STANDARD is the autoload standard (e.g. `psr-0')." 74 | (let ((list-paths (append paths '()))) 75 | 76 | (when base-dir 77 | (mapc #'(lambda (path) 78 | (concat (file-name-as-directory base-dir) path)) 79 | list-paths)) 80 | 81 | (when (string= "psr-0" standard) 82 | (mapc #'(lambda (path) 83 | (concat path namespace "/")) 84 | list-paths)) 85 | 86 | (cons namespace list-paths))) 87 | 88 | (defun ede-php-autoload--format-composer-pair (pair base-dir standard) 89 | "Format composer autoload pair to `ede-php-autoload' format. 90 | 91 | Remove the last character of composer autoload PAIR's namespace. 92 | 93 | Ex: \"Foo\\\" => \"Foo\" 94 | 95 | Composer needs it to perform its matches, but we do not need 96 | it. 97 | 98 | Then, if BASE-DIR is a string, prepend it to the autoload path. 99 | 100 | STANDARD is either \"psr-0\" or \"psr-4\". If STANDARD is 101 | \"psr-0\", append namespace name to the path like composer does." 102 | (let* ((namespace (symbol-name (car pair))) 103 | 104 | (last-character (if (string= namespace "") 105 | nil 106 | (aref namespace (1- (length namespace))))) 107 | 108 | (path (cdr pair))) 109 | 110 | (when (member last-character '(?\\ ?_)) 111 | (setq namespace (substring-no-properties namespace 0 (1- (length namespace))))) 112 | 113 | (if (stringp path) 114 | (ede-php-autoload--format-composer-single-dir namespace path base-dir standard) 115 | (ede-php-autoload--format-composer-multiple-dirs namespace path base-dir standard)))) 116 | 117 | (defun ede-php-autoload-composer-create-autoloads-from-data (composer-data &optional base-dir) 118 | "Return internal autoloads from a composer.json file data. 119 | 120 | COMPOSER-DATA is the parsed content of a composer.json file. 121 | BASE-DIR is the prefix dir to add to each autoload path." 122 | (let ((composer-autoloads (append (cdr (assoc 'autoload composer-data)) 123 | (cdr (assoc 'autoload-dev composer-data)))) 124 | (autoloads '()) 125 | key spec) 126 | 127 | (dolist (autoload-part composer-autoloads) 128 | (when (member (car autoload-part) '(psr-0 psr-4)) 129 | (setq key (intern (concat ":" (symbol-name (car autoload-part)))) 130 | 131 | spec (mapcar #'(lambda (pair) 132 | (ede-php-autoload--format-composer-pair pair base-dir (car autoload-part))) 133 | (cdr autoload-part)) 134 | 135 | autoloads (plist-put autoloads 136 | key 137 | (append (plist-get autoloads key) spec))))) 138 | 139 | autoloads)) 140 | 141 | (defun ede-php-autoload-composer--merge-autoload-paths (base-paths new-paths) 142 | "Merge two paths in a autoload file in one. 143 | 144 | BASE-PATHS and NEW-PATHS are either string opr list of strings. 145 | 146 | It will output a list of strings with each of base and new paths in it." 147 | (let ((list-base-path (if (stringp base-paths) (list base-paths) base-paths)) 148 | (list-new-paths (if (stringp new-paths) (list new-paths) new-paths))) 149 | (append list-base-path list-new-paths))) 150 | 151 | (defun ede-php-autoload-composer--merge-autoload-entries (base-entries new-entries) 152 | "Merge two autoload entries (right after the autoload entry). 153 | 154 | BASE-ENTRIES and NEW-ENTRIES are the entries to merge." 155 | (let ((current-entries base-entries) 156 | pair 157 | pair-path) 158 | (cl-loop for (ns . paths) in new-entries do 159 | (setq pair (assoc ns current-entries) 160 | pair-path (cdr pair)) 161 | (if pair 162 | (setf (cdr pair) (ede-php-autoload-composer--merge-autoload-paths pair-path paths)) 163 | (setq current-entries (push (cons ns paths) current-entries)))) 164 | current-entries)) 165 | 166 | (defun ede-php-autoload-composer-merge-autoloads (base-autoloads new-autoloads) 167 | "Merge two internal autoload definitions in one. 168 | 169 | BASE-AUTOLOADS and NEW-AUTOLOADS are two internal autoload lists. 170 | 171 | NEW-AUTOLOADS will be merged into BASE-AUTOLOADS. BASE-AUTOLOADS will be mutated." 172 | (let ((autoloads base-autoloads) 173 | (index 0) 174 | (new-autoloads-length (length new-autoloads)) 175 | key value) 176 | 177 | (while (< index new-autoloads-length) 178 | (setq key (nth index new-autoloads) 179 | 180 | value (nth (1+ index) new-autoloads) 181 | 182 | autoloads (plist-put autoloads 183 | key 184 | (ede-php-autoload-composer--merge-autoload-entries 185 | (plist-get autoloads key) 186 | value)) 187 | 188 | index (+ index 2))) 189 | 190 | autoloads)) 191 | 192 | (defun ede-php-autoload-composer-merge-composer-autoloads (composer-data autoloads &optional base-dir) 193 | "Load the autoload information in COMPOSER-DATA and merge it with AUTOLOADS. 194 | 195 | COMPOSER-DATA is the parsed composer.json file. 196 | BASE-DIR is the prefix dir to add to each autoload path." 197 | (ede-php-autoload-composer-merge-autoloads autoloads (ede-php-autoload-composer-create-autoloads-from-data composer-data base-dir))) 198 | 199 | (defun ede-php-autoload-composer--get-data (dir) 200 | "Return the parsed composer.json file in DIR if any. 201 | 202 | Return nil otherwise." 203 | (let ((composer-file (expand-file-name ede-php-autoload-composer-file dir))) 204 | (when (file-exists-p composer-file) 205 | (json-read-file composer-file)))) 206 | 207 | (defun ede-php-autoload-composer--get-third-party-data (composer-lock) 208 | "Return the composer packages in composer.lock file. 209 | 210 | COMPOSER-LOCK is the content of the composer.lock file." 211 | (if composer-lock 212 | (vconcat (cdr (assoc 'packages composer-lock)) 213 | (cdr (assoc 'packages-dev composer-lock))) 214 | [])) 215 | 216 | (defun ede-php-autoload-composer--get-third-party-dir (package-data vendor-dir) 217 | "Return the directory that contain third party sources. 218 | 219 | PACKAGE-DATA is the data for the corresponding third-party in the 220 | composer.lock file. 221 | 222 | VENDOR-DIR is the project's vendor directory." 223 | (expand-file-name (cdr (assoc 'name package-data)) vendor-dir)) 224 | 225 | ;; Visitor system ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 226 | 227 | (defun ede-php-autoload-composer-make-context (composer-data composer-lock project-dir) 228 | "Create the context in which the composer project is defined. 229 | 230 | COMPOSER-DATA is the parsed content of the composer.json file. 231 | 232 | COMPOSER-LOCK is the parsed content of the composer.lock file. 233 | 234 | PROJECT-DIR is the absolute path of the project directory." 235 | `((composer-data . ,composer-data) 236 | (composer-lock . ,composer-lock) 237 | (project-dir . ,project-dir))) 238 | 239 | (defun ede-php-autoload-composer-get-composer-data (context) 240 | "Return the content of the composer.json file in the given CONTEXT." 241 | (cdr (assoc 'composer-data context))) 242 | 243 | (defun ede-php-autoload-composer-get-composer-lock (context) 244 | "Return the content of the composer.lock file in the given CONTEXT." 245 | (cdr (assoc 'composer-lock context))) 246 | 247 | (defun ede-php-autoload-composer-get-project-dir (context) 248 | "Return the absolute path to the project directory in the given CONTEXT." 249 | (cdr (assoc 'project-dir context))) 250 | 251 | (defun ede-php-autoload-composer-define-visitor (visitor &optional step) 252 | "Add a new VISITOR to the list of composer visitors. 253 | 254 | A visitor is a function that takes a context and the current list 255 | of autoloads, and returns a new list of autoloads. 256 | 257 | All visitors are executed when a composer project is detected, to 258 | generate the composer autoloads. 259 | 260 | STEP is the autoload construction step at which the visitor 261 | should execute. It can be `:early', `:normal' or `:late. It 262 | defaults to `:normal'.'" 263 | (let* ((real-step (or step :normal)) 264 | (pair (assoc real-step ede-php-autoload-composer--visitors))) 265 | (if pair 266 | (setf (cdr pair) (push visitor (cdr pair))) 267 | (add-to-list 'ede-php-autoload-composer--visitors (cons real-step (list visitor)))))) 268 | 269 | (defun ede-php-autoload-composer--run-visitors (visitors context autoloads) 270 | "Run all the visitors on a specified CONTEXT, with the initial AUTOLOADS. 271 | 272 | Returns the new list of autoloads." 273 | (let ((current-autoloads autoloads) 274 | step-visitors) 275 | 276 | (dolist (step '(:early :normal :late)) 277 | (setq step-visitors (cdr (assoc step visitors))) 278 | (dolist (visitor step-visitors) 279 | (setq current-autoloads (funcall visitor context current-autoloads)))) 280 | 281 | current-autoloads)) 282 | 283 | ;; Basic visitors ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 284 | 285 | (defun ede-php-autoload-composer--merge-composer-data-autoloads (context autoloads) 286 | "Load the autoload information in composer.json and merge it with autoloads. 287 | 288 | CONTEXT is the composer context. 289 | AUTOLOADS is the current list of autoloads." 290 | (ede-php-autoload-composer-merge-composer-autoloads (ede-php-autoload-composer-get-composer-data context) autoloads nil)) 291 | 292 | (ede-php-autoload-composer-define-visitor #'ede-php-autoload-composer--merge-composer-data-autoloads) 293 | 294 | (defun ede-php-autoload-composer--merge-composer-lock-autoloads (context autoloads) 295 | "Load the autoload information from lock file and merge it with autoloads. 296 | 297 | CONTEXT is the composer context. 298 | AUTOLOADS is the current list of autoloads." 299 | (let* ((project-dir (ede-php-autoload-composer-get-project-dir context)) 300 | (composer-lock (ede-php-autoload-composer-get-composer-lock context)) 301 | (third-party-data (ede-php-autoload-composer--get-third-party-data composer-lock)) 302 | (vendor-dir (expand-file-name "vendor" project-dir)) 303 | (i 0) 304 | (l (length third-party-data)) 305 | current-data) 306 | (while (< i l) 307 | (setq current-data (aref third-party-data i) 308 | autoloads (ede-php-autoload-composer-merge-composer-autoloads 309 | current-data 310 | autoloads 311 | (ede-php-autoload-composer--get-third-party-dir current-data vendor-dir)) 312 | i (1+ i))) 313 | autoloads)) 314 | 315 | (ede-php-autoload-composer-define-visitor #'ede-php-autoload-composer--merge-composer-lock-autoloads) 316 | 317 | ;; Entry point ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 318 | 319 | (defun ede-php-autoload--append-composer-autoload-data (project-dir autoloads) 320 | "Add all composer autoload information. 321 | 322 | If PROJECT-DIR has a composer specification, add its autoload 323 | information into AUTOLOADS." 324 | (let* ((composer-data (ede-php-autoload-composer--get-data project-dir)) 325 | (lock-file (expand-file-name ede-php-autoload-composer-lock-file project-dir)) 326 | (composer-lock (when (file-exists-p lock-file) (json-read-file lock-file))) 327 | (context (ede-php-autoload-composer-make-context composer-data composer-lock project-dir))) 328 | (ede-php-autoload-composer--run-visitors ede-php-autoload-composer--visitors context autoloads))) 329 | 330 | 331 | (provide 'ede-php-autoload-composer) 332 | 333 | ;;; ede-php-autoload-composer.el ends here 334 | -------------------------------------------------------------------------------- /ede-php-autoload-mode.el: -------------------------------------------------------------------------------- 1 | ;;; ede-php-autoload-mode.el --- Minor mode for activating ede-php-autoload tag loading 2 | 3 | ;; Copyright (C) 2015, Steven Rémot 4 | 5 | ;; Author: Steven Rémot 6 | ;; original code for C++ by Eric M. Ludlam 7 | ;; Keywords: PHP project ede 8 | ;; Homepage: https://github.com/emacs-php/ede-php-autoload 9 | 10 | ;; This file is not part of GNU Emacs. 11 | 12 | ;; This program is free software; you can redistribute it and/or 13 | ;; modify it under the terms of the GNU General Public License as 14 | ;; published by the Free Software Foundation; either version 2, or (at 15 | ;; your option) any later version. 16 | 17 | ;; This program is distributed in the hope that it will be useful, but 18 | ;; WITHOUT ANY WARRANTY; without even the implied warranty of 19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 20 | ;; 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; see the file COPYING. If not, write to 24 | ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 25 | ;; Boston, MA 02111-1307, USA. 26 | 27 | ;;; Commentary: 28 | ;; 29 | ;;; Code: 30 | 31 | (require 'semantic/db) 32 | (require 'ede-php-autoload-semanticdb) 33 | 34 | 35 | (defgroup ede-php-autoload nil "" 36 | :group 'tools) 37 | 38 | ;;;###autoload 39 | (define-minor-mode ede-php-autoload-mode 40 | "ede-php-autoload // Enable PHP tag loading using `ede-php-autoload-project'. 41 | 42 | \\{ede-php-autoload-mode-map}" 43 | :group ede-php-autoload 44 | 45 | (cond (ede-php-autoload-mode 46 | (unless (listp semanticdb-project-system-databases) 47 | (setq semanticdb-project-system-databases '())) 48 | (add-to-list 'semanticdb-project-system-databases (ede-php-autoload-semanticdb-database "EDE PHP ROOT"))) 49 | 50 | ;; On mode disable, remove ede-php-autoload database 51 | (t (let (new-databases '()) 52 | (dolist (database semanticdb-project-system-databases) 53 | (unless (ede-php-autoload-semanticdb-database-p database) 54 | (push database new-databases))) 55 | (setq semanticdb-project-system-databases new-databases))))) 56 | 57 | (provide 'ede-php-autoload-mode) 58 | 59 | ;;; ede-php-autoload-mode.el ends here 60 | -------------------------------------------------------------------------------- /ede-php-autoload-semanticdb.el: -------------------------------------------------------------------------------- 1 | ;;; ede-php-autoload-semanticdb.el --- Semanticdb support for ede-php-autoload 2 | 3 | ;; Copyright (C) 2015, Steven Rémot 4 | 5 | ;; Author: Steven Rémot 6 | ;; Inspired by Joris Stein's edep 7 | ;; Keywords: PHP project ede 8 | ;; Homepage: https://github.com/emacs-php/ede-php-autoload 9 | 10 | ;; This file is not part of GNU Emacs. 11 | 12 | ;; This program is free software; you can redistribute it and/or 13 | ;; modify it under the terms of the GNU General Public License as 14 | ;; published by the Free Software Foundation; either version 2, or (at 15 | ;; your option) any later version. 16 | 17 | ;; This program is distributed in the hope that it will be useful, but 18 | ;; WITHOUT ANY WARRANTY; without even the implied warranty of 19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 20 | ;; 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; see the file COPYING. If not, write to 24 | ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 25 | ;; Boston, MA 02111-1307, USA. 26 | 27 | ;;; Commentary: 28 | 29 | ;; This provides a simple semanticdb backend that uses ede-php-autoload to 30 | ;; get tags by emulating PHP autoload system. 31 | 32 | ;;; Code: 33 | 34 | (require 'semantic/db) 35 | (require 'ede-php-autoload) 36 | 37 | (eval-and-compile 38 | (unless (fboundp 'cl-defmethod) 39 | (defalias 'cl-defmethod 'defmethod)) 40 | (unless (fboundp 'cl-call-next-method) 41 | (defalias 'cl-call-next-method 'call-next-method))) 42 | 43 | (defclass ede-php-autoload-semanticdb-table (semanticdb-search-results-table eieio-singleton) 44 | ((major-mode :initform php-mode)) 45 | "Database table for PHP using `ede-php-autoload'.") 46 | 47 | (defclass ede-php-autoload-semanticdb-database (semanticdb-project-database eieio-singleton) 48 | ((new-table-class :initform ede-php-autoload-semanticdb-table 49 | :type class 50 | :documentation "Class of the new tables created for this database.")) 51 | "Semanticdb database that uses `ede-php-autoload'.") 52 | 53 | (cl-defmethod semanticdb-get-database-tables ((obj ede-php-autoload-semanticdb-database)) 54 | "For an `ede-php-autoload-project', there is only one singleton table." 55 | (when (or (not (slot-boundp obj 'tables)) 56 | (not (ede-php-autoload-semanticdb-table-p (car (oref obj tables))))) 57 | (let ((newtable (ede-php-autoload-semanticdb-table "EDE-PHP-AUTOLOAD"))) 58 | (oset obj tables (list newtable)) 59 | (oset newtable parent-db obj))) 60 | (cl-call-next-method)) 61 | 62 | (cl-defmethod semanticdb-file-table ((obj ede-php-autoload-semanticdb-database) filename) 63 | "For an `ede-php-autoload-project', use the only table." 64 | (car (semanticdb-get-database-tables obj))) 65 | 66 | (defun ede-php-autoload-semanticdb-import-file-content-for-class (project class-name) 67 | "Import the tags in the file that defines a certain class. 68 | 69 | PROJECT is the ede php project in which class is defined. 70 | CLASS-NAME is the name of the class. 71 | 72 | Return nil if it could not find the file or if the file was the current file." 73 | (let ((file (ede-php-autoload-find-class-def-file project class-name))) 74 | (when (and file (not (string= file (buffer-file-name)))) 75 | (find-file-noselect file) 76 | (semanticdb-file-stream file)))) 77 | 78 | (defun ede-php-autoload-current-project () 79 | "Return the current `ede-php-autoload' project." 80 | (when (ede-php-autoload-project-p (ede-current-project)) 81 | (ede-current-project))) 82 | 83 | (cl-defmethod semanticdb-find-tags-by-name-method 84 | ((table ede-php-autoload-semanticdb-table) name &optional tags) 85 | "Find all tags named NAME in TABLE" 86 | (if (ede-php-autoload-current-project) 87 | (or (ede-php-autoload-semanticdb-import-file-content-for-class 88 | (ede-php-autoload-current-project) 89 | name) 90 | (cl-call-next-method)) 91 | (cl-call-next-method))) 92 | 93 | (cl-defmethod semanticdb-deep-find-tags-by-name-method 94 | ((table ede-php-autoload-semanticdb-table) name &optional tags) 95 | "Find all tags name NAME in TABLE. 96 | Optional argument TAGS is a list of tags to search. 97 | Like `semanticdb-find-tags-by-name-method' for global." 98 | (semanticdb-find-tags-by-name-method table name tags)) 99 | 100 | (defun ede-php-autoload-semanticdb--wrap-suggestion-in-tag (suggestion) 101 | "Wrap the type completion SUGGESTION in a type tag." 102 | (semantic-tag-new-type suggestion 'unknown '() '())) 103 | 104 | (cl-defmethod semanticdb-find-tags-for-completion-method 105 | ((table ede-php-autoload-semanticdb-table) prefix &optional tags) 106 | "In TABLE, find all occurrences of tags matching PREFIX. 107 | Optional argument TAGS is a list of tags to search. 108 | Returns a table of all matching tags." 109 | (if (ede-php-autoload-current-project) 110 | (mapcar #'ede-php-autoload-semanticdb--wrap-suggestion-in-tag 111 | (ede-php-autoload-complete-type-name (ede-php-autoload-current-project) prefix)))) 112 | 113 | (provide 'ede-php-autoload-semanticdb) 114 | 115 | ;;; ede-php-autoload-semanticdb.el ends here 116 | -------------------------------------------------------------------------------- /ede-php-autoload.el: -------------------------------------------------------------------------------- 1 | ;;; ede-php-autoload.el --- Simple EDE PHP Project 2 | 3 | ;; Copyright (C) 2014, 2015, 2016, Steven Rémot 4 | 5 | ;; Author: Steven Rémot 6 | ;; original code for C++ by Eric M. Ludlam 7 | ;; Version: 1.1.0 8 | ;; Keywords: PHP project ede 9 | ;; Homepage: https://github.com/emacs-php/ede-php-autoload 10 | 11 | ;; This file is not part of GNU Emacs. 12 | 13 | ;; This program is free software; you can redistribute it and/or 14 | ;; modify it under the terms of the GNU General Public License as 15 | ;; published by the Free Software Foundation; either version 2, or (at 16 | ;; your option) any later version. 17 | 18 | ;; This program is distributed in the hope that it will be useful, but 19 | ;; WITHOUT ANY WARRANTY; without even the implied warranty of 20 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 21 | ;; General Public License for more details. 22 | 23 | ;; You should have received a copy of the GNU General Public License 24 | ;; along with this program; see the file COPYING. If not, write to 25 | ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 26 | ;; Boston, MA 02111-1307, USA. 27 | 28 | ;;; Commentary: 29 | ;; 30 | ;; PHP EDE project that supports class autoloading and composer.json detection. 31 | ;; 32 | ;; Example project definition : 33 | ;; (ede-php-autoload-project "My project" 34 | ;; :file "/path/to/a/file/at/root" 35 | ;; :class-autoloads '(:psr-0 (("MyNs" . "src/MyNs") 36 | ;; ("AnotherNs" . "src/AnotherNs")) 37 | ;; :psr-4 (("MyModernNs" . "src/modern/MyNs")))) 38 | ;; 39 | ;; This EDE project can then be used through a semanticdb 40 | ;; backend. Enable it by activating `ede-php-autoload-mode'. 41 | ;; 42 | 43 | (require 'ede) 44 | 45 | (require 'ede-php-autoload-composer) 46 | (require 'ede-php-autoload/class-loader) 47 | 48 | ;;; Code: 49 | 50 | (defvar ede-php-autoload-project-list nil 51 | "List of projects created by option `ede-php-autoload-project'.") 52 | 53 | (defun ede-php-autoload-file-existing (dir) 54 | "Find a php-autoload project in the list of php-autoload projects. 55 | DIR is the directory to search from." 56 | (let ((projs ede-php-autoload-project-list) 57 | (ans nil)) 58 | (while (and projs (not ans)) 59 | (let ((root (ede-project-root-directory (car projs)))) 60 | (when (string-match (concat "^" (regexp-quote root)) dir) 61 | (setq ans (car projs)))) 62 | (setq projs (cdr projs))) 63 | ans)) 64 | 65 | (defun ede-php-autoload-project-file-for-dir (&optional dir) 66 | "Return a full file name to the project file stored in DIR." 67 | (let ((proj (ede-php-autoload-file-existing dir))) 68 | (when proj (oref proj file)))) 69 | 70 | (defun ede-php-autoload--proj-root (file) 71 | "Recursively detect project root. 72 | 73 | Return the top-most parent directory of FILE containing a composer.json file." 74 | (let* ((dominating-file (locate-dominating-file file ede-php-autoload-composer-file)) 75 | potential-proj-root) 76 | (when dominating-file 77 | (setq potential-proj-root (file-name-directory dominating-file)) 78 | (or (ede-php-autoload--proj-root (file-name-directory (directory-file-name potential-proj-root))) potential-proj-root)))) 79 | 80 | ;;;###autoload 81 | (defun ede-php-autoload-proj-root () 82 | "Auto-detect composer project root. 83 | 84 | Return the parent directory of the current buffer file that contains a composer.json file." 85 | (ede-php-autoload--proj-root (or (buffer-file-name) default-directory))) 86 | 87 | ;; Composer project detection 88 | 89 | ;;;###autoload 90 | (defun ede-php-autoload-load (dir &optional rootproj) 91 | "Return a `ede-php-autoload-project' for the provided directory. 92 | 93 | DIR is the project directory. 94 | 95 | ROOTPROJ is the parent project. The PHP autoload project is not 96 | intended to be a subproject, so this argument is ignored." 97 | (let* ((truedir (ede-php-autoload--proj-root (file-truename dir))) 98 | (name (concat "PHP Autoload: " truedir))) 99 | (ede-php-autoload-project name 100 | :name name 101 | :directory truedir 102 | :file (expand-file-name ede-php-autoload-composer-file 103 | truedir)))) 104 | 105 | ;;;###autoload 106 | (eval-after-load 'ede 107 | '(ede-add-project-autoload 108 | (ede-project-autoload "php-autoload" 109 | :name "PHP AUTOLOAD" 110 | :file 'ede-php-autoload 111 | :proj-file "composer.json" 112 | :proj-root 'ede-php-autoload-proj-root 113 | :load-type 'ede-php-autoload-load 114 | :class-sym 'ede-php-autoload-project 115 | :new-p nil 116 | :safe-p t) 117 | 'unique)) 118 | 119 | (defun ede-php-autoload-reload-autoloads () 120 | "Reload the autoloads for the current projects. 121 | 122 | This has the same goal than a reindexation in IDEs. Use this 123 | method when your composer.json file changed, or your vendor 124 | directory has been updated in order to take the new autoloads 125 | into account." 126 | (interactive) 127 | (ede-php-autoload-reload-autoloads-for-project (ede-current-project))) 128 | 129 | ;;;; 130 | ;;;; Class loaders 131 | ;;;; 132 | 133 | (defun ede-php-autoload-create-class-loader (conf) 134 | "Create a class loader from a configuration. 135 | 136 | CONF is a property list. Its keys are class norms, and its values 137 | are the mappings between namespace and include path. 138 | 139 | For example, the conf '(:psr-4 ((\"Foo\" . \"src/Foo\") (\"Bar\" 140 | \"src/test/Bar\"))) will create a class loader that will load 141 | classes written with PSR-4 normal, mapping \"Foo\" and \"Bar\" 142 | to the associated directories." 143 | (let ((loaders '()) 144 | (load-config conf)) 145 | (while load-config 146 | (let ((key (car load-config))) 147 | (add-to-list 'loaders (ede-php-autoload-class-loader-call-factory key (cadr load-config))) 148 | (setq load-config (cddr load-config)))) 149 | (ede-php-autoload-aggregate-class-loader "Aggregate loader" 150 | :class-loaders loaders))) 151 | 152 | (defun ede-php-autoload-remove-non-existing-dirs (conf root-dir) 153 | "Remove from CONF the non-existing directories. 154 | 155 | CONF is the same kind of argument than `ede-php-autoload-create-class-loader'. 156 | 157 | ROOT-DIR is the root directory of the project." 158 | (let ((cleaned-conf '()) 159 | (conf-length (length conf)) 160 | (index 0) 161 | key 162 | namespaces cleaned-namespaces 163 | namespace 164 | paths cleaned-paths) 165 | 166 | ;; key: `:psr-0', ... 167 | (while (< index conf-length) 168 | (setq key (nth index conf) 169 | namespaces (nth (1+ index) conf) 170 | cleaned-namespaces '()) 171 | 172 | ;; pair: (ns . paths) 173 | (dolist (pair namespaces) 174 | (setq namespace (car pair) 175 | paths (if (listp (cdr pair)) (cdr pair) (list (cdr pair))) 176 | cleaned-paths '()) 177 | 178 | (dolist (path paths) 179 | (when (file-exists-p (expand-file-name path root-dir)) 180 | (add-to-list 'cleaned-paths path t))) 181 | 182 | (when (> (length cleaned-paths) 0) 183 | (add-to-list 'cleaned-namespaces 184 | (cons 185 | namespace 186 | (if (= (length cleaned-paths) 1) 187 | (car cleaned-paths) 188 | cleaned-paths)) 189 | t))) 190 | 191 | (when (> (length cleaned-namespaces) 0) 192 | (setq cleaned-conf (append cleaned-conf (list key cleaned-namespaces)))) 193 | 194 | (setq index (+ index 2))) 195 | cleaned-conf)) 196 | 197 | 198 | (defclass ede-php-autoload-target (ede-target) 199 | ((project :initform nil 200 | :initarg :project)) 201 | "EDE php-autoload project target.") 202 | 203 | ;;;###autoload 204 | (defclass ede-php-autoload-project (ede-project eieio-instance-tracker) 205 | ((tracking-symbol :initform 'ede-php-autoload-project-list) 206 | (class-loader :type ede-php-autoload-class-loader 207 | :documentation "The project's class loader.") 208 | (include-path :initarg :include-path 209 | :type list 210 | :initform () 211 | :documentation "A list of PHP include paths specific to the project") 212 | (system-include-path :initarg :system-include-path 213 | :type list 214 | :initform () 215 | :documentation "The list of PHP include paths defined for the system.") 216 | (explicit-class-autoloads :initarg :explicit-class-autoloads 217 | :type list 218 | :documentation "The class autoloads explicitly defined at initialization"))) 219 | 220 | (cl-defmethod initialize-instance ((this ede-php-autoload-project) &rest fields) 221 | "Make sure the :file is fully expanded." 222 | (call-next-method this (list 223 | :file (plist-get (car fields) :file) 224 | :explicit-class-autoloads (plist-get (car fields) :class-autoloads) 225 | :include-path (plist-get (car fields) :include-path) 226 | :system-include-path (plist-get (car fields) :system-include-path))) 227 | 228 | (ede-php-autoload-reload-autoloads-for-project this) 229 | 230 | (let ((f (expand-file-name (oref this file)))) 231 | ;; Remove any previous entries from the main list. 232 | (let ((old (eieio-instance-tracker-find (file-name-directory f) 233 | :directory 234 | 'ede-php-autoload-project-list))) 235 | (when (and old (not (eq old this))) 236 | (delete-instance old))) 237 | ;; Basic initialization. 238 | (when (or (not (file-exists-p f)) 239 | (file-directory-p f)) 240 | (delete-instance this) 241 | (error ":file for ede-php-autoload-project must be a file")) 242 | (oset this :file f) 243 | (oset this :directory (file-name-directory f)) 244 | (ede-project-directory-remove-hash (file-name-directory f)) 245 | (ede-add-project-to-global-list this) 246 | (unless (slot-boundp this 'targets) 247 | (oset this :targets nil)))) 248 | 249 | (cl-defmethod ede-php-autoload-reload-autoloads-for-project ((this ede-php-autoload-project)) 250 | "Regenerate the class loaders. 251 | 252 | This can be used when some composer dependencies changed, to take 253 | the new autoloads into account." 254 | (let* ((raw-autoloads 255 | (ede-php-autoload--append-composer-autoload-data 256 | (file-name-directory (ede-project-root-directory this)) 257 | (oref this explicit-class-autoloads))) 258 | (root-dir (ede-project-root-directory this)) 259 | (cleaned-autoloads (ede-php-autoload-remove-non-existing-dirs raw-autoloads root-dir))) 260 | 261 | (oset this class-loader 262 | (ede-php-autoload-create-class-loader cleaned-autoloads)))) 263 | 264 | (cl-defmethod ede-find-subproject-for-directory ((proj ede-php-autoload-project) dir) 265 | "Return PROJ, for handling all subdirs below DIR." 266 | proj) 267 | 268 | (cl-defmethod ede-find-target ((proj ede-php-autoload-project) buffer) 269 | "Find an EDE target in PROJ for BUFFER. 270 | If one doesn't exist, create a new one for this directory." 271 | (let* ((targets (oref proj targets)) 272 | (dir default-directory) 273 | (ans (object-assoc dir :path targets))) 274 | (when (not ans) 275 | (setq ans (ede-php-autoload-target dir 276 | :name (file-name-nondirectory 277 | (directory-file-name dir)) 278 | :path dir 279 | :source nil 280 | :project proj)) 281 | (object-add-to-list proj :targets ans)) 282 | ans)) 283 | 284 | (cl-defmethod ede-project-root ((this ede-php-autoload-project)) 285 | "Return my root." 286 | this) 287 | 288 | (cl-defmethod ede-project-root-directory ((this ede-php-autoload-project)) 289 | "Return my root." 290 | (file-name-directory (oref this file))) 291 | 292 | (cl-defmethod ede-php-autoload-find-class-def-file ((this ede-php-autoload-project) class-name) 293 | "Find the file in which CLASS-NAME is defined. 294 | 295 | CLASS-NAME must be the full name of the class, with all its parent namespaces." 296 | (ede-php-autoload-find-class-def-file (oref this class-loader) class-name)) 297 | 298 | (cl-defmethod ede-php-autoload-get-class-name-for-file 299 | ((this ede-php-autoload-project) file-name) 300 | "Generate a suitable class name for the current FILE-NAME. 301 | 302 | Generate this class name using the class loader information. 303 | 304 | FILE-NAME must be absolute or relative to the project root." 305 | (ede-php-autoload-get-class-name-for-file (oref this class-loader) file-name)) 306 | 307 | (cl-defmethod ede-php-autoload-complete ((this ede-php-autoload-project) prefix) 308 | "Get completion suggestions for the type PREFIX. 309 | 310 | PREFIX is the beginning of a fully-qualified name. 311 | 312 | The result is a list of completion suggestions for this 313 | prefix." 314 | (ede-php-autoload-complete (oref this class-loader) prefix)) 315 | 316 | (cl-defmethod ede-php-autoload-complete-type-name ((this ede-php-autoload-project) prefix) 317 | "Get completion suggestions for the type PREFIX. 318 | 319 | PREFIX is the beginning of a fully-qualified name. 320 | 321 | The result is a list of completion suggestions for this 322 | prefix. Completions are not guaranteed to give full class names, 323 | this can only suggest the next namespace." 324 | (ede-php-autoload-complete-type-name (oref this class-loader) prefix)) 325 | 326 | (provide 'ede-php-autoload) 327 | 328 | ;;; ede-php-autoload.el ends here 329 | -------------------------------------------------------------------------------- /ede-php-autoload/class-loader.el: -------------------------------------------------------------------------------- 1 | ;;; class-loader.el --- Include all autoloading code 2 | 3 | ;; Copyright (C) 2016, Steven Rémot 4 | 5 | ;; This file is not part of GNU Emacs. 6 | 7 | ;; This program is free software; you can redistribute it and/or 8 | ;; modify it under the terms of the GNU General Public License as 9 | ;; published by the Free Software Foundation; either version 2, or (at 10 | ;; your option) any later version. 11 | 12 | ;; This program is distributed in the hope that it will be useful, but 13 | ;; WITHOUT ANY WARRANTY; without even the implied warranty of 14 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 | ;; General Public License for more details. 16 | 17 | ;; You should have received a copy of the GNU General Public License 18 | ;; along with this program; see the file COPYING. If not, write to 19 | ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 20 | ;; Boston, MA 02111-1307, USA. 21 | 22 | ;;; Commentary: 23 | ;; 24 | ;;; Code: 25 | (require 'ede-php-autoload/class-loader/core) 26 | (require 'ede-php-autoload/class-loader/psr4) 27 | (require 'ede-php-autoload/class-loader/psr0) 28 | (require 'ede-php-autoload/class-loader/classmap) 29 | (require 'ede-php-autoload/class-loader/aggregate) 30 | 31 | (provide 'ede-php-autoload/class-loader) 32 | 33 | ;;; class-loader.el ends here 34 | -------------------------------------------------------------------------------- /ede-php-autoload/class-loader/aggregate.el: -------------------------------------------------------------------------------- 1 | ;;; aggregate.el --- Class loader composed of several other loaders 2 | 3 | ;; Copyright (C) 2016, Steven Rémot 4 | 5 | ;; This file is not part of GNU Emacs. 6 | 7 | ;; This program is free software; you can redistribute it and/or 8 | ;; modify it under the terms of the GNU General Public License as 9 | ;; published by the Free Software Foundation; either version 2, or (at 10 | ;; your option) any later version. 11 | 12 | ;; This program is distributed in the hope that it will be useful, but 13 | ;; WITHOUT ANY WARRANTY; without even the implied warranty of 14 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 | ;; General Public License for more details. 16 | 17 | ;; You should have received a copy of the GNU General Public License 18 | ;; along with this program; see the file COPYING. If not, write to 19 | ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 20 | ;; Boston, MA 02111-1307, USA. 21 | 22 | ;;; Commentary: 23 | ;; 24 | ;;; Code: 25 | (require 'ede-php-autoload/class-loader/core) 26 | 27 | (defclass ede-php-autoload-aggregate-class-loader (ede-php-autoload-class-loader) 28 | ((class-loaders :initarg :class-loaders 29 | :initform () 30 | :documentation "The list of aggregated class loaders. 31 | 32 | They must be instances of `ede-php-autoload-class-loader'.")) 33 | "An aggregation of several class loaders.") 34 | 35 | (cl-defmethod ede-php-autoload-find-class-def-file ((this ede-php-autoload-aggregate-class-loader) 36 | class-name) 37 | "Find the file in which CLASS-NAME is defined. 38 | 39 | Return nil if no file has been found." 40 | (let ((loaders (oref this class-loaders)) 41 | (class-def-file nil)) 42 | (while (and loaders (not class-def-file)) 43 | (setq class-def-file (ede-php-autoload-find-class-def-file (car loaders) class-name) 44 | loaders (cdr loaders))) 45 | class-def-file)) 46 | 47 | (cl-defmethod ede-php-autoload-get-class-name-for-file 48 | ((this ede-php-autoload-aggregate-class-loader) file-name) 49 | "Generate a suitable class name for the current FILE-NAME. 50 | 51 | Generate this class name using the class loader information. 52 | 53 | FILE-NAME must be absolute or relative to the project root." 54 | (let ((loaders (oref this class-loaders)) 55 | class-name) 56 | (while (and loaders (not class-name)) 57 | (setq class-name (ede-php-autoload-get-class-name-for-file (car loaders) file-name) 58 | loaders (cdr loaders))) 59 | class-name)) 60 | 61 | (cl-defmethod ede-php-autoload-complete-type-name ((this ede-php-autoload-aggregate-class-loader) prefix) 62 | "Get completion suggestions for the type PREFIX. 63 | 64 | PREFIX is the beginning of a fully-qualified name. 65 | 66 | The result is a list of completion suggestions for this 67 | prefix. Completions are not guaranteed to give full class names, 68 | this can only suggest the next namespace." 69 | (let ((suggestions '())) 70 | (dolist (loader (oref this class-loaders)) 71 | (setq suggestions (append suggestions 72 | (ede-php-autoload-complete-type-name loader prefix)))) 73 | suggestions)) 74 | 75 | (provide 'ede-php-autoload/class-loader/aggregate) 76 | 77 | ;;; aggregate.el ends here 78 | -------------------------------------------------------------------------------- /ede-php-autoload/class-loader/classmap.el: -------------------------------------------------------------------------------- 1 | ;;; classmap.el --- Class map class loader implementation 2 | 3 | ;; Copyright (C) 2016, Steven Rémot 4 | 5 | ;; This file is not part of GNU Emacs. 6 | 7 | ;; This program is free software; you can redistribute it and/or 8 | ;; modify it under the terms of the GNU General Public License as 9 | ;; published by the Free Software Foundation; either version 2, or (at 10 | ;; your option) any later version. 11 | 12 | ;; This program is distributed in the hope that it will be useful, but 13 | ;; WITHOUT ANY WARRANTY; without even the implied warranty of 14 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 | ;; General Public License for more details. 16 | 17 | ;; You should have received a copy of the GNU General Public License 18 | ;; along with this program; see the file COPYING. If not, write to 19 | ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 20 | ;; Boston, MA 02111-1307, USA. 21 | 22 | ;;; Commentary: 23 | ;; 24 | ;; Class loader based on a strong mapping between class names and files. 25 | ;; 26 | ;; To use it : 27 | ;; (ede-php-autoload-project "My project" 28 | ;; :file "main.php" 29 | ;; :class-autoloads '(:class-map (("Class\\Name" . "file/name.php") 30 | ;; ("Class\\Name2" . "file/name2.php")))) 31 | ;; 32 | ;;; Code: 33 | (require 'ede) 34 | (require 'ede-php-autoload/class-loader/core) 35 | 36 | ;;;###autoload 37 | (defclass ede-php-autoload-classmap-class-loader (ede-php-autoload-class-loader) 38 | ((class-hash :initarg :classes 39 | :initform (makehash) 40 | :documentation "A hash associating a class name and an absolute path to the file in whcih the class is defined.")) 41 | "Class loader for direct association between classes and files.") 42 | 43 | (cl-defmethod ede-php-autoload-find-class-def-file ((this ede-php-autoload-classmap-class-loader) class-name) 44 | "Find the file in which CLASS-NAME is defined. 45 | 46 | Return nil if no file has been found." 47 | (let ((file (gethash class-name (oref this class-hash)))) 48 | (when file 49 | (expand-file-name file (ede-project-root-directory (ede-current-project)))))) 50 | 51 | (cl-defmethod ede-php-autoload-get-class-name-for-file ((this ede-php-autoload-classmap-class-loader) file-name) 52 | "Generate a suitable class name for the current FILE-NAME. 53 | 54 | Generate this class name using the class loader information. 55 | 56 | FILE-NAME must be absolute or relative to the project root." 57 | (let* ((project-root (ede-project-root (ede-current-project))) 58 | (abs-file-name (expand-file-name file-name project-root))) 59 | (catch 'class-name 60 | (maphash 61 | #'(lambda (class file) 62 | (when (string= (expand-file-name file project-root) abs-file-name) 63 | (throw 'class-name class))) 64 | (oref this class-hash))))) 65 | 66 | (cl-defmethod ede-php-autoload-complete-type-name ((this ede-php-autoload-classmap-class-loader) prefix) 67 | "Get completion suggestions for the type PREFIX. 68 | 69 | PREFIX is the beginning of a fully-qualified name. 70 | 71 | The result is a list of completion suggestions for this 72 | prefix." 73 | (let ((completions '())) 74 | (maphash 75 | #'(lambda (class file) 76 | (when (string-prefix-p prefix class) 77 | (add-to-list 'completions class))) 78 | (oref this class-hash)) 79 | completions)) 80 | 81 | (ede-php-autoload-class-loader-define-factory :class-map (classes) 82 | (let ((class-hash)) 83 | 84 | ;; Convert association lists to hash map 85 | (if (listp classes) 86 | (progn 87 | (setq class-hash (make-hash-table :test 'equal)) 88 | (dolist (pair classes) 89 | (puthash (format "%s" (car pair)) (cdr pair) class-hash))) 90 | (setq class-hash classes)) 91 | 92 | (ede-php-autoload-classmap-class-loader "Classmap" :classes class-hash))) 93 | 94 | (provide 'ede-php-autoload/class-loader/classmap) 95 | 96 | ;;; classmap.el ends here 97 | -------------------------------------------------------------------------------- /ede-php-autoload/class-loader/core.el: -------------------------------------------------------------------------------- 1 | ;;; core.el --- Base primitives for class loader definition 2 | 3 | ;; Copyright (C) 2016, Steven Rémot 4 | 5 | ;; This file is not part of GNU Emacs. 6 | 7 | ;; This program is free software; you can redistribute it and/or 8 | ;; modify it under the terms of the GNU General Public License as 9 | ;; published by the Free Software Foundation; either version 2, or (at 10 | ;; your option) any later version. 11 | 12 | ;; This program is distributed in the hope that it will be useful, but 13 | ;; WITHOUT ANY WARRANTY; without even the implied warranty of 14 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 | ;; General Public License for more details. 16 | 17 | ;; You should have received a copy of the GNU General Public License 18 | ;; along with this program; see the file COPYING. If not, write to 19 | ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 20 | ;; Boston, MA 02111-1307, USA. 21 | 22 | ;;; Commentary: 23 | ;; 24 | ;;; Code: 25 | (require 'eieio) 26 | 27 | (defclass ede-php-autoload-class-loader () 28 | () 29 | "Base class for finding the file in with some class is defined." 30 | :abstract t) 31 | 32 | (cl-defmethod ede-php-autoload-find-class-def-file ((this ede-php-autoload-class-loader) 33 | class-name) 34 | "Find the file in which CLASS-NAME is defined. 35 | 36 | CLASS-NAME must be the full name of the class, with all its parent namespaces." 37 | (error "Method `ede-php-autoload-find-class-def-file' must be overriden")) 38 | 39 | (cl-defmethod ede-php-autoload-get-class-name-for-file 40 | ((this ede-php-autoload-class-loader) file-name) 41 | "Generate a suitable class name for the current FILE-NAME. 42 | 43 | Generate this class name using the class loader information. 44 | 45 | FILE-NAME must be absolute or relative to the project root." 46 | (error "Method `ede-php-autoload-find-class-def-file' must be overriden")) 47 | 48 | (cl-defmethod ede-php-autoload-complete ((this ede-php-autoload-class-loader) prefix) 49 | "Get completion suggestions for the PREFIX. 50 | 51 | PREFIX is the beginning of a fully-qualified name. 52 | 53 | The result is a list of completion suggestions for this 54 | prefix." 55 | (let* ((split-prefix (split-string prefix "\\\\")) 56 | (ns (mapconcat 'identity (butlast split-prefix) "\\")) 57 | (completions (ede-php-autoload-complete-type-name this prefix))) 58 | ;; Try to detect if we got toplevel namespace returned (which can 59 | ;; contain multiple components), in which case we should not 60 | ;; prepend the base namespace. This is error prone, and just a 61 | ;; stop gap until ede-php-autoload-complete-type-name handles 62 | ;; namespace sub-completion. 63 | (if (cl-loop for completion in completions 64 | always (string-prefix-p prefix completion t)) 65 | completions 66 | (cl-loop for completion in completions 67 | collect (concat ns "\\" completion))))) 68 | 69 | (cl-defmethod ede-php-autoload-complete-type-name ((this ede-php-autoload-class-loader) prefix) 70 | "Get type completion suggestions for the type PREFIX. 71 | 72 | PREFIX is the beginning of a fully-qualified name. 73 | 74 | The result is a list of type completion suggestions for this 75 | prefix. Type completions are not guaranteed to give full class names, 76 | this can only suggest the next namespace." 77 | '()) 78 | 79 | ;;; Utility functions for loaders 80 | 81 | (defun ede-php-autoload--get-path-relative-to-ns (class-name namespace &optional extension) 82 | "Return the path of the class file relative to the namespace directory. 83 | 84 | CLASS-NAME is the class name. 85 | 86 | NAMESPACE is the namespace to map. 87 | 88 | EXTENSION is the file extension to put at the end of the file, \".php\" by default. 89 | 90 | Example: (ede-php-autoload--get-path-relative-to-ns \"My\\Ns\\My\\Class\" \"My\\Ns\") 91 | => \"My/Class.php\"" 92 | (concat 93 | (mapconcat 94 | 'identity 95 | (nthcdr (length (split-string namespace (rx (or "\\" "_")) t)) 96 | (split-string class-name (rx (or "\\" "_")) t)) 97 | "/") 98 | (or extension ".php"))) 99 | 100 | (defun ede-php-autoload--gather-relative-subfiles (ns-directories project-root relative-path prefix) 101 | "Return all relative file names in namespace subdirectories. 102 | 103 | NS-DIRECTORIES are list of directories for a namespace, relative 104 | to the PROJECT-ROOT. 105 | 106 | RELATIVE-PATH is the path to browser in each NS-DIRECTORIES. 107 | 108 | Only files starting with PREFIX will be kept. 109 | 110 | Basically, it returns PROJECT-ROOT/{NS-DIRECTORIES}/RELATIVE-PATH/{PREFIX}*" 111 | (let ((files '()) 112 | absolute-dir 113 | full-dir) 114 | (dolist (dir (ede-php-autoload--ensure-list ns-directories)) 115 | (setq absolute-dir (if (file-name-absolute-p dir) 116 | dir 117 | (expand-file-name dir project-root)) 118 | full-dir (expand-file-name relative-path absolute-dir) 119 | files (when (file-exists-p full-dir) 120 | (append files (mapcar 121 | #'(lambda (file-name) 122 | (if (file-directory-p 123 | (expand-file-name file-name full-dir)) 124 | (concat file-name "\\") 125 | file-name)) 126 | (directory-files 127 | full-dir 128 | nil 129 | (concat "^" (if (string= prefix "") 130 | "[^.]" 131 | (regexp-quote prefix))))))))) 132 | files)) 133 | 134 | (defun ede-php-autoload--ensure-list (list-or-element) 135 | "Ensure LIST-OR-ELEMENT will be wrapped in a list." 136 | (if (listp list-or-element) list-or-element (list list-or-element))) 137 | 138 | (defun ede-php-autoload--search-in-dirs (file directories root) 139 | "Search for a FILE existing in one of the given DIRECTORIES. 140 | 141 | DIRECTORIES are absolute paths or relative to ROOT." 142 | (let ((dirs (ede-php-autoload--ensure-list directories)) 143 | existing-file 144 | candidate 145 | current-dir 146 | absolute-dir) 147 | (while (and (not existing-file) dirs) 148 | (setq current-dir (car dirs) 149 | 150 | absolute-dir (if (file-name-absolute-p current-dir) 151 | current-dir 152 | (expand-file-name current-dir root)) 153 | 154 | candidate (expand-file-name file absolute-dir) 155 | dirs (cdr dirs)) 156 | 157 | (when (file-regular-p candidate) 158 | (setq existing-file candidate))) 159 | 160 | existing-file)) 161 | 162 | ;; 163 | ;; Factories 164 | ;; 165 | ;; Associate a keyword to a factory function that can create a class 166 | ;; loader from a certain configuration. 167 | ;; 168 | 169 | (defvar ede-php-autoload-class-loader--factories '() 170 | "Association list mapping a keyword symbol to a factory function.") 171 | 172 | (defun ede-php-autoload-class-loader--define-factory (name factory) 173 | "Register a new factory. 174 | 175 | NAME is the keyword symbol referring the the factory. 176 | 177 | FACTORY is a function taking as parameter a 178 | configuration (usually a list), and returns an instance of 179 | `ede-php-autoload-class-loader'." 180 | (add-to-list 'ede-php-autoload-class-loader--factories (cons name factory))) 181 | 182 | (defmacro ede-php-autoload-class-loader-define-factory (name arguments &rest body) 183 | "Register a new factory. 184 | 185 | A factory is a function taking a configuration value as argument 186 | and returning an instance of`ede-php-autoload-class-loader'. 187 | 188 | NAME is the keyword symbol referring the the factory. 189 | 190 | ARGUMENTS is the list reprlesenting the function arguments. 191 | 192 | BODY is the function implementation." 193 | (declare (debug (symbolp sexp body)) 194 | (indent defun)) 195 | `(ede-php-autoload-class-loader--define-factory ,name 196 | (lambda ,arguments ,@body))) 197 | 198 | (defun ede-php-autoload-class-loader-call-factory (name configuration) 199 | "Call a class loader factory with the specified configuration. 200 | 201 | NAME is the keyword symbol representing the factory. 202 | 203 | CONFIGURATION is the value sent to the factory. 204 | 205 | Returns an instance of `ede-php-autoload-class-loader'." 206 | (let ((factory (cdr (assoc name ede-php-autoload-class-loader--factories)))) 207 | (unless factory 208 | (error "Class loader factory %S not found" name)) 209 | (funcall factory configuration))) 210 | 211 | (provide 'ede-php-autoload/class-loader/core) 212 | 213 | ;;; core.el ends here 214 | -------------------------------------------------------------------------------- /ede-php-autoload/class-loader/psr0.el: -------------------------------------------------------------------------------- 1 | ;;; psr0.el --- PSR-0 class loader implementation 2 | 3 | ;; Copyright (C) 2016, Steven Rémot 4 | 5 | ;; This file is not part of GNU Emacs. 6 | 7 | ;; This program is free software; you can redistribute it and/or 8 | ;; modify it under the terms of the GNU General Public License as 9 | ;; published by the Free Software Foundation; either version 2, or (at 10 | ;; your option) any later version. 11 | 12 | ;; This program is distributed in the hope that it will be useful, but 13 | ;; WITHOUT ANY WARRANTY; without even the implied warranty of 14 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 | ;; General Public License for more details. 16 | 17 | ;; You should have received a copy of the GNU General Public License 18 | ;; along with this program; see the file COPYING. If not, write to 19 | ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 20 | ;; Boston, MA 02111-1307, USA. 21 | 22 | ;;; Commentary: 23 | ;; 24 | ;; Class loader for PSR-0 namespaces. 25 | ;; 26 | ;; To use it : 27 | ;; (ede-php-autoload-project "My project" 28 | ;; :file "main.php" 29 | ;; :class-autoloads '(:psr-0 (("Psr0\\Ns" . "base/directory") 30 | ;; ("Psr0\\Ns2" . "base/directory2")))) 31 | ;; 32 | ;;; Code: 33 | (require 'ede) 34 | (require 'ede-php-autoload/class-loader/core) 35 | 36 | ;;;###autoload 37 | (defclass ede-php-autoload-psr0-class-loader (ede-php-autoload-class-loader) 38 | ((namespaces :initarg :namespaces 39 | :initform () 40 | :documentation 41 | "An associative list in which keys are namespaces, and values are their include paths. 42 | 43 | For example, if :namespaces has the value '((\"Foo\" . \"src/Foo\") (\"Bar\" . \"src/test/Bar\")), 44 | then The class \"Bar_Foo\" is considered to be defined in \"src/test/Bar/Foo\". 45 | 46 | The include paths can be either a string or a list of strings.")) 47 | "Class loader for PSR-0 convention.") 48 | 49 | (cl-defmethod ede-php-autoload-find-class-def-file ((this ede-php-autoload-psr0-class-loader) 50 | class-name) 51 | "Find the file in which CLASS-NAME is defined. 52 | 53 | Return nil if no file has been found." 54 | (let* ((class-name (if (= (aref class-name 0) ?\\) (substring class-name 1) class-name)) 55 | (project-root (ede-project-root-directory (ede-current-project))) 56 | (namespaces (oref this namespaces)) 57 | class-def-file) 58 | (while (and namespaces (not class-def-file)) 59 | (let ((pair (car namespaces)) 60 | (candidate-file "")) 61 | (when (string-prefix-p (car pair) class-name) 62 | (setq class-def-file (ede-php-autoload--search-in-dirs 63 | (ede-php-autoload--get-path-relative-to-ns class-name (car pair)) 64 | (cdr pair) 65 | project-root))) 66 | (setq namespaces (cdr namespaces)))) 67 | class-def-file)) 68 | 69 | (cl-defmethod ede-php-autoload-get-class-name-for-file 70 | ((this ede-php-autoload-psr0-class-loader) file-name) 71 | "Generate a suitable class name for the current FILE-NAME. 72 | 73 | Generate this class name using the class loader information. 74 | 75 | FILE-NAME must be absolute or relative to the project root." 76 | nil) ;; Work in progress 77 | 78 | (defun ede-php-autoload--create-psr-0-suggestions (file-name prefix) 79 | "Process FILE-NAME to make it a proper PSR-0 completion. 80 | 81 | It basically tries to infer whether \"_\" or \"\\\" should be used. 82 | 83 | PREFIX is the type prefix to complete." 84 | (let ((suggestion (file-name-base file-name))) 85 | (cond 86 | ((string-match-p "\\\\" prefix) suggestion) 87 | (t (mapconcat 88 | 'identity 89 | (append (nbutlast (split-string prefix "_")) (list suggestion)) 90 | "_"))))) 91 | 92 | (defun ede-php-autoload--complete-for-psr0-pair (namespace directories project-root prefix) 93 | "Get completion suggestions for a PSR-0 loader pair. 94 | 95 | NAMESPACE is the represented namespace. 96 | 97 | DIRECTORIES is a list of directories associated to the namespace. 98 | 99 | PROJECT-ROOT is the path to the project's root. 100 | 101 | PREFIX is the beginning of the type to complete." 102 | (let ((list-directories (ede-php-autoload--ensure-list directories)) 103 | (suggestions '()) 104 | split-prefix 105 | relative-path 106 | absolute-dir 107 | separator 108 | full-dir) 109 | (cond 110 | ((string-prefix-p prefix namespace t) 111 | ;; If `prefix' is the beginning of `namespace', let's use 112 | ;; `namespace' as suggestion. 113 | (push (concat namespace "\\") suggestions)) 114 | ((string-prefix-p namespace prefix) 115 | ;; If `prefix' starts with `namespace', let's use directory and 116 | ;; file structure to create suggestions 117 | (setq separator (if (string-match-p "\\\\" prefix) "\\" "_") 118 | split-prefix (split-string prefix (regexp-quote separator)) 119 | relative-path (file-name-as-directory 120 | (ede-php-autoload--get-path-relative-to-ns 121 | (mapconcat 'identity 122 | (butlast split-prefix) 123 | separator) 124 | namespace 125 | ""))) 126 | (setq suggestions (delete-dups 127 | (delete nil 128 | (mapcar 129 | #'(lambda (file-name) 130 | (ede-php-autoload--create-psr-0-suggestions 131 | file-name 132 | prefix)) 133 | (ede-php-autoload--gather-relative-subfiles 134 | directories 135 | project-root 136 | relative-path 137 | (car (last split-prefix))))))))) 138 | suggestions)) 139 | 140 | (cl-defmethod ede-php-autoload-complete-type-name ((this ede-php-autoload-psr0-class-loader) prefix) 141 | "Get completion suggestions for the type PREFIX. 142 | 143 | PREFIX is the beginning of a fully-qualified name. 144 | 145 | The result is a list of completion suggestions for this 146 | prefix. Completions are not guaranteed to give full class names, 147 | this can only suggest the next namespace." 148 | (let ((project-root (ede-project-root-directory (ede-current-project))) 149 | (namespaces (oref this namespaces)) 150 | (suggestions '()) 151 | pair) 152 | (while namespaces 153 | (setq pair (car namespaces) 154 | suggestions (append suggestions 155 | (ede-php-autoload--complete-for-psr0-pair 156 | (car pair) 157 | (cdr pair) 158 | project-root 159 | prefix)) 160 | namespaces (cdr namespaces))) 161 | suggestions)) 162 | 163 | (ede-php-autoload-class-loader-define-factory :psr-0 (config) 164 | (ede-php-autoload-psr0-class-loader "PSR-0" :namespaces config)) 165 | 166 | (provide 'ede-php-autoload/class-loader/psr0) 167 | 168 | ;;; psr0.el ends here 169 | -------------------------------------------------------------------------------- /ede-php-autoload/class-loader/psr4.el: -------------------------------------------------------------------------------- 1 | ;;; psr4.el --- PSR-4 class loader implementation 2 | 3 | ;; Copyright (C) 2016, Steven Rémot 4 | 5 | ;; This file is not part of GNU Emacs. 6 | 7 | ;; This program is free software; you can redistribute it and/or 8 | ;; modify it under the terms of the GNU General Public License as 9 | ;; published by the Free Software Foundation; either version 2, or (at 10 | ;; your option) any later version. 11 | 12 | ;; This program is distributed in the hope that it will be useful, but 13 | ;; WITHOUT ANY WARRANTY; without even the implied warranty of 14 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 | ;; General Public License for more details. 16 | 17 | ;; You should have received a copy of the GNU General Public License 18 | ;; along with this program; see the file COPYING. If not, write to 19 | ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 20 | ;; Boston, MA 02111-1307, USA. 21 | 22 | ;;; Commentary: 23 | ;; 24 | ;; Class loader for PSR-4 namespaces. 25 | ;; 26 | ;; To use it : 27 | ;; (ede-php-autoload-project "My project" 28 | ;; :file "main.php" 29 | ;; :class-autoloads '(:psr-4 (("Psr4\\Ns" . "base/directory") 30 | ;; ("Psr4\\Ns2" . "base/directory2")))) 31 | ;; 32 | ;; 33 | ;;; Code: 34 | (require 'ede) 35 | (require 'ede-php-autoload/class-loader/core) 36 | 37 | ;;;###autoload 38 | (defclass ede-php-autoload-psr4-class-loader (ede-php-autoload-class-loader) 39 | ((namespaces :initarg :namespaces 40 | :initform () 41 | :documentation 42 | "An associative list in which keys are namespaces, and values are their include paths. 43 | 44 | For example, if :namespaces has the value '((\"Foo\" . \"src/Foo\") (\"Bar\" . \"src/test/Bar\")), 45 | then The class \"Bar\\Foo\" is considered to be defined in \"src/test/Bar/Foo\".")) 46 | "Class loader for PSR-4 convention.") 47 | 48 | (cl-defmethod ede-php-autoload-find-class-def-file ((this ede-php-autoload-psr4-class-loader) 49 | class-name) 50 | "Find the file in which CLASS-NAME is defined. 51 | 52 | Return nil if no file has been found." 53 | (let* ((project-root (ede-project-root-directory (ede-current-project))) 54 | (namespaces (oref this namespaces)) 55 | class-def-file) 56 | (while (and namespaces (not class-def-file)) 57 | (let ((pair (car namespaces))) 58 | (when (string-prefix-p (car pair) class-name) 59 | (setq class-def-file (ede-php-autoload--search-in-dirs 60 | (ede-php-autoload--get-path-relative-to-ns class-name (car pair)) 61 | (cdr pair) 62 | project-root))) 63 | (setq namespaces (cdr namespaces)))) 64 | class-def-file)) 65 | 66 | (defun ede-php-autoload--get-longest-prefix (pairs target) 67 | "Find the autoload pair which has the longest matching prefix of the target. 68 | 69 | PAIRS is an associative list. 70 | 71 | TARGET is a string." 72 | (let ((current-pairs pairs) 73 | extracted-list 74 | extracted 75 | longest-extracted 76 | longest-pair) 77 | (while current-pairs 78 | (setq extracted-list (ede-php-autoload--ensure-list 79 | (cdar current-pairs))) 80 | (while extracted-list 81 | (setq extracted (car extracted-list)) 82 | (when (and (string-prefix-p extracted target) 83 | (or (null longest-pair) 84 | (> (length extracted) (length longest-extracted)))) 85 | (setq longest-extracted extracted 86 | longest-pair (cons (caar current-pairs) extracted))) 87 | (setq extracted-list (cdr extracted-list))) 88 | (setq current-pairs (cdr current-pairs))) 89 | longest-pair)) 90 | 91 | (cl-defmethod ede-php-autoload-get-class-name-for-file 92 | ((this ede-php-autoload-psr4-class-loader) file-name) 93 | "Generate a suitable class name for the current FILE-NAME. 94 | 95 | Generate this class name using the class loader information. 96 | 97 | FILE-NAME must be absolute or relative to the project root." 98 | (let* ((project-root (ede-project-root-directory (ede-current-project))) 99 | (rel-file-name (if (file-name-absolute-p file-name) 100 | (file-relative-name file-name project-root) 101 | file-name)) 102 | (associated-ns (ede-php-autoload--get-longest-prefix (oref this namespaces) 103 | rel-file-name))) 104 | (when associated-ns 105 | (replace-regexp-in-string 106 | (rx "\\\\") (rx "\\") 107 | (mapconcat 108 | #'identity 109 | (list 110 | (car associated-ns) 111 | (replace-regexp-in-string (rx "/") 112 | (rx "\\") 113 | (substring (file-name-sans-extension file-name) 114 | (length (cdr associated-ns))))) 115 | "\\"))))) 116 | 117 | (defun ede-php-autoload--complete-for-psr4-pair (namespace directories project-root prefix) 118 | "Get completion suggestions for a PSR-4 loader pair. 119 | 120 | NAMESPACE is the represented namespace. 121 | 122 | DIRECTORIES is a list of directories associated to the namespace. 123 | 124 | PROJECT-ROOT is the path to the project's root. 125 | 126 | PREFIX is the beginning of the type to complete." 127 | (let ((list-directories (ede-php-autoload--ensure-list directories)) 128 | (suggestions '()) 129 | split-prefix 130 | relative-path 131 | absolute-dir 132 | full-dir) 133 | (cond 134 | ((string-prefix-p prefix namespace t) 135 | ;; If `prefix' is the beginning of `namespace', let's use 136 | ;; `namespace' as suggestion. 137 | (push (concat namespace "\\") suggestions)) 138 | ((string-prefix-p namespace prefix) 139 | ;; If `prefix' starts with `namespace', let's use directory and 140 | ;; file structure to create suggestions 141 | (setq split-prefix (split-string prefix "\\\\") 142 | relative-path (file-name-as-directory 143 | (ede-php-autoload--get-path-relative-to-ns 144 | (mapconcat 'identity 145 | (butlast split-prefix) 146 | "\\") 147 | namespace 148 | ""))) 149 | 150 | (setq suggestions (delete-dups 151 | (delete nil 152 | (mapcar 153 | #'file-name-base 154 | (ede-php-autoload--gather-relative-subfiles 155 | directories 156 | project-root 157 | relative-path 158 | (car (last split-prefix))))))))) 159 | suggestions)) 160 | 161 | (cl-defmethod ede-php-autoload-complete-type-name ((this ede-php-autoload-psr4-class-loader) prefix) 162 | "Get completion suggestions for the type PREFIX. 163 | 164 | PREFIX is the beginning of a fully-qualified name. 165 | 166 | The result is a list of completion suggestions for this 167 | prefix. Completions are not guaranteed to give full class names, 168 | this can only suggest the next namespace." 169 | (let ((project-root (ede-project-root-directory (ede-current-project))) 170 | (namespaces (oref this namespaces)) 171 | (suggestions '()) 172 | pair) 173 | (while namespaces 174 | (setq pair (car namespaces) 175 | suggestions (append suggestions 176 | (ede-php-autoload--complete-for-psr4-pair 177 | (car pair) 178 | (cdr pair) 179 | project-root 180 | prefix)) 181 | namespaces (cdr namespaces))) 182 | suggestions)) 183 | 184 | (ede-php-autoload-class-loader-define-factory :psr-4 (config) 185 | (ede-php-autoload-psr4-class-loader "PSR-4" :namespaces config)) 186 | 187 | (provide 'ede-php-autoload/class-loader/psr4) 188 | 189 | ;;; psr4.el ends here 190 | -------------------------------------------------------------------------------- /features/ede-php-autoload-completion.feature: -------------------------------------------------------------------------------- 1 | Feature: Class name completion 2 | 3 | Scenario: Complete PSR-0 namespaces 4 | Given I visit "src/main.php" in project "without-composer" 5 | Then type completions for query "Psr0" should be: 6 | | name | 7 | | Psr0Ns\ | 8 | | Psr0Split\Ns1\ | 9 | | Psr0Split\Ns2\ | 10 | | Psr0Fallback\ | 11 | Then completions for query "Psr0" should be: 12 | | name | 13 | | Psr0Ns\ | 14 | | Psr0Split\Ns1\ | 15 | | Psr0Split\Ns2\ | 16 | | Psr0Fallback\ | 17 | 18 | Scenario: Complete PSR-0 namespace with slashes 19 | Given I visit "src/main.php" in project "without-composer" 20 | Then type completions for query "Psr0Ns\T" should be: 21 | | name | 22 | | TheClass | 23 | | TheSubdir\ | 24 | And type completions for query "Psr0Ns\TheSubdir\" should be: 25 | | name | 26 | | TheClass1 | 27 | | TheClass2 | 28 | Then completions for query "Psr0Ns\T" should be: 29 | | name | 30 | | Psr0Ns\TheClass | 31 | | Psr0Ns\TheSubdir\ | 32 | And completions for query "Psr0Ns\TheSubdir\" should be: 33 | | name | 34 | | Psr0Ns\TheSubdir\TheClass1 | 35 | | Psr0Ns\TheSubdir\TheClass2 | 36 | 37 | Scenario: Complete PSR-0 namespace with underscores 38 | Given I visit "src/main.php" in project "without-composer" 39 | Then type completions for query "Psr0Ns_T" should be: 40 | | name | 41 | | Psr0Ns_TheClass | 42 | | Psr0Ns_TheSubdir\ | 43 | And type completions for query "Psr0Ns_TheSubdir_" should be: 44 | | name | 45 | | Psr0Ns_TheSubdir_TheClass1 | 46 | | Psr0Ns_TheSubdir_TheClass2 | 47 | Then completions for query "Psr0Ns_T" should be: 48 | | name | 49 | | Psr0Ns_TheClass | 50 | | Psr0Ns_TheSubdir\ | 51 | And completions for query "Psr0Ns_TheSubdir_" should be: 52 | | name | 53 | | Psr0Ns_TheSubdir_TheClass1 | 54 | | Psr0Ns_TheSubdir_TheClass2 | 55 | 56 | Scenario: Complete PSR-4 namespaces 57 | Given I visit "src/main.php" in project "without-composer" 58 | Then type completions for query "Psr4" should be: 59 | | name | 60 | | Psr4Ns\ | 61 | | Psr4Split\Ns1\ | 62 | | Psr4Split\Ns2\ | 63 | | Psr4Fallback\ | 64 | And type completions for query "Psr4Ns\T" should be: 65 | | name | 66 | | TheClass | 67 | | TheSubdir\ | 68 | And type completions for query "Psr4Ns\TheSubdir\" should be: 69 | | name | 70 | | TheClass1 | 71 | | TheClass2 | 72 | Then completions for query "Psr4" should be: 73 | | name | 74 | | Psr4Ns\ | 75 | | Psr4Split\Ns1\ | 76 | | Psr4Split\Ns2\ | 77 | | Psr4Fallback\ | 78 | And completions for query "Psr4Ns\T" should be: 79 | | name | 80 | | Psr4Ns\TheClass | 81 | | Psr4Ns\TheSubdir\ | 82 | And completions for query "Psr4Ns\TheSubdir\" should be: 83 | | name | 84 | | Psr4Ns\TheSubdir\TheClass1 | 85 | | Psr4Ns\TheSubdir\TheClass2 | 86 | 87 | Scenario: Complete PSR-4 multi-dir namespaces 88 | Given I visit "src/main.php" in project "without-composer" 89 | Then type completions for query "MultiDirNs\T" should be: 90 | | name | 91 | | TheClass1 | 92 | | TheClass2 | 93 | Then completions for query "MultiDirNs\T" should be: 94 | | name | 95 | | MultiDirNs\TheClass1 | 96 | | MultiDirNs\TheClass2 | 97 | 98 | Scenario: Complete classmap namespaces 99 | Given I visit "src/main.php" in project "without-composer" 100 | Then type completions for query "ClassMapNs\" should be: 101 | | name | 102 | | ClassMapNs\MyClass | 103 | Then completions for query "ClassMapNs\" should be: 104 | | name | 105 | | ClassMapNs\MyClass | 106 | 107 | Scenario: Complete non-existing dir 108 | Given I visit "src/main.php" in project "without-composer" 109 | Then type completions for query "NonExisting\" should be nil 110 | Then completions for query "NonExisting\" should be nil 111 | -------------------------------------------------------------------------------- /features/ede-php-autoload-composer.feature: -------------------------------------------------------------------------------- 1 | Feature: Composer EDE project creation 2 | 3 | Scenario: Visit a file in the EDE project 4 | Given I visit "src/main.php" in project "with-composer" 5 | Then ede-php-autoload-project should exist 6 | 7 | 8 | Scenario: Load a basic PSR-0 class 9 | Given I visit "src/main.php" in project "with-composer" 10 | Then the class "Psr0Ns_TheClass" should be detected in "src/Psr0Ns/TheClass.php" 11 | 12 | Scenario: Load a PSR-0 class using fallback 13 | Given I visit "src/main.php" in project "with-composer" 14 | Then the class "Psr0Fallback_MyClass" should be detected in "src/Fallback/Psr0/Psr0Fallback/MyClass.php" 15 | 16 | 17 | Scenario: Load a basic PSR-4 class 18 | Given I visit "src/main.php" in project "with-composer" 19 | Then the class "Psr4Ns\TheClass" should be detected in "src/Psr4Ns/TheClass.php" 20 | 21 | Scenario: Load a PSR-4 multi-directory namespace 22 | Given I visit "src/main.php" in project "with-composer" 23 | Then the class "MultiDirNs\TheClass1" should be detected in "src/MultiDirNs1/TheClass1.php" 24 | And the class "MultiDirNs\TheClass2" should be detected in "src/MultiDirNs2/TheClass2.php" 25 | 26 | Scenario: Load a PSR-4 class using fallback 27 | Given I visit "src/main.php" in project "with-composer" 28 | Then the class "Psr4Fallback\MyClass" should be detected in "src/Fallback/Psr4/Psr4Fallback/MyClass.php" 29 | 30 | Scenario: Load a class the doesn't exist 31 | Given I visit "src/main.php" in project "with-composer" 32 | Then the class "Psr4Ns\DoesNotExist" should not be detected 33 | 34 | Scenario: Load an autoload-dev class 35 | Given I visit "src/main.php" in project "with-composer" 36 | Then the class "AutoloadDev\TestClass" should be detected in "src/AutoloadDev/TestClass.php" 37 | 38 | Scenario: Load a dependency class 39 | Given I visit "src/main.php" in project "with-composer" 40 | Then the class "ThirdParty\ThirdClass" should be detected in "vendor/third-party/third-party/src/ThirdClass.php" 41 | 42 | Scenario: Load a dependency with a target dir 43 | Given I visit "src/main.php" in project "with-composer" 44 | Then the class "TargetDir\Component\TheClass" should be detected in "vendor/target-dir/target-dir/TargetDir/Component/TheClass.php" 45 | 46 | Scenario: Load a composer dev dependency class 47 | Given I visit "src/main.php" in project "with-composer" 48 | Then the class "DevDependency\TestClass" should be detected in "vendor/third-party/dev-dependency/src/TestClass.php" 49 | 50 | Scenario: Visit a vendor file 51 | Given I visit "vendor/third-party/third-party/src/ThirdClass.php" in project "with-composer" 52 | Then the class "Psr0Ns_TheClass" should be detected in "src/Psr0Ns/TheClass.php" 53 | 54 | Scenario: Update the autoloads 55 | Given I visit "src/main.php" in project "with-composer" 56 | Then the class "NewNs\MyClass" should not be detected 57 | Given I update the composer file 58 | And I refresh the project autoloads 59 | Then the class "NewNs\MyClass" should be detected in "src/NewNs/MyClass.php" 60 | -------------------------------------------------------------------------------- /features/ede-php-autoload.feature: -------------------------------------------------------------------------------- 1 | Feature: Hand-made EDE project creation 2 | 3 | Scenario: Visit a file in the ede project 4 | Given I visit "src/main.php" in project "without-composer" 5 | Then ede-php-autoload-project should exist 6 | And ede-php-autoload-project should have "." as include path 7 | And ede-php-autoload-project should have "/usr/share/php" as system include path 8 | 9 | Scenario: Visit a directory in the ede project 10 | Given I visit "." in project "without-composer" 11 | Then ede-php-autoload-project should exist 12 | 13 | Scenario: Load a basic PSR-0 class 14 | Given I visit "src/main.php" in project "without-composer" 15 | Then the class "Psr0Ns_TheClass" should be detected in "src/Psr0Ns/TheClass.php" 16 | 17 | Scenario: Load a split PSR-0 namespace 18 | Given I visit "src/main.php" in project "without-composer" 19 | Then the class "Psr0Split\Ns2\TheClass" should be detected in "src/Psr0Split/Ns2/TheClass.php" 20 | 21 | Scenario: Load a basic PSR-4 class 22 | Given I visit "src/main.php" in project "without-composer" 23 | Then the class "Psr4Ns\TheClass" should be detected in "src/Psr4Ns/TheClass.php" 24 | 25 | Scenario: Load a split PSR-4 namespace 26 | Given I visit "src/main.php" in project "without-composer" 27 | Then the class "Psr4Split\Ns2\TheClass" should be detected in "src/Psr4Split/Ns2/TheClass.php" 28 | 29 | Scenario: Load a PSR-4 multi-directory namespace 30 | Given I visit "src/main.php" in project "without-composer" 31 | Then the class "MultiDirNs\\TheClass1" should be detected in "src/MultiDirNs1/TheClass1.php" 32 | And the class "MultiDirNs\\TheClass2" should be detected in "src/MultiDirNs2/TheClass2.php" 33 | 34 | Scenario: Guess a PSR-4 class name 35 | Given I visit "src/main.php" in project "without-composer" 36 | Then guessing the class name for "src/MultiDirNs1/SubNs1/SubNs2/Class.php" should return "MultiDirNs\SubNs1\SubNs2\Class" 37 | 38 | Scenario: Guess a split PSR-4 class name 39 | Given I visit "src/main.php" in project "without-composer" 40 | Then guessing the class name for "src/Psr4Split/Ns2/MyClass.php" should return "Psr4Split\Ns2\MyClass" 41 | 42 | Scenario: Load a classmap class 43 | Given I visit "src/main.php" in project "without-composer" 44 | Then the class "ClassMapNs\MyClass" should be detected in "src/ClassMapNs/MyClass.php" 45 | 46 | Scenario: Guess a classmap class name 47 | Given I visit "src/main.php" in project "without-composer" 48 | THen guessing the class name for "src/ClassMapNs/MyClass.php" should return "ClassMapNs\MyClass" 49 | 50 | 51 | Scenario: Load a class that doesn't exist 52 | Given I visit "src/main.php" in project "without-composer" 53 | Then the class "Psr4Ns\DoesNotExist" should not be detected 54 | -------------------------------------------------------------------------------- /features/step-definitions/ede-php-autoload-steps.el: -------------------------------------------------------------------------------- 1 | (Given "^I visit \"\\(.+\\)\" in project \"\\(.+\\)\"$" 2 | (lambda (file-path project-name) 3 | (find-file (ede-php-autoload-test-get-project-file-path file-path project-name)) 4 | )) 5 | 6 | (Given "^I update the composer file" 7 | (lambda () 8 | (ede-php-autoload-test-set-composer "new"))) 9 | 10 | (Given "^I refresh the project autoloads" 11 | (lambda () 12 | (call-interactively #'ede-php-autoload-reload-autoloads))) 13 | 14 | (Then "^ede-php-autoload-project should exist$" 15 | (lambda () 16 | (should (ede-php-autoload-project-p (ede-current-project))))) 17 | 18 | (Then "^ede-php-autoload-project should have \"\\(.+\\)\" as include path" 19 | (lambda (include-path) 20 | (should (string= (car (oref (ede-current-project) :include-path)) include-path)))) 21 | 22 | (Then "^ede-php-autoload-project should have \"\\(.+\\)\" as system include path" 23 | (lambda (include-path) 24 | (should (string= (car (oref (ede-current-project) :system-include-path)) include-path)))) 25 | 26 | (Then "^the class \"\\(.+\\)\" should be detected in \"\\(.+\\)\"$" 27 | (lambda (class-name file-path) 28 | (should 29 | (string= 30 | (ede-php-autoload-test-get-project-file-path 31 | file-path 32 | (ede-php-autoload-test-get-current-project-name)) 33 | (ede-php-autoload-find-class-def-file (ede-current-project) class-name))))) 34 | 35 | (Then "^the class \"\\(.+\\)\" should not be detected" 36 | (lambda (class-name) 37 | (should-not (ede-php-autoload-find-class-def-file (ede-current-project) class-name)))) 38 | 39 | (Then "^guessing the class name for \"\\(.+\\)\" should return \"\\(.+\\)\"" 40 | (lambda (file-name class-name) 41 | (should (string= (ede-php-autoload-get-class-name-for-file 42 | (ede-current-project) 43 | file-name) 44 | class-name)))) 45 | 46 | (Then "^type completions for query \"\\(.+\\)\" should be:" 47 | (lambda (query suggestion-table) 48 | (let ((suggestions (cl-loop for suggestion in (cdr suggestion-table) 49 | collect (car suggestion)))) 50 | (should (equal (ede-php-autoload-complete-type-name (ede-current-project) query) 51 | suggestions))))) 52 | 53 | (Then "^type completions for query \"\\(.+\\)\" should be nil" 54 | (lambda (query) 55 | (should (null (ede-php-autoload-complete-type-name (ede-current-project) query))))) 56 | 57 | (Then "^completions for query \"\\(.+\\)\" should be:" 58 | (lambda (query suggestion-table) 59 | (let ((suggestions (cl-loop for suggestion in (cdr suggestion-table) 60 | collect (car suggestion)))) 61 | (should (equal (ede-php-autoload-complete (ede-current-project) query) 62 | suggestions))))) 63 | 64 | (Then "^completions for query \"\\(.+\\)\" should be nil" 65 | (lambda (query) 66 | (should (null (ede-php-autoload-complete (ede-current-project) query))))) 67 | -------------------------------------------------------------------------------- /features/support/env.el: -------------------------------------------------------------------------------- 1 | (require 'package) 2 | (require 'f) 3 | (require 'cl-lib) 4 | (require 'ert) 5 | (require 'eieio) 6 | 7 | (defvar ede-php-autoload-support-path 8 | (f-dirname load-file-name)) 9 | 10 | (defvar ede-php-autoload-features-path 11 | (f-parent ede-php-autoload-support-path)) 12 | 13 | (defvar ede-php-autoload-root-path 14 | (f-parent ede-php-autoload-features-path)) 15 | 16 | (defvar ede-php-autoload-test-projects-root-path 17 | (f-join ede-php-autoload-root-path "test/projects")) 18 | 19 | (defun ede-php-autoload-test-get-project-file-path (file project) 20 | "Return the absolute path for FILE relative to PROJECT." 21 | (f-join ede-php-autoload-test-projects-root-path project file)) 22 | 23 | (defun ede-php-autoload-test-get-current-project-name () 24 | "Return the test project currently visited." 25 | (car 26 | (f-split 27 | (f-relative 28 | (or (buffer-file-name) default-directory) 29 | ede-php-autoload-test-projects-root-path)))) 30 | 31 | (defun ede-php-autoload-test-set-composer (composer-file-name) 32 | "Set the composer file of the project with-composer. 33 | 34 | COMPOSER-FILE-NAME is either default or new." 35 | (let* ((project-root (f-join ede-php-autoload-test-projects-root-path "with-composer")) 36 | (destination (f-join project-root "composer.json"))) 37 | 38 | (when (f-exists? destination) 39 | (f-delete destination)) 40 | 41 | (f-copy (f-join project-root (format "%s-composer.json" composer-file-name)) 42 | destination))) 43 | 44 | (add-to-list 'load-path ede-php-autoload-root-path) 45 | 46 | (package-generate-autoloads "ede-php-autoload" ede-php-autoload-root-path) 47 | (load (f-join ede-php-autoload-root-path "ede-php-autoload-autoloads.el")) 48 | 49 | (Setup 50 | (global-ede-mode 1)) 51 | 52 | (Before 53 | (setq ede-projects nil) 54 | 55 | (ede-php-autoload-test-set-composer "default") 56 | 57 | ;; Define projects 58 | ;; The composer projet is auto-detected 59 | (ede-php-autoload-project "Without composer" 60 | :file (f-join ede-php-autoload-test-projects-root-path 61 | "without-composer/project") 62 | :class-autoloads '(:psr-0 (("Psr0Ns" . "src/Psr0Ns") 63 | ("Psr0Split\\Ns1" . "src/Psr0Split/Ns1") 64 | ("Psr0Split\\Ns2" . "src/Psr0Split/Ns2") 65 | ("" . "src/Fallback/Psr0")) 66 | :psr-4 (("Psr4Ns" . "src/Psr4Ns") 67 | ("MultiDirNs" . ("src/MultiDirNs1" "src/MultiDirNs2")) 68 | ("Psr4Split\\Ns1" . "src/Psr4Split/Ns1") 69 | ("Psr4Split\\Ns2" . "src/Psr4Split/Ns2") 70 | ("NonExisting" . "src/NonExisting") 71 | ("" . "src/Fallback/Psr4")) 72 | :class-map ((ClassMapNs\\MyClass . "src/ClassMapNs/MyClass.php"))) 73 | :include-path '(".") 74 | :system-include-path '("/usr/share/php"))) 75 | -------------------------------------------------------------------------------- /test/ede-php-autoload-composer-test.el: -------------------------------------------------------------------------------- 1 | ;;; ede-php-autoload-composer-test.el --- Unit test for ede-php-autoload-composer 2 | 3 | ;;; Commentary: 4 | ;; 5 | ;; Copyright (C) 2017, Steven Rémot 6 | 7 | ;; Author: Steven Rémot 8 | ;; Keywords: PHP project ede 9 | ;; Homepage: https://github.com/emacs-php/ede-php-autoload 10 | 11 | ;; This file is not part of GNU Emacs. 12 | 13 | ;; This program is free software; you can redistribute it and/or 14 | ;; modify it under the terms of the GNU General Public License as 15 | ;; published by the Free Software Foundation; either version 2, or (at 16 | ;; your option) any later version. 17 | 18 | ;; This program is distributed in the hope that it will be useful, but 19 | ;; WITHOUT ANY WARRANTY; without even the implied warranty of 20 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 21 | ;; General Public License for more details. 22 | 23 | ;; You should have received a copy of the GNU General Public License 24 | ;; along with this program; see the file COPYING. If not, write to 25 | ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 26 | ;; Boston, MA 02111-1307, USA. 27 | 28 | ;;; Code: 29 | 30 | (ert-deftest ede-php-autoload-composer-merge-autoloads-concats-files () 31 | "`ede-php-autoload-composer-merge-autoloads' should concat provided autoloads." 32 | (should (equal 33 | 34 | (ede-php-autoload-composer-merge-autoloads 35 | '(:psr-0 (("Test" . "test/")) :psr-4 (("Test3" . "test3/"))) 36 | '(:psr-0 (("Test2" . "test2/")) :psr-4 (("Test4" . "test4/")))) 37 | 38 | '(:psr-0 (("Test2" . "test2/") 39 | ("Test" . "test/")) 40 | :psr-4 (("Test4" . "test4/") 41 | ("Test3" . "test3/")))))) 42 | 43 | (ert-deftest ede-php-autoload-composer-merge-autoloads-concats-files-merged-part () 44 | "`ede-php-autoload-composer-merge-autoloads' should merge paths with the same key." 45 | (should (equal 46 | 47 | (ede-php-autoload-composer-merge-autoloads 48 | '(:psr-0 (("Test" . "test/")) :psr-4 (("Test3" . "test3/"))) 49 | '(:psr-0 (("Test" . "test2/")) :psr-4 (("Test4" . "test4/")))) 50 | 51 | '(:psr-0 (("Test" . ("test/" "test2/"))) 52 | :psr-4 (("Test4" . "test4/") 53 | ("Test3" . "test3/")))))) 54 | 55 | (ert-deftest ede-php-autoload-composer-define-visitor () 56 | "`:define-visitors' should define a visitor according to its step." 57 | (setq ede-php-autoload-composer--visitors '()) 58 | 59 | (ede-php-autoload-composer-define-visitor 'test-visitor-1) 60 | (should (equal ede-php-autoload-composer--visitors '((:normal . (test-visitor-1))))) 61 | 62 | (ede-php-autoload-composer-define-visitor 'test-visitor-2 :late) 63 | (should (equal ede-php-autoload-composer--visitors '((:late . (test-visitor-2)) 64 | (:normal . (test-visitor-1))))) 65 | 66 | 67 | (ede-php-autoload-composer-define-visitor 'test-visitor-3 :early) 68 | (should (equal ede-php-autoload-composer--visitors '((:early . (test-visitor-3)) 69 | (:late . (test-visitor-2)) 70 | (:normal . (test-visitor-1))))) 71 | 72 | 73 | (ede-php-autoload-composer-define-visitor 'test-visitor-4) 74 | (should (equal ede-php-autoload-composer--visitors '((:early . (test-visitor-3)) 75 | (:late . (test-visitor-2)) 76 | (:normal . (test-visitor-4 test-visitor-1)))))) 77 | 78 | (ert-deftest ede-php-autoload-composer--run-visitors () 79 | "`ede-php-autoload-composer--run-visitors' should build configuration from visitors." 80 | (should (equal 81 | (ede-php-autoload-composer--run-visitors 82 | '((:late . ((lambda (context autoloads) 83 | (push :late autoloads)))) 84 | (:early . ((lambda (context autoloads) 85 | (push :early autoloads)))) 86 | (:normal . ((lambda (context autoloads) 87 | (push :normal autoloads))))) 88 | nil 89 | '()) 90 | 91 | '(:late :normal :early)))) 92 | 93 | (provide 'ede-php-autoload-composer-test) 94 | 95 | ;;; ede-php-autoload-composer-test.el ends here 96 | -------------------------------------------------------------------------------- /test/projects/with-composer/composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "autoload": { 3 | "psr-0": { 4 | "Psr0Ns": "src/", 5 | "": "src/Fallback/Psr0" 6 | }, 7 | "psr-4": { 8 | "Psr4Ns": "src/Psr4Ns", 9 | "MultiDirNs": ["src/MultiDirNs1", "src/MultiDirNs2"], 10 | "": "src/Fallback/Psr4" 11 | } 12 | }, 13 | "autoload-dev": { 14 | "psr-4": { 15 | "AutoloadDev": "src/AutoloadDev" 16 | } 17 | }, 18 | "require": { 19 | "third-party/third-party": "*", 20 | "target-dir/target-dir": "*" 21 | }, 22 | "require-dev": { 23 | "third-party/dev-dependency": "*" 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /test/projects/with-composer/composer.lock: -------------------------------------------------------------------------------- 1 | { 2 | "packages": [ 3 | { 4 | "name": "third-party/third-party", 5 | "autoload": { 6 | "psr-4": { 7 | "ThirdParty": "src" 8 | } 9 | } 10 | }, 11 | { 12 | "name": "target-dir/target-dir", 13 | "autoload": { 14 | "psr-0": { 15 | "TargetDir\\Component\\": "" 16 | } 17 | }, 18 | "target-dir": "TargetDir/Component" 19 | } 20 | ], 21 | "packages-dev": [ 22 | { 23 | "name": "third-party/dev-dependency", 24 | "autoload": { 25 | "psr-4": { 26 | "DevDependency": "src" 27 | } 28 | } 29 | } 30 | ] 31 | } 32 | -------------------------------------------------------------------------------- /test/projects/with-composer/default-composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "autoload": { 3 | "psr-0": { 4 | "Psr0Ns": "src/", 5 | "": "src/Fallback/Psr0" 6 | }, 7 | "psr-4": { 8 | "Psr4Ns": "src/Psr4Ns", 9 | "MultiDirNs": ["src/MultiDirNs1", "src/MultiDirNs2"], 10 | "": "src/Fallback/Psr4" 11 | } 12 | }, 13 | "autoload-dev": { 14 | "psr-4": { 15 | "AutoloadDev": "src/AutoloadDev" 16 | } 17 | }, 18 | "require": { 19 | "third-party/third-party": "*", 20 | "target-dir/target-dir": "*" 21 | }, 22 | "require-dev": { 23 | "third-party/dev-dependency": "*" 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /test/projects/with-composer/new-composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "autoload": { 3 | "psr-4": { 4 | "NewNs": "src/NewNs" 5 | } 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /test/projects/with-composer/src/AutoloadDev/TestClass.php: -------------------------------------------------------------------------------- 1 | 8 | ;; Keywords: PHP project ede 9 | ;; Homepage: https://github.com/emacs-php/ede-php-autoload 10 | 11 | ;; This file is not part of GNU Emacs. 12 | 13 | ;; This program is free software; you can redistribute it and/or 14 | ;; modify it under the terms of the GNU General Public License as 15 | ;; published by the Free Software Foundation; either version 2, or (at 16 | ;; your option) any later version. 17 | 18 | ;; This program is distributed in the hope that it will be useful, but 19 | ;; WITHOUT ANY WARRANTY; without even the implied warranty of 20 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 21 | ;; General Public License for more details. 22 | 23 | ;; You should have received a copy of the GNU General Public License 24 | ;; along with this program; see the file COPYING. If not, write to 25 | ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 26 | ;; Boston, MA 02111-1307, USA. 27 | 28 | ;;; Code: 29 | 30 | (add-to-list 'load-path (concat (file-name-directory (or load-file-name buffer-file-name)) "/..")) 31 | (message (concat (or load-file-name buffer-file-name) "/..")) 32 | 33 | (require 'ert) 34 | (require 'ede-php-autoload) 35 | 36 | ;;; test-helper.el ends here 37 | --------------------------------------------------------------------------------