Gallery:Eclipse教程

来自站长百科
跳转至: 导航、​ 搜索

Eclipse作为Gallery2的IDE[ ]

我们可以拿(PHP/PDT)Eclipse用于:

  • 语法高亮
  • 实时语法检查
  • Subversion整合
  • 数据库查询(Quantum DB)
  • 图形debug(设定断点等)(尚未经测试)
  • web服务器和DB的综合控制

安装及配置[ ]

默认情况下,Eclipse无法保存带有.class扩展名的文件。因为Eclipse用于Java开发,而.class文件在此是 二进制文件。这里有一些解决办法:

  1. PHP文件不要使用.class作为扩展名-> G2则没办法,在G2中Eclipse的问题无法对此做修改
  2. 请勿安装Eclipse SDK,而应安装Eclipse Platform,后者仅为一最小化的不带有开发工具及debug能力的Eclipse。因为platform不带有JDT插件,因此对于.class它不具有预定义,锁定的文件关联。弊端:你无法使用图形debug来对PHP进行debug操作。
  3. 安装Eclipse SDK但修改org.eclipse.jdt.core_3.1.2/plugin.xml文件来移除JDT与.class文件的文件关联。弊端:如果你希望使用此Eclipse实体进行Java开发的话,还可能需要取消plugin.xml文件中所作的修改。
  4. 如果你做Java和PHP开发,请安装2个Eclipse。一个针对Java,另个则针对PHP。弊端:维护工作量增加,尤其是在升级时。
  5. 在Eclipse中的debug对话中运行一个最小化的Eclipse(即Eclipse中的Eclipse)。对性能的影响最小,但你也无法对PHP脚本进行debug,因为你仍是在一个简单的Eclipse平台中编辑PHP脚本。
  6. 使用具有PDT 0.7的Eclipse 3.2.1,并在Gallery中进行编辑时关闭Java Capability,而在处理Java项目时恢复过来。

处理PDT和.class文件[ ]

安装Eclipse PDT整合,http://www.eclipse.org/pdt/.

Version 3.3[ ]

  1. 向文件添加*.class(Add *.class to File) > 偏好(Preferences) > 一般(General) > 类型(Content Types) > 文本(Text) > PHP内容类型(PHP Content Type)
  2. 向文件添加*.class(Add *.class to File)>偏好(Preferences) >一般(General)> 编辑器(Editors) > 文件关联和选择PHP编辑器作为默认(File Associations and select PHP Editor as the default)

Version 3.2.1[ ]

这里的操作过程能够处理*.class文件的三件事:

  1. 启用语法高亮
  2. 启用载入/保存
  3. 启用文本比对

安装完PDT开始处理Gallery项目之前,打开窗口(Window)->偏好(Preferences)->一般(General)->Eclipse内部功能(Capabilities from within Eclipse)。点击'高级(Advanced)'按钮。在高级对话框中扩展'开发(Development)'功能树并禁用'Java Development'。

这也会禁用其他的开发功能(这些都依赖Java功能)。但重点在于:这会移除*.class文件的二进制解释,这是由Java开发功能所锁定的。

Eclipse: Disabling the Java Capability

点击确定关闭对话框并转到一般(General)->内容类型(Content Types)。扩展'文本(Text)'内容类型树并点击'PHP内容类型(PHP Content type)'。在'文件关联(File Associations)'中你会看到预定义的PHP文件类型*.inc,*.php等。添加新的文件类型'*.class'。

Eclipse: Adding *.class to the PHP Content types

点击确定关闭偏好对话框。现在你就可以处理Gallery *.class文件了。

当完成Gallery相关工作并再次开始处理Java项目之前,将上面的步骤反过来进行一遍:

  1. 从'PHP内容类型(PHP content type)'中移除*.class
  2. 重新开启所有功能

处理PHPeclipse和.class文件[ ]

为.class文件添加PHP语法高亮(Syntax Highlighting)[ ]

现在我们需要告知Eclipse使用PHPeclipse 的PHP编辑器来编辑.class文件:

1. 如果Eclipse正在运行,关闭它
2. 使用文本编辑器打开eclipse/plugins/net.sourceforge.phpeclipse_1.1.7/plugin.xml
3. 搜索所有的php4并在相应位置添加class。即,对逗号分隔的列表,添加 ,class而对于如下类似结构
     <fileTypes
            type="text"
            extension="php4">
      </fileTypes>

添加

     <fileTypes
            type="text"
            extension="class">
      </fileTypes>

当然你可以全局性地将php4替换为class,因为我们不使用.php4扩展名;但最好保留它,因为你也会需要编辑非G2或第三方的库。

