示例#1
0
 /**
  * Sets a header.
  * @param  string
  * @param  string|array  value or pair email => name
  * @param  bool
  * @return MailMimePart  provides a fluent interface
  */
 public function setHeader($name, $value, $append = FALSE)
 {
     if (!$name || preg_match('#[^a-z0-9-]#i', $name)) {
         throw new InvalidArgumentException("Header name must be non-empty alphanumeric string, '{$name}' given.");
     }
     if ($value == NULL) {
         // intentionally ==
         if (!$append) {
             unset($this->headers[$name]);
         }
     } elseif (is_array($value)) {
         // email
         $tmp =& $this->headers[$name];
         if (!$append || !is_array($tmp)) {
             $tmp = array();
         }
         foreach ($value as $email => $recipient) {
             if ($recipient !== NULL && !Strings::checkEncoding($recipient)) {
                 Validators::assert($recipient, 'unicode', "header '{$name}'");
             }
             if (preg_match('#[\\r\\n]#', $recipient)) {
                 throw new InvalidArgumentException("Name must not contain line separator.");
             }
             Validators::assert($email, 'email', "header '{$name}'");
             $tmp[$email] = $recipient;
         }
     } else {
         $value = (string) $value;
         if (!Strings::checkEncoding($value)) {
             throw new InvalidArgumentException("Header is not valid UTF-8 string.");
         }
         $this->headers[$name] = preg_replace('#[\\r\\n]+#', ' ', $value);
     }
     return $this;
 }
示例#2
0
 /**
  * Reads configuration from file.
  * @param  string  file name
  * @param  string  optional section to load
  * @return array
  */
 public function load($file, $section = NULL)
 {
     if (!is_file($file) || !is_readable($file)) {
         throw new FileNotFoundException("File '{$file}' is missing or is not readable.");
     }
     $this->dependencies[] = $file = realpath($file);
     $data = $this->getAdapter($file)->load($file);
     if ($section) {
         if (isset($data[self::INCLUDES_KEY])) {
             throw new InvalidStateException("Section 'includes' must be placed under some top section in file '{$file}'.");
         }
         $data = $this->getSection($data, $section, $file);
     }
     // include child files
     $merged = array();
     if (isset($data[self::INCLUDES_KEY])) {
         Validators::assert($data[self::INCLUDES_KEY], 'list', "section 'includes' in file '{$file}'");
         foreach ($data[self::INCLUDES_KEY] as $include) {
             $merged = ConfigHelpers::merge($this->load(dirname($file) . '/' . $include), $merged);
         }
     }
     unset($data[self::INCLUDES_KEY]);
     return ConfigHelpers::merge($data, $merged);
 }
示例#3
0
 /**
  * Parses single service from configuration file.
  * @return void
  */
 public static function parseService(DIServiceDefinition $definition, $config, $shared = TRUE)
 {
     if ($config === NULL) {
         return;
     } elseif (!is_array($config)) {
         $config = array('class' => NULL, 'factory' => $config);
     }
     $known = $shared ? array('class', 'factory', 'arguments', 'setup', 'autowired', 'run', 'tags') : array('class', 'factory', 'arguments', 'setup', 'autowired', 'tags', 'internal', 'parameters');
     if ($error = array_diff(array_keys($config), $known)) {
         throw new InvalidStateException("Unknown key '" . implode("', '", $error) . "' in definition of service.");
     }
     $arguments = array();
     if (array_key_exists('arguments', $config)) {
         Validators::assertField($config, 'arguments', 'array');
         $arguments = self::filterArguments($config['arguments']);
         $definition->setArguments($arguments);
     }
     if (array_key_exists('class', $config) || array_key_exists('factory', $config)) {
         $definition->class = NULL;
         $definition->factory = NULL;
     }
     if (array_key_exists('class', $config)) {
         Validators::assertField($config, 'class', 'string|stdClass|null');
         if ($config['class'] instanceof stdClass) {
             $definition->setClass($config['class']->value, self::filterArguments($config['class']->attributes));
         } else {
             $definition->setClass($config['class'], $arguments);
         }
     }
     if (array_key_exists('factory', $config)) {
         Validators::assertField($config, 'factory', 'callable|stdClass|null');
         if ($config['factory'] instanceof stdClass) {
             $definition->setFactory($config['factory']->value, self::filterArguments($config['factory']->attributes));
         } else {
             $definition->setFactory($config['factory'], $arguments);
         }
     }
     if (isset($config['setup'])) {
         if (ConfigHelpers::takeParent($config['setup'])) {
             $definition->setup = array();
         }
         Validators::assertField($config, 'setup', 'list');
         foreach ($config['setup'] as $id => $setup) {
             Validators::assert($setup, 'callable|stdClass', "setup item #{$id}");
             if ($setup instanceof stdClass) {
                 Validators::assert($setup->value, 'callable', "setup item #{$id}");
                 $definition->addSetup($setup->value, self::filterArguments($setup->attributes));
             } else {
                 $definition->addSetup($setup);
             }
         }
     }
     $definition->setShared($shared);
     if (isset($config['parameters'])) {
         Validators::assertField($config, 'parameters', 'array');
         $definition->setParameters($config['parameters']);
     }
     if (isset($config['autowired'])) {
         Validators::assertField($config, 'autowired', 'bool');
         $definition->setAutowired($config['autowired']);
     }
     if (isset($config['internal'])) {
         Validators::assertField($config, 'internal', 'bool');
         $definition->setInternal($config['internal']);
     }
     if (isset($config['run'])) {
         $config['tags']['run'] = (bool) $config['run'];
     }
     if (isset($config['tags'])) {
         Validators::assertField($config, 'tags', 'array');
         if (ConfigHelpers::takeParent($config['tags'])) {
             $definition->tags = array();
         }
         foreach ($config['tags'] as $tag => $attrs) {
             if (is_int($tag) && is_string($attrs)) {
                 $definition->addTag($attrs);
             } else {
                 $definition->addTag($tag, $attrs);
             }
         }
     }
 }
示例#4
0
 /**
  * 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);
     }
 }