简体   繁体   English

Aptana PHP Snippet(在ruby中):HOWTO创建getter / setter片段并首先使用变量Name的Char?

[英]Aptana PHP Snippet (in ruby): HOWTO create getter/setter snippet and capitalize first Char of variable Name?

I like snippets in Aptana. 我喜欢Aptana的片段。 I want to create a snippet that creates me a private class variable of type String. 我想创建一个片段,创建一个String类型的私有类变量。 I got it to work like this: 我让它像这样工作:

# code from php bunle in Aptana 'snippets.rb'
# private member with getter setter and type checking for STRING
  snippet 'private String MEMBER' do |s|
  s.trigger = 'myps'
  s.expansion = '
private \$${variableName}; /* string */

function set_${variableName}(/* string */ \$${variableName}) {
    if(!is_string(\$${variableName})) {
        throw new \Exception(get_class(\$this)."->set_${variableName}(): Parameter must be string!");
    }
    \$this->${variableName} = \$${variableName};
}

function get_${variableName}() {
    return \$this->${variableName};
}

'

Unfortunately I do not know Ruby and I would like to know if it is possible to get getter/setter function names like this (CamelCase): 不幸的是我不知道Ruby,我想知道是否有可能得到像这样的getter / setter函数名(CamelCase):

private $member;
function setMember($member);
function getMember();

instead of: 代替:

private $member;
function set_member($member);
function get_member();

I must use Camel case in my company and I really would like to get it to work. 我必须在我公司使用Camel案例,我真的很想让它发挥作用。

Thanks for you help. 谢谢你的帮助。

end 结束

I found a working solution for my question. 我为我的问题找到了一个有效的解决方案 I managed to generate getter and setter methods in a convenient way in Aptana Studio 3. I did not realize this solution via snippets but as a command in the php 'ruble' bundle. 我设法在Aptana Studio 3中以方便的方式生成getter和setter方法。我没有通过片段实现这个解决方案,而是作为php'ruble'包中的命令。 I will post the source for this command even i think it is very ugly code, but i really do not know ruby and just managed to get it to work somehow. 我将发布此命令的源代码,即使我认为它是非常难看的代码,但我真的不知道ruby并且设法让它以某种方式工作。 Beautification and better style are very welcome. 非常欢迎美化和更好的风格。

I found this link interesting: https://wiki.appcelerator.org/display/tis/Ruble+Specification 我发现此链接很有趣: https//wiki.appcelerator.org/display/tis/Ruble+Specification

howto get the command: 如何获得命令:

  1. In Aptana click on Commands > PHP > Edit this bundle 在Aptana中,单击“命令”>“PHP”>“编辑此包”
  2. in your project explorer you'll find a new "* PHP" bundle. 在您的项目资源管理器中,您将找到一个新的“* PHP”软件包。 Open "PHP -> commands" folder here 在这里打开“PHP - > commands”文件夹
  3. create a new file "getter_setter.rb" 创建一个新文件“getter_setter.rb”
  4. past ugly ruby code below into it 过去丑陋的红宝石代码进入它
  5. save that file and restart the IDE 保存该文件并重新启动IDE

code: 码:

require 'ruble'

command "getter_setter" do |cmd|
    cmd.key_binding = "CONTROL+G"

    cmd.input = :selection
    cmd.output = :insert_as_text

    cmd.invoke do |context|

        input = STDIN.read

        splittedInput = input.split(";")

        type = nil
        if splittedInput[1] != nil
            type = splittedInput[1].gsub(/\s+/, "")
            type = type.sub("//", "")
        end

        varName = splittedInput.first
        varName = varName.sub("private", "")
        varName = varName.gsub(/\s+/, "")
        varName = varName.sub("$", "")

        capitalizedName = varName.slice(0,1).capitalize + varName.slice(1..-1)

        templateTypeless = "

    function set#{capitalizedName}($#{varName}) {
        global $MY_CLIENT_LOGGER;
        if($MY_CLIENT_LOGGER) { $MY_CLIENT_LOGGER->log(get_class($this).'->set#{capitalizedName}()');}
        $this->#{varName} = $#{varName};
    }

    function get#{capitalizedName}() {
        global $MY_CLIENT_LOGGER;
        if($MY_CLIENT_LOGGER) { $MY_CLIENT_LOGGER->log(get_class($this).'->get#{capitalizedName}()');}
        return $this->#{varName};
    }

    "

        templateBasicType = "

    function set#{capitalizedName}(/* #{type} */ $#{varName}) {
        global $MY_CLIENT_LOGGER;
        if($MY_CLIENT_LOGGER) { $MY_CLIENT_LOGGER->log(get_class($this).'->set#{capitalizedName}()');}
        if(!is_#{type}(#{varName})) {
            throw new \Exception(get_class($this).'->set#{capitalizedName}(): Parameter must be #{type}!');
        }
        $this->#{varName} = $#{varName};
    }

    function get#{capitalizedName}() {
        global $MY_CLIENT_LOGGER;
        if($MY_CLIENT_LOGGER) { $MY_CLIENT_LOGGER->log(get_class($this).'->get#{capitalizedName}()');}
        return $this->#{varName};
    }

    "
        templateComplexType = "

    function set#{capitalizedName}(#{type} $#{varName}) {
        global $MY_CLIENT_LOGGER;
        if($MY_CLIENT_LOGGER) { $MY_CLIENT_LOGGER->log(get_class($this).'->set#{capitalizedName}()');}
        $this->#{varName} = $#{varName};
    }

    function get#{capitalizedName}() {
        global $MY_CLIENT_LOGGER;
        if($MY_CLIENT_LOGGER) { $MY_CLIENT_LOGGER->log(get_class($this).'->get#{capitalizedName}()');}
        return $this->#{varName};
    }

    "
        outputString = ""
        if type == nil
            outputString = templateTypeless
        elsif type == "string" or type == "int" or type == "float" or type == "double" or type == "array" or type == "object"
            outputString = templateBasicType
        else
            outputString = templateComplexType
        end

        context.output = outputString
    end
end

Usage: 用法:

  1. create a php file containing a class and declare a private $var 创建一个包含类的php文件并声明一个私有的$ var
  2. select the line in which the var was declared and use the keyboard shortcut "CONTROL+G" to create the getter/setter methods 选择声明var的行,并使用键盘快捷键“CONTROL + G”创建getter / setter方法

Hint: If you append a comment to the line of private member declaration note a basic type (string/int/float/double/array/bool/object) then you can get type checking in the setter. 提示:如果在私有成员声明行中添加注释,请注意基本类型(string / int / float / double / array / bool / object),然后您可以在setter中进行类型检查。 eg: private $var //string 例如: private $var //string

Hint: If you append a comment to the line of private member declaration note a complex type then you can get type checking in the setter. 提示:如果您将注释附加到私有成员声明行注释一个复杂类型,那么您可以在setter中进行类型检查。 eg: private $var //my\\namespace\\CComplexTypeClass 例如: private $var //my\\namespace\\CComplexTypeClass

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM