/** * Contstructor * * @link http://www.php.net/manual/en/function.parse-ini-string.php parse_ini_string() * * @param \BLW\Type\IFile $Config * INI file to load. * @param boolean $ProcessSections * By setting the process_sections parameter to <code>TRUE</code> (default), you get a multidimensional array, with the section names and settings included. * @param integer $ScannerMode * Can either be <code>INI_SCANNER_NORMAL</code> (default) or <code>INI_SCANNER_RAW</code>. If <code>INI_SCANNER_RAW</code> is supplied, then option values will not be parsed. */ public function __construct(IFile $Config, $ProcessSections = true, $ScannerMode = INI_SCANNER_NORMAL) { // Default data $input = array(); // Is PHP file unreadable? Exception. if (!$Config->isReadable()) { // Exception throw new FileException(strval($Config)); // PHP is readable? } else { // Current list of variables $OldVars = get_defined_vars(); $OldVars['OldVars'] = $OldVars; // Include file include $Config; // Parse all variables foreach (get_defined_vars() as $k => $v) { // Filter previous variables if (array_key_exists($k, $OldVars)) { continue; // Add all new arrays to $input } elseif (is_array($v)) { $input += $v; } } } // Set up class ArrayObject::__construct($input, IConfig::FLAGS, IConfig::ITERATOR); }
/** * Contstructor * * @link http://www.php.net/manual/en/function.parse-ini-string.php parse_ini_string() * * @param \BLW\Type\IFile $Config * INI file to load. * @param boolean $ProcessSections * By setting the process_sections parameter to <code>TRUE</code> (default), you get a multidimensional array, with the section names and settings included. * @param integer $ScannerMode * Can either be <code>INI_SCANNER_NORMAL</code> (default) or <code>INI_SCANNER_RAW</code>. If <code>INI_SCANNER_RAW</code> is supplied, then option values will not be parsed. */ public function __construct(IFile $Config, $ProcessSections = true, $ScannerMode = INI_SCANNER_NORMAL) { // Is INI file unreadable? Exception. if (!$Config->isReadable()) { // Exception throw new FileException(strval($Config)); } // Parse ini file $input = parse_ini_string($Config->getContents(), !!$ProcessSections, $ScannerMode) ?: array(); // Set up class ArrayObject::__construct($input ?: array(), IConfig::FLAGS, IConfig::ITERATOR); }
/** * Hook that is called just after an object is unserialized. */ public function doUnSerialize() { // Recreate fp $pos = $this->_fp; $this->_fp = $this->_File->createResource($this->_Flags, stream_context_create($this->_Options)); // Restore fp if ($pos && ($this->_Flags & IFile::APPEND) == 0) { fseek($this->_fp, $pos); } }
/** * Constructor * * @param \BLW\Type\IFile $File * File to attatch. * @param string $Name * [optional] File name (basename). * @param string $Type * [optional] File mime type. */ public function __construct(IFile $File, $Name = null, $Type = null) { // Open file if it is not readable if (!$File->isReadable()) { try { $File->openFile(); } catch (FileException $e) { throw new FileException($File->getPathname(), null, $e->getCode(), $e); } } // IContainer constructor parent::__construct(IPart::HEADER, 'string'); // Ensure $Name and $Type are set / not empty $Name = @substr($Name, 0) ?: $File->getBasename(); $Type = @substr($Type, 0) ?: $File->getMimetype(); // Host $Host = @$_SERVER['HTTP_HOST'] ?: @$_SERVER['SERVER_NAME'] ?: @$_SERVER['SERVER_ADDR'] ?: '0.0.0.0'; $Host = "http://{$Host}/"; // Attachment Headers parent::offsetSet('Content-Type', new ContentType($Type, array('name' => $Name))); parent::offsetSet('Content-Transfer-Encoding', new ContentTransferEncoding('base64')); parent::offsetSet('Content-Disposition', new ContentDisposition('inline', array('filename' => $Name))); parent::offsetSet('Content-ID', new ContentID()); parent::offsetSet('Content-Location', new ContentLocation(new GenericURI("/{$Name}"))); parent::offsetSet('Content-Base', new ContentBase(new GenericURI($Host))); // Attachment content parent::offsetSet('Content', $this->format($File->getContents(), self::CHUNKLEN) . $this->_CRLF); }
/** * Constructor * * @param string $Field * Label of field. * @param \BLW\Type\IFile $File * File to attatch. * @param string $Name * File name (basename). * @param string $Type * File mime type. */ public function __construct($Field, IFile $File, $Name = null, $Type = null) { // Is $Field not a string? if (!is_string($Field) && !is_callable(array($Field, '__toString'))) { throw new InvalidArgumentException(0); // Open file if it is not readable } elseif (!$File->isReadable()) { try { $File->openFile(); } catch (FileException $e) { throw new FileException($File->getPathname(), null, $e->getCode(), $e); } } // IContainer constructor parent::__construct(IPart::HEADER, 'string'); $Field = strval($Field); // Get $Name and $Type from file if not set $Type = $Type ?: $File->getMimetype(); $Name = $Name ?: $File->getBasename(); // File Headers parent::offsetSet('Content-Disposition', new ContentDisposition('form-data', array('name' => $Field, 'filename' => $Name))); parent::offsetSet('Content-Type', new ContentType($Type)); parent::offsetSet('Content-Transfer-Encoding', new ContentTransferEncoding('binary')); // File content parent::offsetSet('Content', $this->format($File->getContents(), self::CHUNKLEN) . $this->_CRLF); }
/** * Contstructor * * @link http://www.php.net/manual/en/function.parse-ini-string.php parse_ini_string() * * @param \BLW\Type\IFile $Config * YAML file to load. * @param integer $Options * Bitmask of YAML decode options. (YAML_BIGINT_AS_STRING) */ public function __construct(IFile $Config, $Options = 0) { // Is YAML file unreadable? Exception. if (!$Config->isReadable()) { // Exception throw new FileException(strval($Config)); } // Decoder self::$_Decoder = self::$_Decoder ?: new Parser(); // Parse yaml file try { $input = self::$_Decoder->parse($Config->getContents(), true) ?: array(); // @codeCoverageIgnoreStart // Error? } catch (\Exception $e) { $input = array(); trigger_error(sprintf('Unable to parse yaml file (%s): %s', $Config, $e->getMessage()), E_USER_WARNING); } // @codeCoverageIgnoreEnd // Set up class ArrayObject::__construct($input, IConfig::FLAGS, IConfig::ITERATOR); }
/** * Contstructor * * @link http://www.php.net/manual/en/function.parse-ini-string.php parse_ini_string() * * @param \BLW\Type\IFile $Config * JSON file to load. * @param integer $Options * Bitmask of JSON decode options. (JSON_BIGINT_AS_STRING) */ public function __construct(IFile $Config, $Options = 0) { // Default data $input = array(); // Is JSON file unreadable? Exception. if (!$Config->isReadable()) { // Exception throw new FileException(strval($Config)); // JSON is readable? } else { // Parse json file $json = $Config->getContents(); $json = substr($json, strpos($json, '{'), strrpos($json, '}') + 1); if (!empty($json)) { // @codeCoverageIgnoreStart $input = version_compare(PHP_VERSION, '5.4.0', '>=') ? json_decode($json, true, 20, $Options) : json_decode($json, true, 20); // @codeCoverageIgnoreEnd } } // Set up class ArrayObject::__construct($input, IConfig::FLAGS, IConfig::ITERATOR); }
/** * Constructor * * @param \BLW\Type\IFile $File * File to attatch. * @param string $Name * File name (basename). * @param string $Type * File mime type. */ public function __construct(IFile $File, $Name = null, $Type = null) { // Open file if it is not readable if (!$File->isReadable()) { try { $File->openFile(); } catch (FileException $e) { throw new FileException($File->getPathname(), null, $e->getCode(), $e); } } // IContainer constructor parent::__construct(IPart::HEADER, 'string'); // Ensure $Name and $Type are set / not empty $Name = @substr($Name, 0) ?: $File->getBasename(); $Type = @substr($Type, 0) ?: $File->getMimetype(); // Attachment Headers parent::offsetSet('Content-Type', new ContentType($Type, array('name' => $Name))); parent::offsetSet('Content-Transfer-Encoding', new ContentTransferEncoding('base64')); parent::offsetSet('Content-Disposition', new ContentDisposition('attachment', array('filename' => $Name))); // Attachment content parent::offsetSet('Content', $this->format($File->getContents(), self::CHUNKLEN) . $this->_CRLF); }
/** * Add inline attachment to message. * * @throws \BLW\Model\FileException If <code>$File</code> cannot be read. * * @param \BLW\Type\IFile $File * File to attatch. * @return string UniqueID of attachment. */ public function inlineAttachment(IFile $File) { // Is file readable? if (!$File->isReadable()) { // Try to open file try { $File->openFile(); } catch (FileException $e) { throw new FileException(strval($File), null, $e->getCode(), $e); } } // Generate unique id $File->UniqueID = uniqid(); // Add attachment $this->_InlineAttachments->append($File); // Done return "@{$File->UniqueID}"; }
/** * Adds files in a directory for compilation. * * @throws \BLW\Model\FileException If <code>$Dir</code> is not a readable directory. * @throws \BLW\Model\InvalidArgumentException If <code>$Extention is * @param \BLW\Type\IFile $Dir * Directory to add files from. * @param string $Extention * [optional] Extention of files to add. * @param ... * @return boolean <code>TRUE</code> on success. <code>FALSE</code> otherwise. */ public function addDir(IFile $Dir, $Extention = 'php') { // Is $Dir invalid? if (!$Dir->isDir() || !$Dir->isReadable()) { // Exception throw new FileException($Dir); } // Exclude regex $Excluded = '![\\x2f\\x5c](?:[^\\x2f\\x5c]*tests?|[^\\x2f\\x5c]*examples?|demos?|docs?)[\\x2f\\x5c]!i'; // Include regex try { $Included = array_reduce(array_slice(func_get_args(), 1), array($this, '_extractExctentionRegex'), ''); $Included = "!(?:{$Included})\$!i"; } catch (InvalidArgumentException $e) { throw new InvalidArgumentException($e->getCode(), null, 0, $e); } // Add files $added = 0; $Dir = new RecursiveDirectoryIterator($Dir, FilesystemIterator::SKIP_DOTS | FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CURRENT_AS_FILEINFO); foreach (new RecursiveIteratorIterator($Dir, RecursiveIteratorIterator::SELF_FIRST) as $File) { // $File is a directory? if (is_dir($File)) { continue; // Is $File excluded? } elseif (preg_match($Excluded, $File)) { continue; // Is $File included } elseif (preg_match($Included, $File)) { // Is file readable? if (is_readable($File) && is_file($File)) { // Add file $this->_Files[] = $File; // Increase counter $added++; } } } // Done return $added > 1; }
/** * Constructor * * @see \BLW\Model\Command\Shell Command\Shell() * * @throws \BLW\Model\InvalidArgumentException If <code>$Configuration</code> is not readable. * @throws \RuntimeException If there is an error starting PhantomJS. * * @param \BLW\Type\IFile $Executable * Path to PhantomJS executable. * @param \BLW\Type\IFile $ConfigFile * Path to PhantomJS configuration JSON file. * @param \BLW\Type\IConfig $Config * Engine configuration: * * <ul> * <li><b>Timeout</b>: <i>int</i> Communication timeout between PHP and PhantomJS process.</li> * <li><b>CWD</b>: <i>string</i> Current working directory for PhantomJS. <code>null</code> for current working directory.</li> * <li><b>Enviroment</b>: <i>array</i> Enviroment variables passed to PhantomJS. <code>null</code> for PHP enviroment variables.</li> * <li><b>Extras</b>: <i>array</i> Extra parameters passed to proc_open()</li> * </ul> */ public function __construct(IFile $Executable, IFile $ConfigFile, IConfig $Config) { // Parent constructor parent::__construct(null, 'PhantomJS'); switch (true) { // Validate ConfigFile case !$ConfigFile->isReadable(): throw new InvalidArgumentException(1); // Validate $Config // Validate $Config case !isset($Config['Timeout']): case !$Config->offsetExists('CWD'): case !$Config->offsetExists('Environment'): case !$Config->offsetExists('Extras'): throw new InvalidArgumentException(2); // All okay // All okay default: // Properties $this->_Executable = $Executable; $this->_ConfigFile = $ConfigFile; $this->_Config = $Config; $this->_Mediator = new Mediator(); // Start PhantomJS process if (!$this->phantomStart() || !$this->_Process->getStatus('running')) { // @codeCoverageIgnoreStart throw new RuntimeException('Unable to start PhantomJS'); // @codeCoverageIgnoreEnd } } }