4. 保存文件并使用-clean选项启动Eclipse,告知其重读所有的plugin.xml文件。
 eclipse -clean (在windows上 c:\program files\eclipse\eclipse.exe –clean)
5. 在窗口(Window) -> 偏好(Preferences) -> 一般(General) -> 内容类型(Content Types) -> 文本(Text) -> PHP中,向PHP添加.class。
6. 在窗口(Window) -> 偏好(Preferences) -> 一般(General) -> 编辑器(Editors) -> 文件关联(File Associations)中,添加.class并为.class添加PHP编辑器作为默认。

此时,对.class 文件已具有PHP 语法高亮但还不能进行保存。在尝试保存一个被修改的.class文件时,你会得到错误"Save failed: null"。

解决.class文件保存时出现的问题[ ]

我们需要移除Java和.class文件的文件关联,或者至少解锁此关联,因为你无法禁用.引出Eclipse中锁定的设定。

1. 关闭Eclipse
2. 如果eclipse/plugins/下的org.eclipse.jdt.core_3.1.2 plugin不是一个文件夹而是一个jar文件的话,你必须先解压org.eclipse.jdt.core_3.1.2.jar
 从http://java.sun.com/j2se/1.4.2/download.html下载Java SDK以获取
   jar (un)archiver
 在eclipse/plugins/下创建名为org.eclipse.jdt.core_3.1.2的文件夹
 将org.eclipse.jdt.core_3.1.2.jar移动至该文件夹中
 解压 -xf org.eclipse.jdt.core_3.1.2.jar(解压jar文件到该文件夹中)
 删除the org.eclipse.jdt.core_3.1.2.jar文件,我们不再需要它了
3. 使用文本编辑器打开eclipse/plugins/eclipse.jdt.core_3.1.2/plugin.xml,并按如下所示进行修改
 替换:
 <content-type id="javaClass" name="%javaClassName" 
        priority="high"				
        file-extensions="class">        
        <describer
            class="org.eclipse.core.runtime.content.BinarySignatureDescriber">
            <parameter name="signature" value="CA, FE, BA, BE"/>
        </describer>
    </content-type>
  为:
  <!--    <content-type id="javaClass" name="%javaClassName" 
        priority="high"				
        file-extensions="class">        
        <describer
            class="org.eclipse.core.runtime.content.BinarySignatureDescriber">
            <parameter name="signature" value="CA, FE, BA, BE"/>
        </describer>
    </content-type>        -->
4. 保存文件并按前文所述的-clean选项启动eclipse

此时你可以在(PHP)eclipse 中保存.class文件了!Java与.class之间的文件关联不仅被解锁了,同时也被完全移除了。

使用Compare插件[ ]

如果你想使用Eclipse的compare插件,也就需要"patch" eclipse/plugins/org.eclipse.compare_3.1.1/plugin.xml。

compare 插件/编辑器在CVS/SVN复查某描述性认可消息之前是非常有用的。Eclipse的compare编辑器十分强大,不但能够显示不同的行,还能将它们的不同一并显示出来。

在CVS/SVN同步透视方面,它能自动进行三向对比。如果出现冲突的改动,这就极为有用,这样你就可以查看对存储版本所作的修改了。

  1. 关闭Eclipse并在文本编辑器中打开eclipse/plugins/org.eclipse.compare_3.1.1/plugin.xml
  2. 如果如果eclipse/plugins/下的org.eclipse.compare_3.1.1 plugin不是一个文件夹而是一个jar文件的话,你必须先解压org.eclipse.compare_3.1.1.jar

<extension point="org.eclipse.compare.contentMergeViewers"> Remove ,class from the extension list:

           extensions="exe,class,dll,binary,zip,jar"

替换为

extensions="exe,dll,binary,zip,jar"
  1. 保存文件并使用-clean方式启动Eclipse

这里给出一个例子,Eclipse的compare编辑器于CVS同步透视内打开。双击某文件就可以由CVS处获取必要的数据并显示不一致的行,以及内送,外发和冲突的改动。使用黄色的向下箭头进行浏览,首先可跳至某一有出入的行,接着是行内不一致之处。


Eclipse compare editor on a conflicting *.class file.

