You are here

static function CMISRepositoryWrapper::processTemplateAsStream in CMIS API 7

2 calls to CMISRepositoryWrapper::processTemplateAsStream()
CMISService::getContentEntry in cmis_common/lib/cmis_repository_wrapper.php
CMISService::postObject in cmis_common/lib/cmis_repository_wrapper.php

File

cmis_common/lib/cmis_repository_wrapper.php, line 172

Class

CMISRepositoryWrapper

Code

static function processTemplateAsStream($template, $values = array()) {

  // Set up our return stream, just as a temporary stream.
  $returnstream = fopen('php://temp', 'r+');
  $matches = array();
  while (preg_match("/{([a-zA-Z0-9_]+)}/", $template, $matches, PREG_OFFSET_CAPTURE)) {
    $token_start = $matches[0][1];
    $token = $matches[1][0];

    // Write everything up to the token into the return stream.
    if ($token_start > 0) {
      fwrite($returnstream, substr($template, 0, $token_start));
    }

    // Remove the token from the template.
    $template = substr($template, $token_start + strlen($token) + 2);

    // If we have a matching token, write that to the return string.
    if (isset($values[$token])) {
      if (is_resource($values[$token]) && get_resource_type($values[$token]) == 'stream') {
        stream_copy_to_stream($values[$token], $returnstream);
      }
      else {
        fwrite($returnstream, $values[$token]);
      }
    }
  }

  // Append the rest of the template to the return stream.
  if (!empty($template)) {
    fwrite($returnstream, $template);
  }

  // Finally rewind the stream, ready for someone else to use it.
  rewind($returnstream);
  return $returnstream;
}