public function testReflectionAnnotatedClass() { $reflection = new ReflectionAnnotatedClass('Example'); $this->assertTrue($reflection->hasAnnotation('FirstAnnotation')); $this->assertTrue($reflection->hasAnnotation('SecondAnnotation')); $this->assertFalse($reflection->hasAnnotation('NonExistentAnnotation')); $this->assertIsA($reflection->getAnnotation('FirstAnnotation'), 'FirstAnnotation'); $this->assertIsA($reflection->getAnnotation('SecondAnnotation'), 'SecondAnnotation'); $annotations = $reflection->getAnnotations(); $this->assertEqual(count($annotations), 2); $this->assertIsA($annotations[0], 'FirstAnnotation'); $this->assertIsA($annotations[1], 'SecondAnnotation'); $this->assertFalse($reflection->getAnnotation('NonExistentAnnotation')); $this->assertIsA($reflection->getConstructor(), 'ReflectionAnnotatedMethod'); $this->assertIsA($reflection->getMethod('exampleMethod'), 'ReflectionAnnotatedMethod'); foreach ($reflection->getMethods() as $method) { $this->assertIsA($method, 'ReflectionAnnotatedMethod'); } $this->assertIsA($reflection->getProperty('exampleProperty'), 'ReflectionAnnotatedProperty'); foreach ($reflection->getProperties() as $property) { $this->assertIsA($property, 'ReflectionAnnotatedProperty'); } foreach ($reflection->getInterfaces() as $interface) { $this->assertIsA($interface, 'ReflectionAnnotatedClass'); } $this->assertIsA($reflection->getParentClass(), 'ReflectionAnnotatedClass'); }
private function checkTargetConstraints($target) { $reflection = new ReflectionAnnotatedClass($this); if ($reflection->hasAnnotation('Target')) { $value = $reflection->getAnnotation('Target')->value; $values = is_array($value) ? $value : array($value); foreach ($values as $value) { if ($value == 'class' && $target instanceof ReflectionClass) { return; } if ($value == 'method' && $target instanceof ReflectionMethod) { return; } if ($value == 'property' && $target instanceof ReflectionProperty) { return; } if ($value == 'nested' && $target === false) { return; } } if ($target === false) { trigger_error("Annotation '" . get_class($this) . "' nesting not allowed", E_USER_ERROR); } else { trigger_error("Annotation '" . get_class($this) . "' not allowed on " . $this->createName($target), E_USER_ERROR); } } }
/** * Returns an associative array of classes known to extend Node. * Pulls this array from cache if available - if not, generates * and caches it. * * Class names are keyed on their machine name. * * @access private * @static * @return array */ private static function init_registry() { $cache = cache_get(self::DOODAL_CACHE_ID); ## ## Attempt to get registry from cache if ($cache && !empty($cache->data)) { return $cache->data; } else { ## ## Load all classes registered for autoloading not in an ignored (i.e. system) module $results = db_query("SELECT name FROM {registry} WHERE type = 'class' AND module != '' AND module NOT IN (:modules) ORDER BY name", array(':modules' => self::get_ignored_modules()))->fetchAll(); ## ## Get subset of classes marked as "node" $registry = array(); foreach ($results as $result) { $reflectedClass = new ReflectionAnnotatedClass($result->name); if ($reflectedClass->hasAnnotation('MachineName')) { $registry[$reflectedClass->getAnnotation('MachineName')->value] = $result->name; } } ## ## Cache results and return cache_set(self::DOODAL_CACHE_ID, $registry, 'cache', CACHE_PERMANENT); return $registry; } }
public function loadResourceMap($classes) { $resources = array(); foreach ($classes as $class) { $reflection = new \ReflectionAnnotatedClass($class); $name = ""; if ($reflection->hasAnnotation("Service")) { $annotation = $reflection->getAnnotation('Service'); $resources[$this->getResourceName($annotation, $class)] = $reflection->getName(); } if ($reflection->hasAnnotation("Repository")) { $annotation = $reflection->getAnnotation('Repository'); $resources[$this->getResourceName($annotation, $class)] = $reflection->getName(); } } return $resources; }
/** * Gets the ckAbstractPropertyStrategy implementation for a given class from the PropertyStrategy annotation, * if no annotation is found ckDefaultPropertyStrategy is returned. * * @param ReflectionAnnotatedClass $class A ReflectionAnnotatedClass object * * @return ckAbstractPropertyStrategy The ckAbstractPropertyStrategy implementation */ public static function getPropertyStrategy(ReflectionAnnotatedClass $class) { $strategy = null; if ($class->hasAnnotation('PropertyStrategy')) { $strategy = $class->getAnnotation('PropertyStrategy')->value; $strategy = new $strategy($class); } if (is_null($strategy) || !$strategy instanceof ckAbstractPropertyStrategy) { $strategy = new ckDefaultPropertyStrategy($class); } return $strategy; }
/** * Loads current action according TlalokesRequest or default configuration * * @author Basilio Briceno <*****@*****.**> * @param array $conf * @param TlalokesRequest $request */ function tlalokes_core_conf_get_action(&$conf, TlalokesRequest &$request) { require_once 'ReflectionAnnotatedClass.php'; require_once 'ControllerDefinition.php'; require_once 'ActionDefinition.php'; // reflect Annotations from current controller class $rc = new ReflectionAnnotatedClass($conf['current']['controller']); // try to find the @ControllerDefinition if (!$rc->hasAnnotation('ControllerDefinition')) { tlalokes_error_msg('Define annotation @ControllerDefinition in ' . $conf['current']['controller']); } // try to find the default action property in @ControllerDefinition if (!($default = $rc->getAnnotation('ControllerDefinition')->default)) { tlalokes_error_msg('Define a default action in @ControllerDefinition in ' . $conf['current']['controller']); } // set the current action from TlalokesRequest or default if not found $conf['current']['action'] = !$rc->hasMethod($request->_action) ? $default : $request->_action; return $conf; }
public function testIgnoredAnnotationsAreNotUsed() { Addendum::ignore('FirstAnnotation', 'SecondAnnotation'); $reflection = new ReflectionAnnotatedClass('Example'); $this->assertFalse($reflection->hasAnnotation('FirstAnnotation')); $this->assertFalse($reflection->hasAnnotation('SecondAnnotation')); }
/** * Creates a schema.xml file based in Database Definition objects * * @param TlalokesRegistry $reg */ private static function buildSchemaFromDefs(TlalokesRegistry &$reg) { // static flags static $uniques = 0; static $indexes = 0; // build database xml dom object $dom = new DOMDocument('1.0', 'utf-8'); $dom->formatOutput = true; $db = $dom->appendChild(new DOMElement('database')); $db->setAttribute('name', $reg->conf['dsn']['name']); // find table definitions foreach (glob($reg->conf['path']['app'] . $reg->conf['path']['def'] . '*Def.php') as $def) { // get class name $class_name = preg_replace('/.*\\/(\\w*Def).php$/', '$1', $def); // reflect annotated class $ref = new ReflectionAnnotatedClass($class_name); // check if @DefinitonObject is set if (!$ref->hasAnnotation('DefinitionObject')) { tlalokes_error_msg('PropelFactory: There is no DefinitionObject in ' . $class_name); } // check if object is marked for build if ($ref->getAnnotation('DefinitionObject')->build) { // build xml table node $table = $db->appendChild(new DOMElement('table')); // set table name attribute $table_name = $ref->getAnnotation('DefinitionObject')->table; $table->setAttribute('name', $table_name); unset($table_name); $table->setAttribute('idMethod', 'native'); // find columns foreach ($ref->getProperties() as $property) { // reflect column $column = $property->getAnnotation('DefinitionObject'); // build xml column nodes $col = $table->appendChild(new DOMElement('column')); // name $col->setAttribute('name', $column->column); // phpName if ($property->getName() != $column->column) { $col->setAttribute('phpName', (string) $property->getName()); } // type $col->setAttribute('type', $column->type); // size if ($column->size) { $col->setAttribute('size', $column->size); } // scale if ($column->scale) { $col->setAttribute('scale', $column->scale); } // required if ($column->required) { $col->setAttribute('required', $column->required ? 'true' : 'false'); } // autoIncrement if ($column->autoIncrement) { $col->setAttribute('autoIncrement', $column->autoIncrement ? 'true' : 'false'); // If RBDMS is PgSQL and there is no default set the default // WARNING: It needs to be tested with Oracle if ($reg->conf['dsn']['type'] == 'pgsql') { // build id-method-parameter $imp_name = 'id-method-parameter'; $imp = $table->appendChild(new DOMElement($imp_name)); $imp->setAttribute('value', $table_name . '_seq'); // set default if (!isset($column->default)) { $col->setAttribute('default', 'nextval(\'' . $table_name . '_seq\'::regclass)'); } } } // primaryKey if ($column->primaryKey) { $col->setAttribute('primaryKey', $column->primaryKey ? 'true' : 'false'); } // default if ($column->default || $column->default === 0) { $col->setAttribute('default', tlalokes_core_get_type($column->default)); } // find unique if (isset($column->unique) && $column->unique) { if (isset($uniques)) { $uniques++; } else { $uniques = 1; } $unique_column_name[] = $column->column; } // find index if (isset($column->index) && $column->index) { $indexes = isset($indexes) ? $indexes + 1 : 1; $index_column_name[] = $column->column; } // find reference $reference = $property->getAnnotation('ReferenceDef'); if ($reference) { // build foreign-key xml node $fk = $table->appendChild(new DOMElement('foreign-key')); $fk->setAttribute('foreignTable', $reference->table); $fk->setAttribute('onDelete', strtolower($reference->onDelete)); $fk->setAttribute('onUpdate', strtolower($reference->onUpdate)); $rf = $fk->appendChild(new DOMElement('reference')); $rf->setAttribute('local', $column->column); $rf->setAttribute('foreign', $reference->column); } } // find uniques flag if (isset($uniques) && $uniques >= 1) { if (isset($unique_column_name)) { foreach ($unique_column_name as $ucn) { // build unique xml node $unique = $table->appendChild(new DOMElement('unique')); // build unique-column xml node $uc = $unique->appendChild(new DOMElement('unique-column')); $uc->setAttribute('name', $ucn); } unset($unique_column_name); } unset($uniques); } // find indexes flag if (isset($indexes) && $indexes >= 1) { foreach ($index_column_name as $icn) { // build index xml node $index = $table->appendChild(new DOMElement('index')); // build index-column xml node $ic = $index->appendChild(new DOMElement('index-column')); $ic->setAttribute('name', $icn); } unset($indexes); unset($index_column_name); } } // set file path to schema.xml $file = $reg->conf['path']['app'] . $reg->conf['path']['tmp'] . 'generator/schema.xml'; // write schema.xml file or return a CoreException if (!@file_put_contents($file, $dom->saveXML())) { tlalokes_error_msg('Propel: Cannot write database schema', true); } } }
/** * Checks if web services are declares in the controller and loads them * * @author Basilio Briceno <*****@*****.**> * @param TlalokesRegistry $reg */ function tlalokes_receiver_webservices(&$reg) { // reflect Annotations in method require 'ReflectionAnnotatedClass.php'; $ref = new ReflectionAnnotatedClass($reg->conf['current']['controller']); //require 'ControllerDefinition.php'; if ($ref->hasAnnotation('ControllerDefinition')) { // JSON if ($ref->getAnnotation('ControllerDefinition')->json) { // check request if ($_SERVER['REQUEST_METHOD'] == 'POST') { var_dump($_POST); $reg->webservice = true; $obj = new $reg->conf['current']['controller']($reg); unset($reg); echo json_encode($obj->response); exit; } } // JSON-RPC if ($ref->getAnnotation('ControllerDefinition')->jsonrpc) { require 'tlalokes_jsonrpc.php'; // check request if (tlalokes_jsonrpc_server_check()) { $reg->json = true; $obj = new $reg->conf['current']['controller']($reg); unset($reg); tlalokes_jsonrpc_server_handle($obj); } } // SOAP if ($ref->getAnnotation('ControllerDefinition')->soap) { if ($_SERVER['REQUEST_METHOD'] == 'POST' || $_SERVER['CONTENT_TYPE'] == 'application/soap+xml') { // set URI for the service $uri = 'http://' . $_SERVER['HTTP_HOST'] . $reg->conf->path->uri . preg_replace('/^\\/(.*).php/', '$1', $_SERVER['SCRIPT_NAME']) . '/' . $reg->conf['current']['controller']; // set service and handle it $server = new SoapServer(null, array('uri' => $uri)); $server->setClass($reg->conf['current']['controller'] . 'Ctl', $reg); $server->handle(); unset($ref); exit; } } } }
/** * {@inheritdoc} */ public function matches($className, \ReflectionAnnotatedClass $reflection) { return $reflection->hasAnnotation($this->annotation); }
public function testClassAndAnnotationInNamespaces() { $reflection = new ReflectionAnnotatedClass('Example\\Example'); $this->assertTrue($reflection->hasAnnotation('Example\\Annotation\\ExampleAnnotation')); }
public function parse() { $map = array(); $map['time'] = $this->getModificationTime(); $map['fields'] = array(); $map['relations'] = array(); // pegando as anotacoes da classe $ref = new ReflectionAnnotatedClass($this->getClassname()); if (!$ref->hasAnnotation('LumineEntity')) { return; } $map['package'] = $ref->getAnnotation('LumineEntity')->package; if ($ref->hasAnnotation('LumineTable')) { $map['tablename'] = $ref->getAnnotation('LumineTable')->name; } else { $map['tablename'] = strtolower($this->getClassname()); } // pegando as propriedades $props = $ref->getProperties(); /** @var $prop ReflectionAnnotatedProperty */ foreach ($props as $prop) { if (!$prop->hasAnnotation('LumineTransient')) { if ($prop->hasAnnotation('LumineColumn')) { $anno = $prop->getAnnotation('LumineColumn'); if ($prop->hasAnnotation('LumineId')) { $anno->options['primary'] = true; } if ($prop->hasAnnotation('LumineManyToOne')) { $mto = $prop->getAnnotation('LumineManyToOne'); $anno->options['class'] = $mto->class; $anno->options['linkOn'] = $mto->linkOn; $anno->options['onUpdate'] = $mto->onUpdate; $anno->options['onDelete'] = $mto->onDelete; $anno->options['lazy'] = $mto->lazy; $anno->options['foreign'] = true; } if (empty($anno->name)) { $anno->name = $prop->getName(); } if (empty($anno->column)) { $anno->column = $prop->getName(); } $map['fields'][] = array($anno->name, $anno->column, $anno->type, $anno->length, empty($anno->options) ? array() : $anno->options); } else { if ($prop->hasAnnotation('LumineManyToMany')) { $anno = $prop->getAnnotation('LumineManyToMany'); if (empty($anno->name)) { $anno->name = $prop->getName(); } $map['relations'][] = array($anno->name, Lumine_Metadata::MANY_TO_MANY, $anno->class, $anno->linkOn, $anno->table, $anno->column, $anno->lazy); } else { if ($prop->hasAnnotation('LumineOneToMany')) { $anno = $prop->getAnnotation('LumineOneToMany'); if (empty($anno->name)) { $anno->name = $prop->getName(); } $map['relations'][] = array($anno->name, Lumine_Metadata::ONE_TO_MANY, $anno->class, $anno->linkOn, null, null, $anno->lazy); } else { if ($prop->getDeclaringClass()->getName() == $this->getClassname()) { $map['fields'][] = array($prop->getName(), $prop->getName(), 'varchar', 255, array()); } } } } } } return $map; }
public static function getNewInstancesInDir($path) { $original = get_declared_classes(); if ($dir = dir($path)) { while (false !== ($file = $dir->read())) { if (!is_dir($path . '/' . $file) && preg_match("/\\.php\$/i", $file)) { require_once "{$path}/{$file}"; } } $dir->close(); } $new = array_diff(get_declared_classes(), $original); $newInstances = array(); foreach ($new as $className) { $reflection = new ReflectionAnnotatedClass($className); if ($reflection->hasAnnotation('Instance')) { if ($reflection->getAnnotation('Instance')->value != '') { $name = $reflection->getAnnotation('Instance')->value; } else { $name = $className; } $newInstances[$name] = $className; } } return $newInstances; }
/** * If a child class is properly annotated with its machine name, * returns that. Throws an exception if not able to retrieve * a valid annotation. * * @access public * @static * @return string * @throws MissingNodeAnnotationException if the @MachineName annotation is improperly defined. */ public static function get_machine_name() { ## ## Get annotated machine name of class $reflectedClass = new ReflectionAnnotatedClass(get_called_class()); if ($reflectedClass->hasAnnotation('MachineName') && strlen($reflectedClass->getAnnotation('MachineName')->value)) { return $reflectedClass->getAnnotation('MachineName')->value; } else { throw new MissingNodeAnnotationException('Missing @MachineName annotation in class: ' . get_called_class()); } }