/** * @param Callback * @param string * @return Callback */ public static function create(Callback $callback, $className) { $factory = new self(); $factory->callback = $callback->getNative(); $factory->className = $className; return Callback::create($factory, 'call'); }
/** * @return \Venne\Security\Login\LoginControl */ protected function createComponentSignInForm() { $form = $this->loginControlFactory->create(); $form->onSuccess[] = $this->formSuccess; $form->onError[] = $this->formError; return $form; }
/** * Logs message or exception to file and sends email notification. * @param string|array * @param int one of constant INFO, WARNING, ERROR (sends email), CRITICAL (sends email) * @return bool was successful? */ public function log($message, $priority = self::INFO) { if (!is_dir($this->directory)) { throw new DirectoryNotFoundException("Directory '{$this->directory}' is not found or is not directory."); } if (is_array($message)) { $message = implode(' ', $message); } $res = error_log(trim($message) . PHP_EOL, 3, $this->directory . '/' . strtolower($priority) . '.log'); if (($priority === self::ERROR || $priority === self::CRITICAL) && $this->email && $this->mailer && @filemtime($this->directory . '/email-sent') + self::$emailSnooze < time() && @file_put_contents($this->directory . '/email-sent', 'sent')) { Callback::create($this->mailer)->invoke($message, $this->email); } return $res; }
/** @param int */ private function handleLazy($key) { $object = call_user_func($this->lazy[$key][0]); if (!$object instanceof IListener) { $cb = Callback::create($this->lazy[$key][0]); throw new BadReturnException(array(NULL, __CLASS__ . " lazy factory {$cb}()", 'Orm\\IListener', $object)); } $types = $this->lazy[$key][1]; foreach (self::$instructions as $e => $m) { if (isset($types[$e])) { if ($object instanceof $m[0]) { $this->listeners[$e][$types[$e]] = array(true, array($object, $m[1])); } else { throw new InvalidArgumentException(ExceptionHelper::format(array($this, Callback::create($this->lazy[$key][0]), $m[0], $object), "%c1 lazy factory %s2() must return %s3; '%v4' given.")); } } else { if ($object instanceof $m[0]) { throw new InvalidArgumentException(ExceptionHelper::format(array($this, Callback::create($this->lazy[$key][0]), $m[0], $object), "%c1 lazy factory %s2() returns not expected %s3; '%v4'.")); } } } unset($this->lazy[$key]); }
/** * Writes item into the cache. * Dependencies are: * - Cache::PRIORITY => (int) priority * - Cache::EXPIRATION => (timestamp) expiration * - Cache::SLIDING => (bool) use sliding expiration? * - Cache::TAGS => (array) tags * - Cache::FILES => (array|string) file names * - Cache::ITEMS => (array|string) cache items * - Cache::CONSTS => (array|string) cache items * * @param mixed key * @param mixed value * @param array dependencies * @return mixed value itself * @throws InvalidArgumentException */ public function save($key, $data, array $dp = NULL) { $this->release(); $key = $this->generateKey($key); if ($data instanceof Callback || $data instanceof Closure) { $this->storage->lock($key); $data = Callback::create($data)->invokeArgs(array(&$dp)); } if ($data === NULL) { $this->storage->remove($key); } else { $this->storage->write($key, $data, $this->completeDependencies($dp, $data)); return $data; } }
/** * __get() implementation. * @param object * @param string property name * @return mixed property value * @throws MemberAccessException if the property is not defined. */ public static function &get($_this, $name) { $class = get_class($_this); $uname = ucfirst($name); if (!isset(self::$methods[$class])) { self::$methods[$class] = array_flip(get_class_methods($class)); // public (static and non-static) methods } if ($name === '') { throw new MemberAccessException("Cannot read a class '{$class}' property without name."); } elseif (isset(self::$methods[$class][$m = 'get' . $uname]) || isset(self::$methods[$class][$m = 'is' . $uname])) { // property getter $val = $_this->{$m}(); return $val; } elseif (isset(self::$methods[$class][$name])) { // public method as closure getter $val = Callback::create($_this, $name); return $val; } else { // strict class $type = isset(self::$methods[$class]['set' . $uname]) ? 'a write-only' : 'an undeclared'; throw new MemberAccessException("Cannot read {$type} property {$class}::\${$name}."); } }
/** * {use class MacroSet} */ public function macroUse(MacroNode $node, PhpWriter $writer) { Callback::create($node->tokenizer->fetchWord(), 'install')->invoke($this->getCompiler())->initialize(); }
/** * Generates code. * @return string */ private function compile(MacroNode $node, $def) { $node->tokenizer->reset(); $writer = PhpWriter::using($node, $this->compiler); if (is_string($def) && substr($def, 0, 1) !== "") { return $writer->write($def); } else { return Callback::create($def)->invoke($node, $writer); } }
/** * Formats PHP code for class instantiating, function calling or property setting in PHP. * @return string * @internal */ public function formatStatement(DIStatement $statement, $self = NULL) { $entity = $this->normalizeEntity($statement->entity); $arguments = $statement->arguments; if (is_string($entity) && Strings::contains($entity, '?')) { // PHP literal return $this->formatPhp($entity, $arguments, $self); } elseif ($service = $this->getServiceName($entity)) { // factory calling or service retrieving if ($this->definitions[$service]->shared) { if ($arguments) { throw new ServiceCreationException("Unable to call service '{$entity}'."); } return $this->formatPhp('$this->getService(?)', array($service)); } $params = array(); foreach ($this->definitions[$service]->parameters as $k => $v) { $params[] = preg_replace('#\\w+$#', '\\$$0', is_int($k) ? $v : $k) . (is_int($k) ? '' : ' = ' . PhpHelpers::dump($v)); } $rm = new FunctionReflection(create_function(implode(', ', $params), '')); $arguments = DIHelpers::autowireArguments($rm, $arguments, $this); return $this->formatPhp('$this->?(?*)', array(DIContainer::getMethodName($service, FALSE), $arguments), $self); } elseif ($entity === 'not') { // operator return $this->formatPhp('!?', array($arguments[0])); } elseif (is_string($entity)) { // class name if ($constructor = ClassReflection::from($entity)->getConstructor()) { $this->addDependency($constructor->getFileName()); $arguments = DIHelpers::autowireArguments($constructor, $arguments, $this); } elseif ($arguments) { throw new ServiceCreationException("Unable to pass arguments, class {$entity} has no constructor."); } return $this->formatPhp("new {$entity}" . ($arguments ? '(?*)' : ''), array($arguments), $self); } elseif (!Validators::isList($entity) || count($entity) !== 2) { throw new InvalidStateException("Expected class, method or property, " . PhpHelpers::dump($entity) . " given."); } elseif ($entity[0] === '') { // globalFunc return $this->formatPhp("{$entity['1']}(?*)", array($arguments), $self); } elseif (Strings::contains($entity[1], '$')) { // property setter Validators::assert($arguments, 'list:1', "setup arguments for '" . Callback::create($entity) . "'"); if ($this->getServiceName($entity[0], $self)) { return $this->formatPhp('?->? = ?', array($entity[0], substr($entity[1], 1), $arguments[0]), $self); } else { return $this->formatPhp($entity[0] . '::$? = ?', array(substr($entity[1], 1), $arguments[0]), $self); } } elseif ($service = $this->getServiceName($entity[0], $self)) { // service method if ($this->definitions[$service]->class) { $arguments = $this->autowireArguments($this->definitions[$service]->class, $entity[1], $arguments); } return $this->formatPhp('?->?(?*)', array($entity[0], $entity[1], $arguments), $self); } else { // static method $arguments = $this->autowireArguments($entity[0], $entity[1], $arguments); return $this->formatPhp("{$entity['0']}::{$entity['1']}(?*)", array($arguments), $self); } }