在BBEdit中检查PHP语法

如上所述几个月最近的PHP版本包括一个命令行工具,这意味着您可以使用PHP作为shell脚本语言The version of PHP that comes with Panther includes the PHP command-line tool; if you’re using Jaguar, you can download a free来自Marc Liyanage的PHP安装程序其中包括命令行支持。

让我们通过向BBEdit添加PHP语法检查来使用它。

这是它的工作原理如果你通过了PHP命令行工具-l选项(这是一个小写的L),它将执行基本的语法检查而不是执行源代码例如,从终端输入:

php -l /path/to/your/file.php

我说“基本”,因为据我所知,PHP的语法检查器只检测解析错误 - 错误如此恶劣导致PHP无法解析代码Also, because PHP stops processing the file once it hits a single parse error, it only returns a single error; if you’ve got multiple parse errors in your source code, PHP’s syntax checker will only gripe about the first one你需要修复它并重新运行检查器以找到下一个不过,它总比没有检查器好。

根据PHP的文档-l开关

This option provides a convenient way to only perform a syntax check on the given PHP code关于成功,文本No syntax errors detected in <filename>写入标准输出,shell返回代码是0失败时,文字解析<filename>时出错in addition to the internal parser error message is written to standard output and the shell return code is set to255

好的,现在我们知道如何告诉命令行了PHPtool to perform a syntax check, and we know what kind of output to expect要从BBEdit调用它,我们可以使用AppleScript做shell脚本命令所以AppleScript脚本如何工作的基本要点:

  1. 获取前文本窗口文档的路径。
  2. 使用做shell脚本打电话php -l然后是步骤1的路径。
  3. If there are no errors, put up a “No errors found” alert; if a parse error is found, show the error message in an alert.

但是,如上所述,步骤1有点懒惰What if the front BBEdit window has unsaved changes? We可以如果文件有未保存的更改,则让脚本自动保存文件但这仍然是懒惰的 - 我们应该能够使用未保存的更改对文件进行语法检查另外,如果前BBEdit窗口是一个从未保存过的无标题新文档怎么办?

解决这些问题的一种方法是使用临时文件,如果前BBEdit窗口有未保存的更改(或者如果它没有使用Unix样式的行结尾,那么这就是PHP工具预期)然后我们将临时文件传递给PHP的语法检查器。

当发现错误时,我们可以创建BBEdit结果浏览器窗口,而不是简单地在警报中显示错误消息BBEdit通过其内置的HTML语法检查器以及对语法检查Perl脚本的支持来实现这一点But what would be the point, since PHP is only ever going to return a single error at a time? A simple alert is less intrusive:

警报对话框中PHP错误的屏幕截图。

什么然而,是一个很好的额外的触摸,将检查PHP是否返回带有错误消息的行号,如果是,则在文本窗口中选择该行所以我们会这样做。

And, speaking of BBEdit’s built-in HTML checker, wouldn’t it be nice if we could combine it with our PHP checker? Because BBEdit supports script粘贴性, 我们可以加上一个简短的menuselect()我们脚本的处理程序,我们可以将它用作BBEdit菜单脚本。

菜单脚本保存在“BBEdit Support”文件夹的“Menu Scripts”文件夹中菜单脚本可以替换或补充任何BBEdit的内置菜单命令菜单脚本通过在表单中​​提供特殊文件名附加到命令菜单•名称在这种情况下,因为我们想要补充BBEdit的HTML语法检查器,我们需要使用文件名“Check•Document Syntax”。

但是我们也可以使用与BBEdit的Scripts菜单中调用的常规脚本相同的脚本为此,我们将添加一个在奔跑脚本的处理程序。

这是我在这里设置的方式:

  1. I saved the script as a compiled AppleScript, in the “Scripts” folder in my “BBEdit Support FolderI gave it the name “PHP Syntax Check”, and that’s how it appears in my Scripts menu.

  2. I made an alias to the “PHP Syntax Check” script, and I named the alias “Check•Document Syntax”I moved the alias to the “Menu Scripts” folder.

所以它只是一个脚本文件,但是使用别名和两个单独的AppleScript处理程序例程,它可以以两种不同的方式使用:

  • 当它正常调用时,通过Scripts菜单,脚本的在奔跑handler is invoked, and the only thing it does is perform a PHP syntax check against the front window.

  • When you invoke BBEdit’s Check Document Syntax command from the Markup menu, the script’smenuselect()处理程序被调用If PHP returns a parse error, we display the error message in an alert and tell BBEdit not to perform the HTML syntax checkIf PHP doesfind any errors, we tell BBEdit to perform its HTML syntax checkThus, if BBEdit displays its “No errors were found” dialog, we know that the file passed through both syntax checkers without complaint.

