Skip to content

jpshannon/sublime-phex

 
 

Repository files navigation

Phex for Sublime Text

Phex extends the PHP support provided by Sublime Text 3. It adds additional snippets (PSR-comptabile), extended syntax highlighting and useful commands. Currently there is no possibility to extend the PHP.tmlanguage, therefore this is a replacement instead of an addition.

Installation

First you have to disable the default PHP package in your preferences:

{
    ...
    "ignored_packages": [ "PHP" ],
    ...
}

Then navigate into your Packages directory and clone this repository:

$ git clone https://github.com/florianeckerstorfer/sublime-phex phex

Commands

Phex contains some commands to make it easier for you to work with PHP.

You can find videos of every command in better quality on the Phex website.

Create Class

The Create Class command allows you to create a new PHP class. This currently only works if you are working in a project. By default the class is created based on the project root folder and if there is a src folder in your project root this will be used as base. You can prefix the class with ~ to create the class in the currently active directory, that is, in the directory of the open file.

Create Class Screencast

Phex's Create Class command supports PSR-4, except when using relative names (~ operator). Suppose the following composer.json exists in the project root directory:

{
    "autoload": {
        "psr-4": {
            "Phex\\": "src"
        }
    }
}

Invoking the Create Class command with Phex\Foo\Bar creates a class with the following properties:

  • Class name: Bar
  • Namespace: Phex\Foo\Bar
  • Filename: src/Foo/Bar.php

Create PSR-4 Class Screencast

Create Interface

Works like the Create Class command, but creates an interface instead of a class.

Create Interface Screencast

Insert Class Name

The Insert Class Name command opens a fuzzy search panel allowing you to select a class and insertes the selected class into the view. This command also works outside of the PHP scope and therefore helps you when writing PHPDoc or other documentation files.

Insert Class Name Screencast

Insert Namespace

The Insert Namespace command inserts the namespace statement of the active file.

Insert Namespace Screencast

I recommend setting up a keyboard shortcut, for example, Super+Alt+n. You can do this by going to the Preferences menu and selecting Key Bindings - User. Insert the following snippet:

[
    // Other key bindings
    {
        "keys": ["super+alt+n"], "command": "phex_insert_namespace"
    }
]

Create Property

The Create Property command inserts the code for a property and the corresponding setter and getter into the active document. Phex also creates the appropriate PHPDoc for property, setter and getter. You can invoke the command and provide either just the property name or type and property name (separated by a space). Types of the form string[] will result in a array typehint.

Create Property Screencast

Configuration

Phex supports multiple levels of configuration. Currently you can set the author, license and copyright information used by Phex in the PHPDoc when creating new classes and interfaces. You can set your default system settings by creating a file called phex.sublime-settings in Sublime's User/ directory.

For example, this file could look like:

{
    "phex_default_author": "Florian Eckerstorfer <florian@eckerstorfer.co>",
    "phex_default_license": "http://opensource.org/licenses/MIT The MIT License",
    "phex_default_copyright": "(c) 2014 Florian Eckerstorfer"
}

Additionally you can change these settings on a per-project basis. There exists a setting for the package PHPDoc tag. Your .sublime-project file could look like:

{
    "folders": [
        // ...
    ],
    "settings": {
        "package": "florianeckerstorfer/phex-test",
        "author": "Florian Eckerstorfer",
        "copyright": "2014 Florian Eckerstorfer",
        "license": "http://opensource.org/licenses/MIT The MIT License"
    }
}

Note: You need to open the project in order to allow Phex to access these settings. If you open the directory (for example, by using the command line helper subl dir_name) Phex cannot access the settings and will fall back to the global settings.

Snippets

PHP Extended contains all snippets from the default PHP package from Sublime Text 3 (some slightly modified to match the Symfony 2 coding standard) and adds new snippets.

The snippets are designed to be as short as possible and still intuitive.

In this documentation the snippets are organized by area of usage: methods, properties, variables and PHPDoc.

Methods

Snippets to create methods

Tab trigger Content
pufun public function A(B) { … }
pofun protected function A(B) { … }
pifun private function A(B) { … }
pusfun public static function A(B) { … }
posfun protected static function A(B) { … }
pisfun private static function A(B) { … }

Properties

Snippets to create properties.

Tab trigger Content
set public function setA($A) { $this->A = $A; return $this; }
get public function getA() { return $this->A; }
setget public function setA($A) { $this->A = $A; return $this; } public function getA() { return $this->A; }
puv public $var;
pov protected $var;
piv private $var;
pusv public static $var;
posv protected static $var;
pisv private static $var;

Variables

Snippets to create and access variables.

Tab trigger Content
rth return $this;
-> $this->var = $var;
ptr print_r($var);
preptr echo '<pre>'.print_r($var, true).'</pre>';
vd var_dump($var);
prevd echo '<pre>'; var_dump($var); echo '</pre>';

PHPDoc

Snippets for PHPDoc.

Tab trigger Content
indoc /** {@inheritDoc} */

Syntax Highlighting

Currently Phex extends the syntax highlighting provided by the default PHP package. I hope to extend this in the future.

Annotations

The default PHP.tmlanguage only has support for PHPDoc annotations. Phex adds support for popular libraries and their annotations.

