/** * 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 may not exist && @file_put_contents($this->directory . '/email-sent', 'sent') // @ - file may not be writable ) { NCallback::create($this->mailer)->invoke($message, $this->email); } return $res; }
/** * __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 = NCallback::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(NMacroNode $node, NPhpWriter $writer) { NCallback::create($node->tokenizer->fetchWord(), 'install') ->invoke($this->getCompiler()) ->initialize(); }
/** * Generates code. * @return string */ private function compile(NMacroNode $node, $def) { $node->tokenizer->reset(); $writer = NPhpWriter::using($node, $this->compiler); if (is_string($def)&& substr($def, 0, 1) !== "\0") { return $writer->write($def); } else { return NCallback::create($def)->invoke($node, $writer); } }
/** * Formats PHP code for class instantiating, function calling or property setting in PHP. * @return string * @internal */ public function formatStatement(NDIStatement $statement, $self = NULL) { $entity = $this->normalizeEntity($statement->entity); $arguments = $statement->arguments; if (is_string($entity) && NStrings::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 NServiceCreationException("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+\z#', '\$$0', (is_int($k) ? $v : $k)) . (is_int($k) ? '' : ' = ' . NPhpHelpers::dump($v)); } $rm = new NFunctionReflection(create_function(implode(', ', $params), '')); $arguments = NDIHelpers::autowireArguments($rm, $arguments, $this); return $this->formatPhp('$this->?(?*)', array(NDIContainer::getMethodName($service, FALSE), $arguments), $self); } elseif ($entity === 'not') { // operator return $this->formatPhp('!?', array($arguments[0])); } elseif (is_string($entity)) { // class name if ($constructor = NClassReflection::from($entity)->getConstructor()) { $this->addDependency($constructor->getFileName()); $arguments = NDIHelpers::autowireArguments($constructor, $arguments, $this); } elseif ($arguments) { throw new NServiceCreationException("Unable to pass arguments, class $entity has no constructor."); } return $this->formatPhp("new $entity" . ($arguments ? '(?*)' : ''), array($arguments), $self); } elseif (!NValidators::isList($entity) || count($entity) !== 2) { throw new InvalidStateException("Expected class, method or property, " . NPhpHelpers::dump($entity) . " given."); } elseif ($entity[0] === '') { // globalFunc return $this->formatPhp("$entity[1](?*)", array($arguments), $self); } elseif (NStrings::contains($entity[1], '$')) { // property setter NValidators::assert($arguments, 'list:1', "setup arguments for '" . NCallback::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); } }
/** * Writes item into the cache. * Dependencies are: * - NCache::PRIORITY => (int) priority * - NCache::EXPIRATION => (timestamp) expiration * - NCache::SLIDING => (bool) use sliding expiration? * - NCache::TAGS => (array) tags * - NCache::FILES => (array|string) file names * - NCache::ITEMS => (array|string) cache items * - NCache::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 $dependencies = NULL) { $this->release(); $key = $this->generateKey($key); if ($data instanceof NCallback || $data instanceof Closure) { $this->storage->lock($key); $data = NCallback::create($data)->invokeArgs(array(& $dependencies)); } if ($data === NULL) { $this->storage->remove($key); } else { $this->storage->write($key, $data, $this->completeDependencies($dependencies, $data)); return $data; } }