Here’s the script, with inline comments to explain the details:

-- start script
on run
   -- The run handler is called when the script is invoked normally,
   -- such as from BBEdit's Scripts menu.
   set php_errs to PHPSyntaxCheck()
   if not php_errs then
      display dialog "No PHP syntax errors detected." buttons {"OK"} ¬
         default button 1 with icon note
   end if
end run


on menuselect()
   -- The menuselect() handler gets called when the script is invoked
   -- by BBEdit as a menu scriptSave this script, or an alias to it,
   -- as "Check•Document Syntax" in the "Menu Scripts" folder in your
   -- "BBEdit Support" folder.
   set php_errs to PHPSyntaxCheck()
   if php_errs then
      -- PHP reported an error, so tell BBEdit *not* to
      -- continue with it's HTML syntax check:
      return true
   else
      -- No PHP errors, so tell BBEdit to run its
      -- HTML syntax check 
      return false
   end if
end menuselect


on PHPSyntaxCheck()
   -- Input: none
   -- Returns: true if PHP reports errors, false otherwise

   tell application "BBEdit"
      try
         set w to text window 1
      on error
         beep
         return
      end try

        set is_dirty to modified of (document of w)
        -- Find out if w is using Mac, Unix, or DOS line endings:
        set linebreaks to (line breaks of document of w)
        set the_filename to name of (document of w)

      if (is_dirty) or (linebreaks is not equal to Unix) then
         -- We need to write a temporary file.
         set parent_folder to (path to temporary items folder)
         set temp_file to (parent_folder as string) & the_filename
         if not (my WriteUnixTextFile(temp_file, text of w)) then
            -- End script, because an error occured writing the temp file
            return
         end if
      else
         -- We can pass the actual file to PHP, no temp file needed:
         tell application "Finder"
            set parent_folder to (container of (file of w as alias)) as alias
         end tell
      end if
   end tell

   -- First, cd to the directory where the script file is, then tell
   -- php to syntax check itWe *could* do this with one command,
   -- by passing to php the full path of the script, but then the
   -- error messages from php will contain the full path as well,
   -- instead of just the file name.
   --
   -- (NOTE: If you're running Jaguar, you'll need to change the path for
   -- the php tool, probably to "/usr/local/php/bin/php" )
   set the_command to "cd " & quoted form of POSIX path of parent_folder & ¬
      ";  /usr/bin/php -l " & (quoted form of (the_filename))
   try
      set the_result to do shell script the_command
      if the_result starts with "No syntax errors detected" then
         set errors_found to false -- the return value
      else
         -- I don't think we'll ever get here, because if PHP reports
         -- any actual errors, they're sent to STDERR, not STDOUT, and
         -- will thus trigger the below 'on error' handler.
         display dialog the_result
      end if

   on error err_text
      tell application "BBEdit"
         try
            -- first line of err_text is, as far as I can tell, always blank
            -- second line is the useful error message
            -- third line is "Errors parsing <filename>"
            set errors_found to true
            set msg to paragraph 2 of err_text
            set line_num to last word of msg as integer
            select line line_num of text window 1
            display dialog "PHP " & msg with icon stop ¬
               buttons {"OK"} default button 1
         on error
            -- If the error message from PHP comes back in
            -- in an unexpected format, display it in a new
            -- BBEdit window.
            make new text window with properties {text:err_text}
         end try
      end tell
   end try

   return errors_found

end PHPSyntaxCheck



on WriteUnixTextFile(file_name, file_contents)
   -- Write a text file with unix-style line endings.
   -- Input:
   --    file_name - the HFS-style path for the file to write
   --    file_contents - the text to write to a file
   -- Returns: true for success, false for failure
   try
      set file_ref to ¬
         open for access file file_name with write permission
      set eof of file_ref to 0

      -- change the text of file_contents to unix line breaks
      set old_delims to AppleScript's text item delimiters
      set AppleScript's text item delimiters to return
      set text_list to every text item of file_contents as list
      set AppleScript's text item delimiters to (ASCII character 10)
      set file_contents to (text_list as string)
      set AppleScript's text item delimiters to old_delims

      write file_contents to file_ref starting at eof
      close access file_ref
      return true
   on error err_msg
      try
         close access file file_ref
      end try
      display dialog err_msg with icon stop buttons {"OK"} ¬
         default button 1
      return false
   end try
end WriteUnixTextFile
-- end script
以前: 图文传播
下一个: 老习惯