/** * Call to undefined method. * @param string method name * @param array arguments * @return mixed * @throws \MemberAccessException */ public static function call($_this, $name, $args) { $class = new Nette\Reflection\ClassReflection($_this); if ($name === '') { throw new \MemberAccessException("Call to class '$class->name' method without name."); } // event functionality if ($class->hasEventProperty($name)) { if (is_array($list = $_this->$name) || $list instanceof \Traversable) { foreach ($list as $handler) { callback($handler)->invokeArgs($args); } } return NULL; } // extension methods if ($cb = $class->getExtensionMethod($name)) { array_unshift($args, $_this); return $cb->invokeArgs($args); } throw new \MemberAccessException("Call to undefined method $class->name::$name()."); }
public function getPresenterClass(&$name) { if (isset($this->cache[$name])) { list($className, $name) = $this->cache[$name]; return $className; } if (!is_string($name) || !String::match($name, "#^[a-zA-Z-ÿ][a-zA-Z0-9-ÿ:]*\$#")) { throw new InvalidPresenterException("Presenter name must be alphanumeric string, '{$name}' is invalid."); } $classNameBase = str_replace(':', 'Module\\', $name) . 'Presenter'; $classNames = array_map(function ($namespace) use($classNameBase) { return ($namespace ? $namespace . "\\" : "") . $classNameBase; }, $this->namespaces); foreach ($classNames as $className) { if (!class_exists($className)) { continue; } $reflection = new ClassReflection($className); if (!$reflection->implementsInterface('Nette\\Application\\IPresenter')) { throw new InvalidPresenterException("Cannot load presenter '{$name}', class '{$className}' is not Nette\\Application\\IPresenter implementor."); } if ($reflection->isAbstract()) { throw new InvalidPresenterException("Cannot load presenter '{$name}', class '{$className}' is abstract."); } return $className; } throw new InvalidPresenterException("Cannot load presenter {$name}, class " . implode(" nor ", $classNames) . " does not exist"); }
/** * @param string presenter name * @return string class name * @throws InvalidPresenterException */ public function getPresenterClass(& $name) { if (isset($this->cache[$name])) { list($class, $name) = $this->cache[$name]; return $class; } if (!is_string($name) || !Nette\String::match($name, "#^[a-zA-Z\x7f-\xff][a-zA-Z0-9\x7f-\xff:]*$#")) { throw new InvalidPresenterException("Presenter name must be alphanumeric string, '$name' is invalid."); } $class = $this->formatPresenterClass($name); if (!class_exists($class)) { // internal autoloading $file = $this->formatPresenterFile($name); if (is_file($file) && is_readable($file)) { Nette\Loaders\LimitedScope::load($file); } if (!class_exists($class)) { throw new InvalidPresenterException("Cannot load presenter '$name', class '$class' was not found in '$file'."); } } $reflection = new Nette\Reflection\ClassReflection($class); $class = $reflection->getName(); if (!$reflection->implementsInterface('Nette\Application\IPresenter')) { throw new InvalidPresenterException("Cannot load presenter '$name', class '$class' is not Nette\\Application\\IPresenter implementor."); } if ($reflection->isAbstract()) { throw new InvalidPresenterException("Cannot load presenter '$name', class '$class' is abstract."); } // canonicalize presenter name $realName = $this->unformatPresenterClass($class); if ($name !== $realName) { if ($this->caseSensitive) { throw new InvalidPresenterException("Cannot load presenter '$name', case mismatch. Real name is '$realName'."); } else { $this->cache[$name] = array($class, $realName); $name = $realName; } } else { $this->cache[$name] = array($class, $realName); } return $class; }
/** * Sets a variable in this session namespace. * @param string name * @param mixed value * @return void */ public function __set($name, $value) { $this->data[$name] = $value; if (is_object($value)) { $this->meta[$name]['V'] = Nette\Reflection\ClassReflection::from($value)->getAnnotation('serializationVersion'); } }
/** * Provides write access to ExtensibleObject's properties. * @param string $name * @param mixed $value */ public function __set($name, $value) { if ($this->objectReflection->hasProperty($name) && ($property = $this->objectReflection->getProperty($name)) && $property->isProtected()) { $property->setAccessible(TRUE); $property->setValue($this->object, $value); } else { $this->object->{$name} = $value; } }
/** * Registers adapter for given file extension. * @param string file extension * @param string class name (IConfigAdapter) * @return void */ public static function registerExtension($extension, $class) { if (!class_exists($class)) { throw new \InvalidArgumentException("Class '{$class}' was not found."); } if (!Nette\Reflection\ClassReflection::from($class)->implementsInterface('Nette\\Config\\IConfigAdapter')) { throw new \InvalidArgumentException("Configuration adapter '{$class}' is not Nette\\Config\\IConfigAdapter implementor."); } self::$extensions[strtolower($extension)] = $class; }
/** * Returns annotations. * @param \ReflectionClass|\ReflectionMethod|\ReflectionProperty * @return array */ public static function getAll(\Reflector $r) { if ($r instanceof \ReflectionClass) { $type = $r->getName(); $member = ''; } elseif ($r instanceof \ReflectionMethod) { $type = $r->getDeclaringClass()->getName(); $member = $r->getName(); } else { $type = $r->getDeclaringClass()->getName(); $member = '$' . $r->getName(); } if (!self::$useReflection) { // auto-expire cache $file = $r instanceof \ReflectionClass ? $r->getFileName() : $r->getDeclaringClass()->getFileName(); // will be used later if ($file && isset(self::$timestamps[$file]) && self::$timestamps[$file] !== filemtime($file)) { unset(self::$cache[$type]); } unset(self::$timestamps[$file]); } if (isset(self::$cache[$type][$member])) { // is value cached? return self::$cache[$type][$member]; } if (self::$useReflection === NULL) { // detects whether is reflection available self::$useReflection = (bool) Nette\Reflection\ClassReflection::from(__CLASS__)->getDocComment(); } if (self::$useReflection) { return self::$cache[$type][$member] = self::parseComment($r->getDocComment()); } else { if (self::$cache === NULL) { self::$cache = (array) self::getCache()->offsetGet('list'); self::$timestamps = isset(self::$cache['*']) ? self::$cache['*'] : array(); } if (!isset(self::$cache[$type]) && $file) { self::$cache['*'][$file] = filemtime($file); self::parseScript($file); self::getCache()->save('list', self::$cache); } if (isset(self::$cache[$type][$member])) { return self::$cache[$type][$member]; } else { return self::$cache[$type][$member] = array(); } } }
public function universalFactory($options) { $arguments = isset($options["arguments"]) ? $this->processArguments($options["arguments"], $options["context"]) : array(); if (isset($options["class"])) { if (!empty($arguments)) { $object = ClassReflection::from($options["class"])->newInstanceArgs($arguments); } else { $class = $options["class"]; $object = new $class(); } } if (isset($options["factory"])) { $object = call_user_func_array($options["factory"], $arguments); } if (isset($options["callMethods"])) { foreach ($options["callMethods"] as $method => $args) { call_user_func_array(array($object, $method), $this->processArguments($args, $options["context"])); } } return $object; }
protected function execute(InputInterface $input, OutputInterface $output) { $name = ClassReflection::from($input->getOption("entity"))->getShortName(); $dir = $input->getOption("output") ?: APP_DIR . "/AdminModule/forms/{$name}"; $em = $this->getHelper("em")->getEntityManager(); /* @var $em \Doctrine\ORM\EntityManager */ $config = $em->getClassMetadata($input->getOption("entity")); $pk = $config->getSingleIdentifierFieldName(); foreach ($config->getColumnNames() as $column) { $fieldName = $config->getFieldName($column); if ($fieldName === $pk) { continue; } $maping = $config->getFieldMapping($fieldName); $field = (object) array("name" => $fieldName); if ($maping["type"] === "text") { $field->inputName = "TextArea"; } elseif ($maping["type"] === "boolean") { $field->inputName = "Checkbox"; } else { $field->inputName = "Text"; } $fields[] = $field; } foreach ($config->getAssociationMappings() as $maping) { $field = (object) null; $field->name = $maping["fieldName"]; $field->inputName = "Select"; $fields[] = $field; } $template = $this->createTemplate()->setFile(__DIR__ . "/formClass.phtml"); $template->name = $name; $template->fields = $fields; $this->tryWriteFile($dir . '/' . $name . 'Form.php', $template, $output); $template = $this->createTemplate()->setFile(__DIR__ . "/formTemplate.phtml"); $template->fields = $fields; $this->tryWriteFile($dir . '/' . $name . 'Form.phtml', $template, $output); }
/** * Checks object @serializationVersion label. * @param string * @param mixed * @return bool */ private static function checkSerializationVersion($class, $value) { return Nette\Reflection\ClassReflection::from($class)->getAnnotation('serializationVersion') === $value; }
/** * Returns array of persistent components. * This default implementation detects components by class-level annotation @persistent(cmp1, cmp2). * @return array */ public static function getPersistentComponents() { return (array) Nette\Reflection\ClassReflection::from(get_called_class())->getAnnotation('persistent'); }
/** * Load associations */ protected function loadAssociations() { $annotations = ClassReflection::from($this->rowClass)->getAnnotations(); foreach ($annotations as $k => $v) { if ($v[0] instanceof \Ormion\Association\IAssociation) { foreach ($v as $association) { $association->setMapper($this); $this->associations[$association->getName()] = $association; } } } }
/** * Get entity instance with default data * * @param ActiveMapper\Manager $em * @param array $data * @return mixed */ public function getInstance(Manager $em, $data) { if (!is_array($data) && !$data instanceof \ArrayAccess) { throw new \InvalidArgumentException("Get instance data must be array or implement ArrayAccess."); } $ref = ClassReflection::from($this->entity); if ($this->hasProxy()) { $instance = $ref->newInstance($data); } else { $instance = $ref->newInstance(); foreach ($this->columns as $column) { $tmpName = Tools::underscore($column->name); if (isset($data[$tmpName])) { $prop = $ref->getProperty($column->name); $prop->setAccessible(TRUE); $prop->setValue($instance, $column->convertToPHPValue($data[$tmpName])); $prop->setAccessible(FALSE); } } } if (count($this->getAssociations()) > 0) { $propRef = $ref->getProperty('_associations'); $propRef->setAccessible(TRUE); $associations = $propRef->getValue($instance); foreach ($this->getAssociations() as $assoc) { $associations[$assoc->name] = new Associations\LazyLoad($em, $this->entity, $assoc->name, $data); } $propRef->setValue($instance, $associations); $propRef->setAccessible(FALSE); } return $instance; }
/** * Returns array of persistent components. * This default implementation detects components by class-level annotation @persistent(cmp1, cmp2). * @return array */ public static function getPersistentComponents() { return (array) Nette\Reflection\ClassReflection::from(/*5.2*func_get_arg(0)*//**/get_called_class()/**/)->getAnnotation('persistent'); }
/** * @return Nette\Reflection\ClassReflection */ public function getDeclaringClass() { return ClassReflection::import(parent::getDeclaringClass()); }
/** * Returns array of classes persistent parameters. They have public visibility and are non-static. * This default implementation detects persistent parameters by annotation @persistent. * @return array */ public static function getPersistentParams() { $rc = new Nette\Reflection\ClassReflection(get_called_class()); $params = array(); foreach ($rc->getProperties(\ReflectionProperty::IS_PUBLIC) as $rp) { if (!$rp->isStatic() && $rp->hasAnnotation('persistent')) { $params[] = $rp->getName(); } } return $params; }
/** * Adding method to class. * @param string method name * @param mixed callback or closure * @return mixed */ public static function extensionMethod($name, $callback = NULL) { if (strpos($name, '::') === FALSE) { $class = get_called_class(); } else { list($class, $name) = explode('::', $name); } $class = new Nette\Reflection\ClassReflection($class); if ($callback === NULL) { return $class->getExtensionMethod($name); } else { $class->setExtensionMethod($name, $callback); } }
/** * Starts and initializes session data. * @throws \InvalidStateException * @return void */ public function start() { if (self::$started) { throw new \InvalidStateException('Session has already been started.'); } elseif (self::$started === NULL && defined('SID')) { throw new \InvalidStateException('A session had already been started by session.auto-start or session_start().'); } // start session try { $this->configure($this->options); } catch (\NotSupportedException $e) { // ignore? } Nette\Tools::tryError(); session_start(); if (Nette\Tools::catchError($msg)) { @session_write_close(); // this is needed throw new \InvalidStateException($msg); } self::$started = TRUE; if ($this->regenerationNeeded) { session_regenerate_id(TRUE); $this->regenerationNeeded = FALSE; } /* structure: __NF: Counter, BrowserKey, Data, Meta DATA: namespace->variable = data META: namespace->variable = Timestamp, Browser, Version */ unset($_SESSION['__NT'], $_SESSION['__NS'], $_SESSION['__NM']); // old unused structures // initialize structures $nf =& $_SESSION['__NF']; if (empty($nf)) { // new session $nf = array('C' => 0); } else { $nf['C']++; } // browser closing detection $browserKey = $this->getHttpRequest()->getCookie('nette-browser'); if (!$browserKey) { $browserKey = (string) lcg_value(); } $browserClosed = !isset($nf['B']) || $nf['B'] !== $browserKey; $nf['B'] = $browserKey; // resend cookie $this->sendCookie(); // process meta metadata if (isset($nf['META'])) { $now = time(); // expire namespace variables foreach ($nf['META'] as $namespace => $metadata) { if (is_array($metadata)) { foreach ($metadata as $variable => $value) { if (!empty($value['B']) && $browserClosed || !empty($value['T']) && $now > $value['T'] || $variable !== '' && is_object($nf['DATA'][$namespace][$variable]) && (isset($value['V']) ? $value['V'] : NULL) !== Nette\Reflection\ClassReflection::from($nf['DATA'][$namespace][$variable])->getAnnotation('serializationVersion')) { if ($variable === '') { // expire whole namespace unset($nf['META'][$namespace], $nf['DATA'][$namespace]); continue 2; } unset($nf['META'][$namespace][$variable], $nf['DATA'][$namespace][$variable]); } } } } } register_shutdown_function(array($this, 'clean')); }
/** * @return Nette\Reflection\ClassReflection */ public function getDeclaringClass() { return ($ref = parent::getDeclaringClass()) ? ClassReflection::import($ref) : NULL; }