|
| 1 | +<?php |
| 2 | +namespace ClassTemplate; |
| 3 | +use Exception; |
| 4 | +use ReflectionClass; |
| 5 | +use ReflectionObject; |
| 6 | +use CodeGen\UserClass; |
| 7 | +use CodeGen\Renderable; |
| 8 | + |
| 9 | +class ClassFile extends UserClass |
| 10 | +{ |
| 11 | + public $class; |
| 12 | + |
| 13 | + public $extends; |
| 14 | + |
| 15 | + public $interfaces = array(); |
| 16 | + |
| 17 | + public $uses = array(); |
| 18 | + |
| 19 | + public $methods = array(); |
| 20 | + |
| 21 | + public $consts = array(); |
| 22 | + |
| 23 | + public $properties = array(); |
| 24 | + |
| 25 | + public $staticVars = array(); |
| 26 | + |
| 27 | + /** |
| 28 | + * Registered trait |
| 29 | + */ |
| 30 | + public $traits = array(); |
| 31 | + |
| 32 | + public $templateFile; |
| 33 | + public $templateDirs; |
| 34 | + public $options = array(); |
| 35 | + public $msgIds = array(); |
| 36 | + |
| 37 | + |
| 38 | + public $usedClasses = array(); |
| 39 | + |
| 40 | + /** |
| 41 | + * constructor create a new class template object |
| 42 | + * |
| 43 | + * @param string $className |
| 44 | + * @param array $options |
| 45 | + * |
| 46 | + * a sample options: |
| 47 | + * |
| 48 | + * $t = new ClassTemplate('NewClassFoo',[ |
| 49 | + * 'template_dirs' => [ path1, path2 ], |
| 50 | + * 'template' => 'Class.php.twig', |
| 51 | + * 'template_args' => [ ... predefined template arguments ], |
| 52 | + * 'twig' => [ 'cache' => false, ... ] |
| 53 | + * ]) |
| 54 | + * |
| 55 | + */ |
| 56 | + public function __construct($className, array $options = array()) |
| 57 | + { |
| 58 | + parent::__construct($className); |
| 59 | + $this->setOptions($options); |
| 60 | + } |
| 61 | + |
| 62 | + public function setOptions(array $options) |
| 63 | + { |
| 64 | + $this->options = $options; |
| 65 | + } |
| 66 | + |
| 67 | + public function setOption($key, $val) { |
| 68 | + $this->options[$key] = $val; |
| 69 | + } |
| 70 | + |
| 71 | + public function render(array $args = array()) |
| 72 | + { |
| 73 | + return "<?php\n" . parent::render($args); |
| 74 | + } |
| 75 | + |
| 76 | + public function writeTo($file) |
| 77 | + { |
| 78 | + return file_put_contents($file, $this->render()); |
| 79 | + } |
| 80 | + |
| 81 | + public function load() { |
| 82 | + $tmpname = tempnam('/tmp', str_replace('\\','_',$this->class->getFullName()) ); |
| 83 | + file_put_contents($tmpname, $this->render() ); |
| 84 | + return require $tmpname; |
| 85 | + } |
| 86 | + |
| 87 | + public function addMsgId($msgId) |
| 88 | + { |
| 89 | + $this->msgIds[] = $msgId; |
| 90 | + } |
| 91 | + |
| 92 | + public function setMsgIds($msgIds) |
| 93 | + { |
| 94 | + $this->msgIds = $msgIds; |
| 95 | + } |
| 96 | +} |
| 97 | + |
0 commit comments