hugot/phpns

一个使PHP命名空间操作变得简单的命令行工具。

维护者

详细信息

github.com/hugot/phpns

源代码

问题

安装: 8

依赖: 0

建议者: 0

安全: 0

星标: 1

关注者: 1

分支: 0

开放问题: 0

语言:Shell

dev-master 2019-10-23 12:30 UTC

This package is auto-updated.

Last update: 2024-09-23 22:54:38 UTC


README

Phpns是一个用于管理PHP项目中命名空间和索引的命令行界面。它使用bash编写,主要利用grep来索引目录中存在的命名空间和类。使用此索引,Phpns可以解析PHP文件,添加缺失的use语句,完成完全限定名(FQN)的命名空间和类,等等!

安装

使用basher

安装phpns的推荐方法是使用basher,bash的包管理器。如果正确安装,basher将负责为phpns添加到您的shell中的参数完成,并将可执行文件包含到您的PATH中。Basher还可以使更新到最新版本变得简单。您可以使用以下命令安装phpns

basher install --ssh hugot/phpns

克隆

phpns没有依赖,除了sedgrepawk以及一些大多数Linux系统上应该可以轻松获得的工具。您可以通过以下步骤使用git轻松安装phpns

  1. 在您想要安装phpns的空目录中执行cd
  2. 将存储库克隆到目录中:git clone git@github.com:hugot/phpns.git .
  3. 将存储库的bin目录添加到您的路径中,并使其永久有效
    printf 'export PATH="%s:$PATH"\n' "$(pwd)/bin" >> ~/.bashrc && source ~/.bashrc
  4. 源完成文件,并确保每次启动shell时都将其源代码
    printf "source '%s'\n" "$(pwd)/completions/phpns.bash" >> ~/.bashrc && source completions/phpns.bash
  5. 通过运行phpns help测试您的安装

您可以使用以下命令自动化此过程

# Note: Don't forget to replace INSTALL_DIR with your actual installation directory here!
cd INSTALL_DIR && git clone git@github.com:hugot/phpns.git . \
    && printf 'export PATH="%s:$PATH"\nsource '"'%s'"'\n' "$(pwd)/"{bin,completions/phpns.bash} >> ~/.bashrc \
    && source ~/.bashrc

用法

命令和选项

Phpns提供了一组命令,允许您查询项目中可用的命名空间和类信息。除了提供信息命令之外,phpns还提供了像"fix-uses"这样的文件操作命令。下面是phpns help的输出,简要概述了可用的命令和选项。

phpns - Resolve namespaces and fix missing use statements in your PHP scripts.
    
    USAGE:
        phpns COMMAND [ ARGUMENTS ] [ OPTIONS ]
    
    COMMANDS:
        ns,  namespace            FILE         Echo the namespace that is declared in FILE
        i,   index                             Index a project
        fu,  find-use             CLASS_NAME   Echo the FQN of a class
        fxu, fix-uses             FILE         Add needed use statements to FILE
        cns, classes-in-namespace NAMESPACE    Echo the classes that reside in NAMESPACE
        cmp, complete             WORD         Echo completions for FQN's that match WORD.
        fp,  filepath             FQN          Echo the filepath of the class by the name of FQN.

    TO BE IMPLEMENTED:
        rmuu, remove-unneeded-uses FILE: Remove all use statements for classes that are not being used.

    OPTIONS FOR ALL COMMANDS:
        -s --silent     Don't print info.
    
    UNIQUE OPTIONS PER COMMAND:
        namespace: -
        index:
            -d, --diff                Show differences between the files in the index and the files in the project directory.
            -N, --new                 Only index new files
        find-use:
            -j, --json                Provide possible use FQN's as a json array.
            -p, --prefer-own          If there are matches inside the "src" dir, only use those.
            -a, --auto-pick           Use first encountered match, don't provide a choice.
            -b. --bare                Print FQN's without any additives.
        fix-uses:
            -j, --json                Provide possible use FQN's per class as a json object with the class names as keys.
            -p, --prefer-own          If there are matches inside the "src" dir, only use those.
            -a, --auto-pick           Use first encountered match, for every class, don't provide a choice.
            -o, --stdout              Print to stdout in stead of printing to the selected file.
        complete:
            -e, --expand-classes      If no root namespaces match, expand FQN's for classes by the name of WORD
            -n, --no-classes          Only provide completions for namespaces.
            -c, --complete-classes    If no root namespaces match, provide FQN's for all (partially) matching classes.
        filepath: -

与其他程序的集成

一些phpns命令有--json选项,这将使它们以JSON格式输出信息,这可能对您将phpns与其他应用程序集成很有用。其他命令将以易于解析的格式输出数据,但如果您遇到问题,请不要犹豫,可以提出问题或甚至提交一个pull request,这样我们可以为您解决这个问题。

Emacs插件

此存储库的"elisp"目录中包含了一个小的emacs插件。只需将其添加到您的loadpath中,并使用以下(或类似)设置

(require 'phpns)

(global-set-key (kbd "C-c c") 'phpns-helm)

;; Inside your php-mode hook:
(define-key php-mode-map (kbd "C-c u") 'phpns-fix-uses-interactive)

更详细的示例

(require 'phpns)

(global-set-key (kbd "C-c c") 'phpns-helm)

(defun my-php-mode-hook ()
    (define-key php-mode-map (kbd "C-c u") 'phpns-fix-uses-interactive))

(add-hook 'php-mode-hook 'my-php-mode-hook)

bash完成函数

如果phpns已在您的系统上正确安装,则此存储库的completions目录中的完成函数将被源到您的shell中,为您提供phpns及其各种命令的自动完成功能。其中一些函数使用phpns的complete命令来完成当前目录中命名空间和类的FQN。这些函数还可以用于完成您自己的自定义别名、函数和程序的FQN。

示例

我在日常工作中经常会做的事情之一就是通过命令行打开php文件。一个能让这个过程变得更简单、更简洁的函数将是我工作流程中的一大福音,所以让我们来创建一个吧!
我希望我的函数能够根据我提供的类全称(FQN)来打开文件。毕竟,我是与这些类打交道的,它们在文件系统中的位置并不是我最关心的问题。为了将类的FQN解析为其定义的文件路径,我们可以使用phpns的filepath命令。以下是一个bash函数,它可以在这种情况下派上用场:

# Note: I like to use vim, which is why it is in this example,
# but the trick would of course still work if you were to replace
# vim with nano, gedit or even vscode in this function.

pvim() {
    vim "$(phpns filepath "$1")"
}

这个函数将允许我通过输入pvim 'App\Entity\Post'来打开定义了App\Entity\Post类的文件。然而,这对我来说仍然有点啰嗦。我更喜欢尽量少地输入,所以让我们给这个函数添加一些自动补全功能!

# Complete FQN's of classes for the pvim command
complete -o nospace -F __phpns_complete_expand_classes pvim

这样,我就可以输入pvim Post<TAB>,然后得到pvim App\\Entity\\Post,之后我可以按下ENTER键开始编辑文件。酷吧?这一切只需要4行bash代码!

菜单补全

__phpns_complete_expand_classes补全类全称,如果提供了一个完整的类名。也可以使用__phpns_complete_classes_expand_classes函数,该函数还可以补全部分匹配的类名,但这会产生大量结果,bash的标准补全可能无法很好地处理。如果你选择这条路线,请考虑启用readline的菜单补全,因为使用phpns这种方式会带来更好的体验。为了启用菜单补全,我使用了以下命令,你可以将这些命令添加到bashrcbash_aliases中,以便永久启用此行为。

bind 'set show-all-if-ambiguous on'
bind 'TAB:menu-complete'