/**
  * Scans the given $file, looking for a list of functions that we want to identify
  * to locate custom capabilities, taxonomies, etc...
  * 
  * Creates meta objects for everything that we find.
  * 
  * @param AnalyzedFile $file The file to scan
  * @param FileRenderer $file_renderer The meta object for this file.
  */
 public function scan_file($file, $file_renderer)
 {
     $function_calls = $file->get_code_elements('function_calls');
     foreach ($this->resource_types as $resource) {
         foreach ($resource['func_name'] as $function_name) {
             foreach ($function_calls as $call_path => $functions) {
                 // check and see if this function was called
                 if (array_key_exists($function_name, $functions)) {
                     if (isset($functions[$function_name]['args'])) {
                         $calls = array($functions[$function_name]);
                     } else {
                         $calls = $functions[$function_name];
                     }
                     foreach ($calls as $call) {
                         $child_renderer = new ResourceRenderer(str_replace($this->remove_chars, '', $call['args'][0]));
                         $child_renderer->set_resource_type($resource['singular'], $resource['plural']);
                         $child_renderer->add_attribute('file', $file->get_filename());
                         $child_renderer->add_attribute('args', $call['args']);
                         $file_renderer->add_child($child_renderer);
                         $this->renderers[$resource['plural']]->add_child($child_renderer);
                     }
                 }
             }
         }
     }
 }
Example #2
0
 public function addResource($rmID, $width, $height, $resourcePath, $format)
 {
     if (isset($this->renderersByRMID[$rmID])) {
         $renderer = $this->renderersByRMID[$rmID];
     } else {
         $renderer = new ResourceRenderer($width, $height);
         $this->renderersByRMID[$rmID] = $renderer;
     }
     $renderer->addResource($resourcePath, $format);
 }
Example #3
0
 /**
  * @throws Exception
  */
 private function createFile()
 {
     /** @var \Phalcon\Db\Adapter\Pdo $db */
     $db = $this->getDI()['db'];
     $query = "SELECT t.id AS textureID, t.metaID AS textureMetaID, r.id AS resourceID, r.width AS resourceWidth, r.height AS resourceHeight, r.metaID AS resourceMetaID, rm.path AS resourceMetaPath" . " FROM Texture t" . " INNER JOIN Texture_Resource j ON t.id = j.textureID" . " INNER JOIN Resource r ON j.resourceID = r.id" . " INNER JOIN ResourceMeta rm ON r.metaID = rm.id" . " WHERE t.id IN ({$this->textureIDString})";
     $result = $db->query($query);
     if (!$result->execute()) {
         throw new Exception("Failed to execute Texture search query");
     }
     $result->setFetchMode(PDO::FETCH_OBJ);
     $this->prepareFilePath();
     $specs = array();
     $resourceMetaPathsByRMID = array();
     $resourcePathsByRID = array();
     while ($obj = $result->fetch()) {
         $resourcePath = ResourcePackWorkspace::resourcePath($obj->resourceID, $obj->resourceMetaID, $this->packID);
         if (file_exists($resourcePath)) {
             $resourceMetaPathsByRMID[$obj->resourceMetaID] = $obj->resourceMetaPath;
             $resourcePathsByRID[$obj->resourceID] = $resourcePath;
             if (isset($specs[$obj->resourceMetaID][$obj->resourceID])) {
                 $resource = $specs[$obj->resourceMetaID][$obj->resourceID];
             } else {
                 $resource = new stdClass();
                 $resource->width = $obj->resourceWidth;
                 $resource->height = $obj->resourceHeight;
                 $specs[$obj->resourceMetaID][$obj->resourceID] = $resource;
             }
             $texture = new stdClass();
             $texture->id = $obj->textureID;
             $texture->metaID = $obj->textureMetaID;
             $resource->textures[] = $texture;
         }
     }
     $result = NULL;
     $zip = new ZipArchive();
     $opened = $zip->open($this->packFilePath, ZipArchive::CREATE);
     if ($opened !== TRUE) {
         throw new Exception("Failed to open new zip file");
     }
     $multiSpecs = array();
     foreach ($specs as $rmID => $resources) {
         $resourceCount = count($resources);
         if ($resourceCount == 1) {
             // just add the whole resource since we don't need to merge anything
             reset($resources);
             $rID = key($resources);
             $resourcePath = $resourcePathsByRID[$rID];
             $resourceMetaPath = $resourceMetaPathsByRMID[$rmID];
             $zip->addFile($resourcePath, $resourceMetaPath);
         } else {
             $multiSpecs[$rmID] = $resources;
         }
     }
     $specs = NULL;
     if (count($multiSpecs) > 0) {
         $largestSizeByRMID = array();
         $resourceIDsByTMID = array();
         foreach ($multiSpecs as $rmID => $resources) {
             foreach ($resources as $rID => $resource) {
                 if (!isset($largestSizeByRMID[$rmID]) || $resource->width > $largestSizeByRMID[$rmID][0]) {
                     $largestSizeByRMID[$rmID][0] = $resource->width;
                     $largestSizeByRMID[$rmID][1] = $resource->height;
                 }
                 foreach ($resource->textures as $texture) {
                     $resourceIDsByTMID[$texture->metaID] = $rID;
                 }
             }
         }
         $multiRenderer = new MultiResourceRenderer();
         $multiRMIDs = array_keys($multiSpecs);
         $placeholders = implode(',', array_fill(0, count($multiRMIDs), '?'));
         $rectStmt = $db->query("SELECT j.textureMetaID, j.resourceMetaID, j.sourceFormat FROM TextureMeta_ResourceMeta j WHERE resourceMetaID IN ({$placeholders})", $multiRMIDs);
         $rectStmt->setFetchMode(PDO::FETCH_OBJ);
         while ($obj = $rectStmt->fetch()) {
             $sourceFormat = ResourceRenderer::createSourceFormatFromString($obj->sourceFormat);
             $size = $largestSizeByRMID[$obj->resourceMetaID];
             $resourceID = $resourceIDsByTMID[$obj->textureMetaID];
             $path = $resourcePathsByRID[$resourceID];
             $multiRenderer->addResource($obj->resourceMetaID, $size[0], $size[1], $path, $sourceFormat);
         }
         unset($rectStmt);
         unset($largestSizeByRMID);
         unset($resourceIDsByTMID);
         unset($multiRMIDs);
         while ($image = $multiRenderer->renderOne($imageRMID)) {
             ob_start();
             imagepng($image, NULL, 1, PNG_NO_FILTER);
             $imageData = ob_get_clean();
             imagedestroy($image);
             $metaPath = $resourceMetaPathsByRMID[$imageRMID];
             $zip->addFromString($metaPath, $imageData);
         }
     }
     $isEmpty = $zip->numFiles == 0;
     $zip->close();
     $zip = NULL;
     return !$isEmpty;
 }