public function __construct(Reader $reader, \ReflectionClass $reflect, array &$cached_namespace) { $class_name = $reflect->getName(); $this->name = $class_name; $this->reflection = $reflect; $this->addAnnotation($reflect, $cached_namespace, $reader->getClassAnnotations($reflect)); foreach ($reflect->getProperties() as $p) { $property_name = $p->getName(); if (PhpHelper::isSQLKeywords($property_name)) { throw new \Exception(sprintf("`%s->%s` can not use SQL key words as property name", $class_name, $property_name)); } $this->properties_annotations[$property_name] = array(); self::addAnnotation($reflect, $cached_namespace, $reader->getPropertyAnnotations($p), $property_name); if (count($this->properties_annotations[$property_name]) < 1) { unset($this->properties_annotations[$property_name]); } } }
public function addAnnotationPropertyCompiler($id, array &$attributes) { if (!isset($attributes['alias'])) { throw new \Exception(sprintf("service(%s, tags:{name: %s}) require tag alias", $id, self::PROPERTY_TAG_NAME)); } $name = $attributes['alias']; if (!PhpHelper::isPropertyName($name) || in_array($name, $this->_ignore_name_list)) { throw new \Exception(sprintf("service(%s, tags:{name: %s, alias: %s}) tag alias invalid", $id, self::PROPERTY_TAG_NAME, $name)); } $builder = new SymforceAnnotationPropertyBuilder(); $builder->setId($id); $builder->setName($name); if (isset($attributes['parent'])) { $parent_name = $attributes['parent']; if (!isset($this->class_builders[$parent_name])) { throw new \Exception(sprintf("service(%s, tags:{name: %s, alias: %s, parent: %s}) parent must be one of (%s)", $id, self::PROPERTY_TAG_NAME, $name, $parent_name, join(',', array_keys($this->class_builders)))); } $parent = $this->getClassBuilderByName($parent_name); if ($parent->hasPropertyBuilder($name)) { throw new \Exception(sprintf("service(%s, tags:{name: %s, alias: %s, parent: %s}) parent must be one of (%s)", $id, self::PROPERTY_TAG_NAME, $name, $parent_name, $parent->getPropertyBuilder($name)->getId())); } $parent->addPropertyBuilder($builder); } else { if (isset($this->public_properties[$name])) { throw new \Exception(sprintf("service(%s, tags:{name: %s, alias: %s}) conflict with service(%s) tag alias", $id, self::PROPERTY_TAG_NAME, $name, $this->getPropertyBuilderByName($name)->getId())); } $this->public_properties[$name] = $builder; } if (isset($attributes['type'])) { if (!in_array($attributes['type'], $this->_default_property_types)) { throw new \Exception(sprintf("service(%s, tags:{name: %s, alias: %s, type: %s}) type must be one of(%s)", $id, self::PROPERTY_TAG_NAME, $name, $attributes['type'], join(',', $this->_default_property_types))); } $builder->setType($attributes['type']); } }
public function writeCache($_class_file = null) { if (!$_class_file) { $_class_file = \Symforce\CoreBundle\PhpHelper\PhpHelper::findFileByClassName($this->getName()); } $shortName = pathinfo($_class_file, \PATHINFO_FILENAME); $namespace = $this->getNamespace(); $writer = new \Symforce\CoreBundle\PhpHelper\PhpWriter(); $writer->writeln("<?php\n"); if (!empty($namespace)) { $writer->writeln("namespace " . $namespace . ";\n"); } $imports = $this->getUseStatements(); foreach ($imports as $alias => $use) { $_alias = substr($use, -1 - strlen($alias)); if ($_alias == '\\' . $alias) { $writer->writeln(sprintf("use %s ;", $use)); } else { $writer->writeln(sprintf("use %s as %s ;", $use, $alias)); } } $doc_block = $this->getDocblock(); $writer->writeln("/**")->writeln($doc_block ? $doc_block : sprintf(' * This code has been auto-generated by \\%s', __CLASS__))->writeln(" */\n"); if ($this->isAbstract()) { $writer->write('abstract '); } else { if ($this->isFinal()) { $writer->write('final '); } } $writer->write('class ' . $shortName); if ($this->getParentClassName()) { $writer->write(' extends \\' . ltrim($this->getParentClassName(), '\\')); } $writer->writeln(" {\n")->indent(); foreach ($this->_traits as $_trait) { if (!trait_exists($_trait)) { throw new \Exception(); } $writer->writeln(' use \\' . ltrim($_trait, '\\') . ';'); } foreach ($this->getConstants() as $name => $value) { $writer->writeln(sprintf('const %s = %s;', $name, var_export($value, 1))); } /** * @var $property PhpProperty */ foreach ($this->getProperties() as $property) { $property->writeCache($writer); } foreach ($this->lazy_properties as $visible => $visible_values) { foreach ($visible_values as $name => $value) { $writer->writeln(sprintf("\n%s \$%s = %s ;", $visible, $name, PhpHelper::compilePropertyValue($value))); } } if ($this->lazy_writer) { $this->lazy_writer->writeln($this->lazy_method->getBody()); $this->lazy_method->setBody($this->lazy_writer->getContent()); } /** * @var $method PhpMethod */ foreach ($this->getMethods() as $method) { if ($method instanceof PhpMethod) { $method->flushLazyCode(); $_body = $method->getWriter()->getContent(); } else { $_body = $method->getBody(); } $writer->write("\n"); if ($method->getDocblock()) { $writer->writeln($method->getDocblock()); } if ($method->isFinal()) { $writer->write('final '); } $writer->write($method->getVisibility())->write(' function ')->write($method->getName()); $ps = $method->getParameters(); if (empty($ps)) { $writer->write('()'); } else { $writer->write('('); foreach ($method->getParameters() as $i => $p) { if ($p->getType()) { if (in_array($p->getType(), array('mixed'))) { $writer->write('/** @var ' . $p->getType() . ' */ '); } else { $writer->write($p->getType() . ' '); } } if ($p->isPassedByReference()) { $writer->write(' & '); } $writer->write('$')->write($p->getName()); if ($p->hasDefaultValue()) { $writer->write(' = ' . json_encode($p->getDefaultValue())); } if ($i < count($ps) - 1) { $writer->write(", "); } } $writer->write(')'); } $writer->writeln('{')->indent()->write($_body)->outdent()->writeln("}"); } $writer->outdent()->writeln('}'); $content = PhpHelper::decompilePhpCode($writer->getContent()); $_class_dir = dirname($_class_file); if (!file_exists($_class_dir)) { if (!@mkdir($_class_dir, 0755, true)) { throw new \Exception(sprintf("mkdir(%s) error!", $_class_dir)); } } PhpHelper::write_file($_class_file, $content); return $_class_file; }
protected function getClassName($name) { if (!isset($this->_classNameCache[$name])) { $this->_classNameCache[$name] = sprintf('Symforce\\Event\\%s', \Symforce\CoreBundle\PhpHelper\PhpHelper::camelize($name)); } return $this->_classNameCache[$name]; }