Doctrine MongoDB ODM

Phex has syntax highlighting for annotations provided by Doctrine MongoDB ODM.

  • MongoDB\AlsoLoad
  • MongoDB\Bin
  • MongoDB\BinCustom
  • MongoDB\BinFunc
  • MongoDB\BinMD5
  • MongoDB\BinUUID
  • MongoDB\Boolean
  • MongoDB\Collection
  • MongoDB\Date
  • MongoDB\DiscriminatorField
  • MongoDB\DiscriminatorMap
  • MongoDB\Distance
  • MongoDB\Document)
  • MongoDB\EmbedMany
  • MongoDB\EmbedOne
  • MongoDB\EmbeddedDocument
  • MongoDB\Field
  • MongoDB\File
  • MongoDB\Float
  • MongoDB\Hash
  • MongoDB\Id
  • MongoDB\Increment
  • MongoDB\Index
  • MongoDB\Int
  • MongoDB\InheritanceType
  • MongoDB\Key
  • MongoDB\MappedSuperclass
  • MongoDB\NotSaved
  • MongoDB\PostLoad
  • MongoDB\PostPersist
  • MongoDB\PostRemove
  • MongoDB\PostUpdate
  • MongoDB\PreLoad
  • MongoDB\PrePersist
  • MongoDB\PreRemove
  • MongoDB\PreUpdate
  • MongoDB\ReferenceMany
  • MongoDB\ReferenceOne
  • MongoDB\String
  • MongoDB\Timestamp
  • MongoDB\UniqueIndex

Doctrine ORM

Phex has syntax highlighting for annotations provided by Doctrine ORM.

  • ORM\Column
  • ORM\ChangeTrackingPolicy
  • ORM\|DiscriminatorColumn
  • ORM\|DiscriminatorMap
  • ORM\Entity
  • ORM\GeneratedValue
  • ORM\HasLifecycleCallbacks
  • ORM\Id
  • ORM\Index
  • ORM\InheritanceType
  • ORM\JoinColumn
  • ORM\JoinColumns
  • ORM\JoinTable
  • ORM\ManyToOne
  • ORM\ManyToMany
  • ORM\MappedSuperclass
  • ORM\OneToOne
  • ORM\OneToMany
  • ORM\OrderBy
  • ORM\PostLoad
  • ORM\PostPersist
  • ORM\PostRemove
  • ORM\PostUpdate
  • ORM\PrePersist
  • ORM\PreRemove
  • ORM\PreUpdate
  • ORM\SequenceGenerator
  • ORM\Table
  • ORM\UniqueConstraint
  • ORM\Version

Symfony2 Validation

Phex has syntax highlighting for annotations provided by Symfony2 Validation.

  • Assert\All
  • Assert\Blank
  • Assert\Callback
  • Assert\Choice
  • Assert\Collection
  • Assert\Country
  • Assert\Date
  • Assert\DateTime
  • Assert\Email
  • Assert\False
  • Assert\File
  • Assert\Image
  • Assert\Ip
  • Assert\Language
  • Assert\Length
  • Assert\Locale
  • Assert\Min
  • Assert\MinLength
  • Assert\Max
  • Assert\MaxLength
  • Assert\NotBlank
  • Assert\NotNull
  • Assert\Null
  • Assert\Regex
  • Assert\Time
  • Assert\True
  • Assert\Type
  • Assert\Url
  • Assert\Valid
  • UniqueEntity

PHPUnit

Phex has syntax highlighting for annotations provided by PHPUnit.

  • assert
  • backupGlobals
  • backupStaticAttributes
  • codeCoverageIgnore
  • codeCoverageIgnoreStart
  • codeCoverageIgnoreEnd
  • covers
  • dataProvider
  • depends
  • expectedException
  • expectedExceptionCode
  • expectedExceptionMessage
  • group
  • outputBuffering
  • runTestsInSeparateProcesses
  • runInSeparateProcess
  • test
  • testdox
  • ticket

Gedmo Doctrine Extensions

Phex has syntax highlighting for annotations provided by Gedmo Doctrine Extensions.

  • Gedmo\Slug
  • Gedmo\SoftDeleteable
  • Gedmo\Timestampable
  • Gedmo\Translatable

Braincrafted Validation

Phex has syntax highlighting for annotations provided by Braincrafted Validation.

  • BraincraftedAssert\Enum

Changelog

Version 0.1.1 (8 May 2014)

  • Added Create Property command
  • Insert Class Name command now replaces the current selection with the class name
  • Insert Class Name now shows the name of the class (instead of the filename) in the menu

Version 0.1 (8 May 2014)

  • Initial release
  • Included commands:
    • Create class
    • Create interface
    • Insert namespace
    • Insert class name
  • Syntax highlighting for annotations
    • Doctrine MongoDB ODM
    • Doctrine ORM
    • Symfony2 Validation
    • PHPUnit
    • Gedmo Doctrine Extensions
    • Braincrafted Validation
  • Snippets

Author

About

Phex provides PHP developers with tools to writer better PHP code faster. It replaces the default PHP package provided by Sublime Text and adds a bunch of useful commands, contains additional snippets and extends syntax highlighting.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Packages

 
 
 

Contributors

Languages

  • JavaScript 94.6%
  • Python 5.4%