/** * Searches for documents in the bundle and tries to read them. * * @param string $bundle * * @return array Empty array on containing zero documents. */ public function getBundleMapping($bundle) { $mappings = []; $bundleNamespace = $this->finder->getBundleClass($bundle); $bundleNamespace = substr($bundleNamespace, 0, strrpos($bundleNamespace, '\\')); // Checks if is mapped document or bundle. if (strpos($bundle, ':') !== false) { $documents = []; } else { $documents = $this->finder->getBundleDocumentPaths($bundle); } // Loop through documents found in bundle. foreach ($documents as $document) { $documentReflection = new \ReflectionClass($bundleNamespace . '\\' . DocumentFinder::DOCUMENT_DIR . '\\' . pathinfo($document, PATHINFO_FILENAME)); $documentMapping = $this->getDocumentReflectionMapping($documentReflection); if (is_array($documentMapping) && isset($documentMapping['type'])) { $documentMapping['bundle'] = $bundle; $mappings = array_replace_recursive($mappings, [$documentMapping['type'] => $documentMapping]); } } return $mappings; }
/** * Searches for documents in the bundle and tries to read them. * * @param string $name * * @return array Empty array on containing zero documents. */ public function getBundleMapping($name) { if (!is_string($name)) { throw new \LogicException('getBundleMapping() in the Metadata collector expects a string argument only!'); } if (isset($this->mappings[$name])) { return $this->mappings[$name]; } // Checks if is mapped document or bundle. if (strpos($name, ':') !== false) { $bundleInfo = explode(':', $name); $bundle = $bundleInfo[0]; $documentClass = $bundleInfo[1]; $documents = $this->finder->getBundleDocumentPaths($bundle); $documents = array_filter($documents, function ($document) use($documentClass) { if (pathinfo($document, PATHINFO_FILENAME) == $documentClass) { return true; } }); } else { $documents = $this->finder->getBundleDocumentPaths($name); $bundle = $name; } $mappings = []; $this->finder->getBundleClass($bundle); if (!count($documents)) { return []; } // Loop through documents found in bundle. foreach ($documents as $document) { $namespace = $this->getFileNamespace($document); if (!$namespace) { continue; } $documentReflection = new \ReflectionClass($namespace . '\\' . pathinfo($document, PATHINFO_FILENAME)); $documentMapping = $this->getDocumentReflectionMapping($documentReflection); if (!array_key_exists('type', $documentMapping)) { continue; } if (!array_key_exists($documentMapping['type'], $mappings)) { $documentMapping['bundle'] = $bundle; $mappings = array_merge($mappings, [$documentMapping['type'] => $documentMapping]); } else { throw new \LogicException($bundle . ' has 2 same type names defined in the documents. ' . 'Type names must be unique!'); } } $this->cacheBundle($name, $mappings); return $mappings; }
/** * Searches for documents in bundle and tries to read them. * * @param string $bundle * * @return array Empty array on containing zero documents. */ public function getBundleMapping($bundle) { $mappings = []; $this->proxyPaths = []; $bundleNamespace = $this->finder->getBundleClass($bundle); $bundleNamespace = substr($bundleNamespace, 0, strrpos($bundleNamespace, '\\')); $documentDir = str_replace('/', '\\', $this->finder->getDocumentDir()); // Loop through documents found in bundle. foreach ($this->finder->getBundleDocumentPaths($bundle) as $document) { $documentReflection = new \ReflectionClass($bundleNamespace . '\\' . $documentDir . '\\' . pathinfo($document, PATHINFO_FILENAME)); $documentMapping = $this->getDocumentReflectionMapping($documentReflection); if ($documentMapping !== null) { $mappings = array_replace_recursive($mappings, $documentMapping); } } return $mappings; }
/** * Searches for documents in the bundle and tries to read them. * * @param string $name * * @return array Empty array on containing zero documents. */ public function getBundleMapping($name) { if (!is_string($name)) { throw new \LogicException('getBundleMapping() in the Metadata collector expects a string argument only!'); } if (isset($this->mappings[$name])) { return $this->mappings[$name]; } // Handle the case when single document mapping requested if (strpos($name, ':') !== false) { list($bundle, $documentClass) = explode(':', $name); $documents = $this->finder->getBundleDocumentClasses($bundle); $documents = in_array($documentClass, $documents) ? [$documentClass] : []; } else { $documents = $this->finder->getBundleDocumentClasses($name); $bundle = $name; } $mappings = []; $bundleNamespace = $this->finder->getBundleClass($bundle); $bundleNamespace = substr($bundleNamespace, 0, strrpos($bundleNamespace, '\\')); if (!count($documents)) { return []; } // Loop through documents found in bundle. foreach ($documents as $document) { $documentReflection = new \ReflectionClass($bundleNamespace . '\\' . DocumentFinder::DOCUMENT_DIR . '\\' . $document); try { $documentMapping = $this->getDocumentReflectionMapping($documentReflection); } catch (MissingDocumentAnnotationException $exception) { // Not a document, just ignore continue; } if (!array_key_exists($documentMapping['type'], $mappings)) { $documentMapping['bundle'] = $bundle; $mappings = array_merge($mappings, [$documentMapping['type'] => $documentMapping]); } else { throw new \LogicException($bundle . ' has 2 same type names defined in the documents. ' . 'Type names must be unique!'); } } $this->cacheBundle($name, $mappings); return $mappings; }
/** * Tests if exception is thrown for unregistered bundle. * * @expectedException \LogicException * @expectedExceptionMessage Bundle 'NotExistingBundle' does not exist. */ public function testGetBundleClassException() { $finder = new DocumentFinder($this->getContainer()->getParameter('kernel.bundles')); $finder->getBundleClass('NotExistingBundle'); }