1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

phpStorm class name generation

Discussion in 'XenForo Development Discussions' started by Jake B., May 21, 2015.

  1. Jake B.

    Jake B. Well-Known Member

    Does anyone who uses phpStorm know of some way I can set it up so when I create something along the lines of library/X/Y/Z.php it will create it with a template along the lines of:
    Code:
    class X_Y_Z
    {
    
    }
    
    No big deal if there is no way to do this since it only takes a matter of seconds to manually do it, just more curious what phpStorm is capable of :)
     
    SneakyDave likes this.
  2. Chris D

    Chris D XenForo Developer Staff Member

    I don't think so.

    It's easier with namespaces.

    PhpStorm can infer the correct namespace automatically when they are used so when you go to New > PHP Class, all you usually have to do is enter the file name itself (e.g. ClassTest) and it will populate the namespace automatically (e.g. Test)

    PHP:
    <?php

    namespace Test;

    class 
    ClassTest
    {

    }
     
    SneakyDave and Jake B. like this.
  3. Jake B.

    Jake B. Well-Known Member

    Does XenForo support namespaces in its autoloader? All of our Add-ons require PHP 5.3 anyways, so I have no problem starting to use namespaces if they're supported.
     
  4. Liam W

    Liam W Well-Known Member

    Yes, it was added a few versions ago. Can't remember which one, might have been before or after 1.2.

    Some of my add-ons use namespaces.

    For the class proxy, you have to extend XFCP_Class, like so:

    PHP:
    namespace LiamW\Test\Extend\Model;

    class 
    User extends XFCP_User
    {
    }
    Oh, another thing, when adding the class in the listener, use the full class name with backslashes.

    So, for above:

    PHP:
    namespace LiamW\Test;

    class 
    Listener
    {
        public static function 
    extendUserModel($class, array &$extend)
        {
            
    $extend[] = 'LiamW\Test\Extend\Model\User';
        }
    }
    Liam
     
    Last edited: May 21, 2015
  5. Chris D

    Chris D XenForo Developer Staff Member

    Yeah, what he said :)
     
    Jake B. likes this.
  6. lol768

    lol768 Active Member

    I normally use a helper file in conjunction with namespaces to improve auto-complete with PhpStorm. Maybe this will also be useful to others. Something like:

    PHP:
    <?php
    if (false) {
        namespace 
    Addon\ControllerPublic;
        class 
    XFCP_Account extends \XenForo_ControllerPublic_Account {}
    }
     
    SneakyDave likes this.
  7. Liam W

    Liam W Well-Known Member

    I do the same, without the additional namespace... no need for it. I just have the if(false) branch in the same file as the main extending class.
     
  8. Yoskaldyr

    Yoskaldyr Well-Known Member

    PhpStorm file templates can be used for auto generating headers of new class files.
    I've modified a "\.WebIde90\config\fileTemplates\internal\PHP Class.php" file:
    PHP:
    <?php
    #parse("PHP File Header.php")
    #set ( $EXTENDS = "")
    #if (${NAMESPACE})
     #set ( $NAMESPACE = $NAMESPACE.replace("\", "_") )
     #set ( $NAME = "${NAMESPACE}_${NAME}")
     #if ( $NAMESPACE.endsWith("_Extend"))
      #set ( $EXTENDS = " extends XFCP_${NAME}")
     #elseif ( $NAMESPACE.endsWith("_Model"))
      #set ( $EXTENDS = " extends XenForo_Model")
     #elseif ( $NAMESPACE.endsWith("_DataWriter"))
      #set ( $EXTENDS = " extends XenForo_DataWriter")
     #elseif ( $NAMESPACE.endsWith("_ControllerPublic"))
      #set ( $EXTENDS = " extends XenForo_ControllerPublic_Abstract")
     #elseif ( $NAMESPACE.endsWith("_ControllerAdmin"))
      #set ( $EXTENDS = " extends XenForo_ControllerAdmin_Abstract")
     #elseif ( $NAMESPACE.endsWith("_ControllerHelper"))
      #set ( $EXTENDS = " extends XenForo_ControllerHelper_Abstract")
     #elseif ( $NAMESPACE.endsWith("_ViewPublic"))
      #set ( $EXTENDS = " extends XenForo_ViewPublic_Base")
     #elseif ( $NAMESPACE.endsWith("_AdminAdmin"))
      #set ( $EXTENDS = " extends XenForo_ViewAdmin_Base")
     #end
    #end

    class ${NAME}${EXTENDS}
    {
    }
    And after that in the "New PHP Class" form a namespace field auto renamed to a correct xenforo class.
    How it works in the attached screenshots.
     

    Attached Files:

    Kirk, arms, lol768 and 2 others like this.

Share This Page