Last active
October 22, 2015 12:52
-
-
Save vaneves/2257ed2de2f9df464524 to your computer and use it in GitHub Desktop.
PHPator - Creator of PHP classes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
class PHPator | |
attr_reader :content, :properties, :namespace | |
attr_accessor :clazz | |
def initialize | |
@content = "" | |
@properties = [] | |
end | |
def namespace(name) | |
@namespace = name.gsub("::", "\\") | |
end | |
def add_var(name) | |
@properties.push(name) | |
end | |
def add_line(content, tab = 0) | |
@content << content << "\n" | |
end | |
def encapsulate(property) | |
method = property.gsub("_", " ").split.map(&:capitalize).join("") | |
content = <<METHODS | |
public function set#{method}(#{property}) | |
{ | |
$this->#{property} = $#{property}; | |
} | |
public function get#{method}() | |
{ | |
return $this->#{property}; | |
} | |
METHODS | |
@content << "\n" << content | |
end | |
def processor | |
add_line("<?php\n") | |
add_line("namespace #{namespace}\n") | |
add_line("class #{clazz}") | |
add_line("{") | |
properties.each {|property| add_line("\tprivate $" + property + ";") } | |
properties.each {|property| encapsulate(property)} | |
add_line("}") | |
File.write("#{clazz}.php", @content) | |
puts "Arquivo #{clazz}.php criado com sucesso\n:D" | |
end | |
end | |
if __FILE__ == $0 | |
puts "Bem-vindo ao PHPator, por favor, informe seu nome para que possa lhe identificar:" | |
nome = gets.chomp | |
puts "Então, #{nome}, diga-me o nome da classe que deseja criar:" | |
clazz = gets.chomp | |
phpator = PHPator.new | |
phpator.clazz = clazz | |
puts "#{nome}, informe um namespace (por exemplo App::Model)" | |
namespace = gets.chomp | |
phpator.namespace(namespace) | |
puts "#{nome}, informe o nome para uma propriedade ou \\q para sair" | |
property = gets.chomp | |
while property != "\\q" | |
phpator.add_var(property) | |
puts "#{nome}, informe o nome para uma propriedade ou \\q para sair" | |
property = gets.chomp | |
end | |
phpator.processor | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment