/** * Creates a new DataModel subclass object and * if a DataAccessInterface implementation is * passed in it sets it as the dao property * * You may not want a dao associated with this * DataModel if it is just being used as bean * or model object for the view. * * @param DataAccessInterface $dao */ public function __construct(DataAccessInterface $dao = null) { $this->init(); // a hook to the constructor $this->className = get_class($this); if (is_null($this->_table)) { $info = AnnotationsParser::getAnnotations(new ReflectionClass($this->className)); if (isset($info['table'])) { $this->_table = $info['table']; } } if ($dao) { $this->setDAO($dao); } }
/** * fetches a collection of the datamodel from the db * * @param DataModel $o * @return DataModelIterator */ public function fetch(DataModel $o) { $columns = array(); $values = array(); $where = ""; $objectProperties = array(); $class = new ReflectionClass($o); $properties = $class->getProperties(ReflectionProperty::IS_PUBLIC); foreach ($properties as $property) { if ($property->getDeclaringClass() == $class) { $propertyName = $property->getName(); if (isset($o->{$propertyName})) { if (!is_object($o->{$propertyName})) { array_push($columns, $propertyName); array_push($values, $o->{$propertyName}); } else { $objectProperties[] = $property; } } } } $select = "SELECT * FROM {$this->quote()}{$o->getTableName()}{$this->quote()}"; if (count($columns) == 0) { $where = "LIMIT 1000"; $values = null; } else { $where = "WHERE {$this->quote()}" . implode("{$this->quote()} = ? AND {$this->quote()}", $columns) . "{$this->quote()} = ?"; } $sql = "{$select} {$where}"; $results = $this->query($sql, $values, $o); foreach ($objectProperties as $property) { foreach ($results as $o) { $propertyName = $property->getName(); $info = AnnotationsParser::getAnnotations($property); if (array_key_exists('model', $info)) { $keys = $this->setKeys($o); foreach ($keys['PRI'] as $key) { if (property_exists($o->{$propertyName}, $key) && isset($o->{$key})) { $o->{$propertyName}->{$key} = $o->{$key}; } } if ($a = $o->{$propertyName}->fetch()) { if ($z = $a->current()) { $o->{$propertyName} = $z; } } } } } $results->rewind(); return $results; }
/** * applies the advice after the method * * @param ReflectionClass $class * @param ReflectionMethod $method * @param ReflectionClass $adviceClass * @throws Exception */ private function applyAfterAdvice(ReflectionClass $class, ReflectionMethod $method, ReflectionClass $adviceClass) { $adviceMethods = array(); $methods = $adviceClass->getMethods(); foreach ($methods as $advice) { $name = $advice->getName(); $info = AnnotationsParser::getAnnotations($advice); // preg match if (isset($info['match']) && preg_match("/" . strtolower($info['match']) . "/", strtolower(Aspect::POINTCUT_AFTER . $this->className . $method->getName()))) { $adviceMethods[] = $advice; } // exact match if ($name == Aspect::POINTCUT_AFTER . $this->className . $method->getName()) { $adviceMethods[] = $advice; } } foreach ($adviceMethods as $adviceMethod) { $adviceCode = $this->getAdviceCode($adviceClass, $adviceMethod); if ($method->getDeclaringClass()->getName() != $class->getName() && !in_array($method->getName(), $this->adviceMethodNames)) { // and not already override generated $signature = $this->generateMethodSignature($method); $invoke = $this->generateMethodInvoking($method); $random = "var" . md5(rand()); $adviceCode = array($signature . "\n", "\${$random} = parent::{$invoke}\n", $adviceCode, "return \${$random};\n", "}\n", "\n"); $index = count($this->classCodeArray) - 2; array_push($this->adviceMethodNames, $method->getName()); foreach ($adviceCode as $code) { $this->injectCode($code, $index); $index++; } } else { $index = $this->getAfterIndex($method->getName()); } $this->injectCode($adviceCode, $index); } }
/** * aspects the instance as per is has annotations * * @param string $classname * @return Aspect */ private function getAspectedInstance($classname) { // get the aspects this object has $c = new ReflectionClass($classname); $info = AnnotationsParser::getAnnotations($c); $aspects = null; if (array_key_exists('has', $info)) { $aspects = explode(",", $info['has']); foreach ($aspects as &$aspect) { $aspectclass = trim($aspect) . "Aspect"; $aspect = new $aspectclass(); } } return Aspect::createInstance()->setClassName($classname)->setAdvice($aspects)->compile(); }