针对G2编码风格配置Eclipse[ ]

  • 窗口(Window) -> 偏好(Preferences) -> 一般(General) -> 编辑器(Editors)(或Eclipse3.2中的工作区):在编辑G2文件时,不要忘记配置Eclipse以使用ISO-8859-1作为默认locale。这也可在单位项目的基础上进行设定,这位于项目属性的Info部分之中。
  • 在相同的窗口中,设定New Text File Line Delimiter为Default。这将与Subversion中的svn:eol-style=native相兼容。
  • 设定tab = 8 spaces (每缩进层次缩进4个空格长度,我们的tabs就是8个空格的长度)
  • 窗口(Window) -> 偏好(Preferences) -> 一般(General) -> 编辑器(Editors) -> 文本编辑器(Text Editors):设定表宽为8个空格的长度
  • 窗口(Window) -> 偏好(Preferences)-> PHPeclipse Web Development -> PHP: Tab Width 8 spaces, Print Margin Column 100
  • 窗口(Window) -> 偏好(Preferences)-> PHPeclipse Web Development -> PHP -> Formatter: Line Splitting tab: Maximum line length 100
  • 窗口(Window) -> 偏好(Preferences)-> PHPeclipse Web Development -> PHP -> Formatter: Style tab: 禁用'Indention is represented by a tab'并设定表宽为8空格长度

同时对外部工具进行配置以使语法检查器等运作:

  • 窗口(Window) -> 偏好(Preferences)PHPeclipse Web Development ->PHP外部工具( PHP External Tools): 设定外部解析命令(必须在同一台计算机上安装了PHP)

安装CVS并基于sourceforge CVS存储库进行Gallery2项目的创建[ ]

  • 修改Eclipse CVS 设定(窗口(Window) -> 偏好(Preferences) -> Team -> CVS -> Ext连接方式(Ext Connection Method): 选择"使用其他连接方式类型进行连接"并在下拉列表中选择extssh (原因:Eclipse仅在extssh方式的CVS中使用SSH,但but cvs.sourceforge.net只接受ext。因此在你的CVS连接中,选择ext作为方式;但通过在Eclipse中对此设定进行修改,你就告知其在ext已使用与连接的情况下使用extssh)
  • 向你的存储库添加gallery CVS:窗口(Window)-> 显示视图(Show View) -> 其他…(Other ...) -> CVS -> CVS存储库(CVS Repositories)。在此右击并选择新建(New) -> 存储库位置(Repository Location)。Host:cvs.sourceforge.net,存储库路径: /cvsroot/gallery,用户名:你的sf.net用户名,密码:现在可以留空,连接类型:ext并点击完成。
  • 现在你可以创建新的PHPEclipse项目了:文件(File) -> 新建(New) -> 项目(Project)。HP Project:名称,如gallery2。
  • 最后为该项目使用我们的CVS存储库:右击你的gallery2项目,点击Team -> Share Project。CVS -> 使用现有存储库位置(Use Existing Repository Location),选择你的ext:username@cvs.sourceforge:/cvsroot/gallery repository。在下一个视图中"输入模块名称",选择"使用一个现有模块(Use an existing module)",选取gallery2,点击下一步。选取HEAD分支,接着点击下一步。当其开始进行同步时,点击中止,因为我们在本地存储库中没有任何内容,同步过程无的放矢,实际上也不会由CVS检出。
  • 接着右击你的项目 -> Team -> 更新(Update)。来获取首次检出的文件。

使用SVN并基于sourceforge SVN存储库进行Gallery2项目的创建[ ]

由于Gallery2现在而不再使用CVS而使用SVN(Subversion)作为其源代码控制系统,这将促进SVN支持在Eclipse中的添加。默认情况下SVN支持是不与Eclipse同时放出的(如CVS)但有一Subclipse插件随时可用。

但不一定非要安装subclipse不可。你可以使用SVN的命令行版本(如果你使用Windows的话就是TortoiseSVN),且仍能使用Eclipse进行编辑。在偏好(Preferences) -> 一般(General) -> 工作区(Workspace)中启用自动刷新(或在使用命令行SVN之后,通过导航视图中的context菜单进行刷新)。

如果你觉得安装并使用这里所述的插件,还可以从命令行/ [[Gallery:Using_Subversion_on_Windows|TortoiseSVN]做一些SVN方面的工作,同时还包括来自Eclipse内部的其他工作(尽管Sourceforge 不提倡在某一相同的拷贝上杂糅使用SVN的各种客户端! 这里给出警告)。Subclipse插件在Eclipse中整合地非常好,这与CVS团队卓著的支持能力有异曲同工之妙。

安装Subclipse[ ]

按照Subclipse安装指导的详细步骤对应到要求你重启Eclipse的步骤。确保确实重启了Eclipse,而不要仅是应用修改!

在重启Eclipse之后,请先打开偏好(Preferences) -> Team -> SVN并确认SVN接口 JavaHL (JNI)被检查了(相对于JavaSVN (Pure Java),因为这正是我所测试过的,并且能够保证完全与SVN的命令行版本兼容)。其他所有设定先放在一边吧,除非你知道它们的实际含义。

接着你应当应用G2 Developer SVN设定,如资料Gallery:Using_Subversion#开发者信息中所给出的信息,并相应地对你的subclipse config文件进行修改。

另见有关Error: Secure connection truncated 的提示并确保你使用的是正确/新的URL。

 https://gallery.svn.sourceforge.net/svnroot/gallery/......

由Sourceforge SVN存储库检出Gallery2[ ]

准备一来自存储库的Gallery2检出:

  1. 打开SVN存储库透视:Window -> Open Perspective -> Other -> SVN Repository Exploring
  2. SVN Repository视图中新建一个存储库位置:通过右键菜单新建(New) -> 存储库位置(Repository Location)或工具条的添加SVN存储库(Add SVN Repository)
  3. 在对话框中输入该位置的URL:https://gallery.svn.sourceforge.net/svnroot/gallery/trunk/gallery2 (注意是https)并点击完成(Finish)
  4. SVN Repository视图中出现一个新入口,打开它的属性(Properties)并确保设定了存储库根URLhttps://gallery.svn.sourceforge.net/svnroot/gallery

现在就能检查Gallery2的当前版本了(即trunk)。如果你希望能够在分支版本中轻松转换的话,就可以输入https://gallery.svn.sourceforge.net/svnroot/gallery作为第三步中的位置URL。

接着就是做新PHP项目的检出了。默认情况下,Eclipse将新项目及SVN拷贝置入你的工作区中。这是完全没有问题的,要在本地host上使用Apache测试G2的话,可以为工作区中的项目位置使用别名(Alias)。但你可能更希望在htdocs区总创建一个外部Eclipse项目。

检出Gallery2 trunk:

  1. if you have used the long URL above in step 3: choose Checkout in the context menu of the repository location.
  2. if you have choosen the short URL above in step 3: open the tree for the repository location, open the trunk path and choose Checkout in the context menu of the gallery2 path.
  3. in the Check out as dialog choose Check out as a project configured using the New Project Wizard, since we want to make the project a PHP project and may be don't want it to reside in the workspace. Press Finish.
  4. in the New Project wizard select PHP -> PHP Project, press Next >.
  5. give the project a name and either use the default location in the workspace or enter the file system path to a new directory elsewhere (e.g. in your htdocs tree). Press Finish.

The directory will be created, Eclipse switches to the PHP perspective and shows the new project in the navigator view. The checkout progresses, which will take some time. After the checkout Eclipse will do a workspace build, which will take some more time ... so get yourself a cup of coffee.

You can also look at an animated tutorial for the checkout (note: any URLs there are outdated, prepend 'gallery.' before 'svn.sourceforge.net').

处理Subclipse[ ]

When the workspace is built, open the new project in the navigator and have a look. It looks like every other project (except for the [gallery/trunk/gallery2] annotation on the project name may be).

Dig deeper into the project and notice the decoration on every file name: it'll show the revision number, datetime and last author of the file (you can have similar decorations for folders too, see Window -> Preferences -> Team -> SVN -> Label Decorations in the Text tab).

Now do a dummy change (to be undone/reverted soon) to some source file deep in the hierarchy, say themes/matrix/templates/error.tpl. Open that file, notice the Smarty syntax highlighting and add a Smarty comment {* Dummy change to be reverted *} after </head> (in the same line). As soon as you save the file you'll notice a decoration (a white star in a dark square) in the lower right corner of the files icon and all the way up to the projects icon in the Navigator view, which reminds you of changes you've done locally.

Feel free to have a first look at the Team Synchronizing perspective: with your dummy change still in place open (on the project folder) the context menu Team -> Synchronize with Repository. Eclipse switches to the Synchronizing perspective and compares the whole project with the repository version. This again will take some time, but typically much less than the initial checkout. Most work is done locally comparing the working copy against the BASE revision (which is magically stored by SVN, the PRISTINE copy). Sooner or later you'll see all the differences: just one outgoing change in error.tpl. With some chance you'll see incoming changes too, when some commit from others has taken place since your checkout. Note, that synchronizing does NO HARM AT ALL to either the repository or your working copy. It is only a compare.

In the Synchronize view just double click the error.tpl. Instead of opening the file for simple editing Eclipse now opens the Compare Editor and highlights the first changed line. Look at the header center column: the arrow to the right indicates an 'outgoing' change. Click the yellow down arrow: the compare editor now jumps to the first difference in the changed line. You'll begin to love this advanced compare features soon, at last when you ever have to resolve a conflicting change!

Enough exploring, let's quickly undo the dummy change. Close the compare editor. From the context menu of error.tpl choose Revert, press OK in the dialog and note, that the 'outgoing' synchronize view is now empty. Switch back to the PHP perspective and note, that all decoration icons are gone, indicating, that you have a clean working copy.

Most things you can do with the command line SVN you can also do with Subclipse and often more comfortably. But since Subclipse is tightly integrated into Eclipse, similar to the CVS integration, be sure to have read the Subclipse docs (Help -> Help Contents then Subclipse) and make yourself familiar with the integration. If you are working with SVN for the first time you might also want to read the [SVN Book] for SVN concepts.

Some daily tasks (very short description) are:

  • PHP Navigator, project context menu: Team -> Update
    Use that to quickly get up to date, when you have no local changes or don't care about details of repository changes. Do this also before every attempt to commit any changes. It updates your whole working copy to the right revision. Else commit attempts may fail.
  • PHP Navigator, project context menu: Team -> Synchronize with Repository
    to be used, when you have local changes and/or care about changes done to the repository since last update. Eclipse will open the Team Synchronizing perspective, compares your working copy with the repository and shows all differences in the Synchronize view (see image above). Don't forget to update the whole project before attempting to commit anything (see last point), even if you have updated everything from the synchronizing perspective step by step!
  • Synchronize view, double click on a file:
    Opens the very comfortable Eclipse compare editor (see image above) where your working copy is compared to the HEAD revision in the repository (and optionally to the BASE revision, if you switch to the 3-way compare).
  • Synchronize view, file context menu: Show in Resource History
    Opens the History view, where you can inspect the commit history including the commit comment and other files participating in the atomic commit operation.

You naturally can work with only part of the project by selecting one or more individual files or directories in the PHP Navigator and opening the Team context menu for one of the selected items.

  • PHP Navigator, individual item(s) context menu: Team -> Commit or
    Synchronize view, individual item(s) context menu: Commit
    This commits all selected items in an atomic operation. If selected files ain't added to SVN before you get the opportunity to do so in the same operation.
  • PHP Navigator, individual item(s) context menu: Team -> Revert or
    Synchronize view, individual item(s) context menu: Revert
    Undoes all local changes for the items and reverts them to the BASE revision.
  • PHP Navigator, individual file/dir context menu: Team -> Create Patch
    Creates a unified diff for the item, that can be stored to the clipboard or filesystem.

Just enjoy.

A final note: as is the case with CVS you always can have more then one working copy of the SVN repository. This might help developers having to do different tasks at the same time, e.g. enhancing module A and at the same time having to fix a bug in a totally unrelated module B or even the core. Instead of mangeling both modifications in the same working copy and having to sort them out at commit time it is often more advisable to e.g. do the bug fix on a freshly checked out working copy. That way a simple commit on the bug fix working copy commits the right things automatically. Just update the other project to get the bug fix into that also.

使用Quantum DB[ ]

有了Eclipse的Quantum DB扩展,你可以由Eclipse内部浏览数据库,查询数据库以及复制数据等。

你应当使用PHPeclipse 安装了Quantum DB。

在此指导中,我们假定你是在Windows XP上使用MySQL的,但是你应能使其在Linux或其他DBMS上运行。

首先我们需要安装ODBC数据源:

  1. 你需要安装MySQL ODBC驱动:可从http://dev.mysql.com/downloads/connector/odbc/3.51.html获取安装程序并执行
  2. 打开开始(Start) > 控制面板(Control Panel) > 管理工具(Administrative Tools) > 数据源(Datasources (ODBC))
  3. 点击System-DSN并添加一个新的数据源,当然你得选择新安装的MySQL驱动了。数据源名称(Data Source Name):任意,描述(Description):任意,服务器(Server):localhost,用户名/密码(User / Password):你的mysql用户名/密码,数据库(Database):选择你的gallery2数据库。接着点击确定保存。

现在你就可以使用Eclipse Quantum DB中的新ODBC数据源了。

  1. 启动Eclipse并转到Quantum DB透视。
  2. 在数据库书签视图中,右击并选择新书签…(New Bookmark...)
  3. 选取JDBC-ODBC 驱动并点击下一步。
  4. 输入你的DB用户名/密码并输入你在ODBD DSN安装中所输入的数据源名称。点击下一步。
  5. 为此书签命名并点击完成。

现在你可以双击新书签来使用它了(或右击-> 连接(Connect))