name: ImportMapConfigReader
class_comment: '# * Reads/Writes the importmap.php file and returns the list of entries.

  # *

  # * @author Ryan Weaver <ryan@symfonycasts.com>'
dependencies:
- name: RuntimeException
  type: class
  source: Symfony\Component\AssetMapper\Exception\RuntimeException
- name: Path
  type: class
  source: Symfony\Component\Filesystem\Path
- name: VarExporter
  type: class
  source: Symfony\Component\VarExporter\VarExporter
properties: []
methods:
- name: convertPathToFilesystemPath
  visibility: public
  parameters:
  - name: path
  comment: "# * Reads/Writes the importmap.php file and returns the list of entries.\n\
    # *\n# * @author Ryan Weaver <ryan@symfonycasts.com>\n# */\n# class ImportMapConfigReader\n\
    # {\n# private ImportMapEntries $rootImportMapEntries;\n# \n# public function\
    \ __construct(\n# private readonly string $importMapConfigPath,\n# private readonly\
    \ RemotePackageStorage $remotePackageStorage,\n# ) {\n# }\n# \n# public function\
    \ getEntries(): ImportMapEntries\n# {\n# if (isset($this->rootImportMapEntries))\
    \ {\n# return $this->rootImportMapEntries;\n# }\n# \n# $configPath = $this->importMapConfigPath;\n\
    # $importMapConfig = is_file($this->importMapConfigPath) ? (static fn () => include\
    \ $configPath)() : [];\n# \n# $entries = new ImportMapEntries();\n# foreach ($importMapConfig\
    \ ?? [] as $importName => $data) {\n# $validKeys = ['path', 'version', 'type',\
    \ 'entrypoint', 'package_specifier'];\n# if ($invalidKeys = array_diff(array_keys($data),\
    \ $validKeys)) {\n# throw new \\InvalidArgumentException(\\sprintf('The following\
    \ keys are not valid for the importmap entry \"%s\": \"%s\". Valid keys are: \"\
    %s\".', $importName, implode('\", \"', $invalidKeys), implode('\", \"', $validKeys)));\n\
    # }\n# \n# $type = isset($data['type']) ? ImportMapType::tryFrom($data['type'])\
    \ : ImportMapType::JS;\n# $isEntrypoint = $data['entrypoint'] ?? false;\n# \n\
    # if (isset($data['path'])) {\n# if (isset($data['version'])) {\n# throw new RuntimeException(\\\
    sprintf('The importmap entry \"%s\" cannot have both a \"path\" and \"version\"\
    \ option.', $importName));\n# }\n# if (isset($data['package_specifier'])) {\n\
    # throw new RuntimeException(\\sprintf('The importmap entry \"%s\" cannot have\
    \ both a \"path\" and \"package_specifier\" option.', $importName));\n# }\n# \n\
    # $entries->add(ImportMapEntry::createLocal($importName, $type, $data['path'],\
    \ $isEntrypoint));\n# \n# continue;\n# }\n# \n# $version = $data['version'] ??\
    \ null;\n# \n# if (null === $version) {\n# throw new RuntimeException(\\sprintf('The\
    \ importmap entry \"%s\" must have either a \"path\" or \"version\" option.',\
    \ $importName));\n# }\n# \n# $packageModuleSpecifier = $data['package_specifier']\
    \ ?? $importName;\n# $entries->add($this->createRemoteEntry($importName, $type,\
    \ $version, $packageModuleSpecifier, $isEntrypoint));\n# }\n# \n# return $this->rootImportMapEntries\
    \ = $entries;\n# }\n# \n# public function writeEntries(ImportMapEntries $entries):\
    \ void\n# {\n# $this->rootImportMapEntries = $entries;\n# \n# $importMapConfig\
    \ = [];\n# foreach ($entries as $entry) {\n# $config = [];\n# if ($entry->isRemotePackage())\
    \ {\n# $config['version'] = $entry->version;\n# if ($entry->packageModuleSpecifier\
    \ !== $entry->importName) {\n# $config['package_specifier'] = $entry->packageModuleSpecifier;\n\
    # }\n# } else {\n# $config['path'] = $entry->path;\n# }\n# if (ImportMapType::JS\
    \ !== $entry->type) {\n# $config['type'] = $entry->type->value;\n# }\n# if ($entry->isEntrypoint)\
    \ {\n# $config['entrypoint'] = true;\n# }\n# \n# $importMapConfig[$entry->importName]\
    \ = $config;\n# }\n# \n# $map = class_exists(VarExporter::class) ? VarExporter::export($importMapConfig)\
    \ : var_export($importMapConfig, true);\n# file_put_contents($this->importMapConfigPath,\
    \ <<<EOF\n# <?php\n# \n# /**\n# * Returns the importmap for this application.\n\
    # *\n# * - \"path\" is a path inside the asset mapper system. Use the\n# *   \
    \  \"debug:asset-map\" command to see the full list of paths.\n# *\n# * - \"entrypoint\"\
    \ (JavaScript only) set to true for any module that will\n# *     be used as an\
    \ \"entrypoint\" (and passed to the importmap() Twig function).\n# *\n# * The\
    \ \"importmap:require\" command can be used to add new entries to this file.\n\
    # */\n# return $map;\n# \n# EOF);\n# }\n# \n# public function findRootImportMapEntry(string\
    \ $moduleName): ?ImportMapEntry\n# {\n# $entries = $this->getEntries();\n# \n\
    # return $entries->has($moduleName) ? $entries->get($moduleName) : null;\n# }\n\
    # \n# public function createRemoteEntry(string $importName, ImportMapType $type,\
    \ string $version, string $packageModuleSpecifier, bool $isEntrypoint): ImportMapEntry\n\
    # {\n# $path = $this->remotePackageStorage->getDownloadPath($packageModuleSpecifier,\
    \ $type);\n# \n# return ImportMapEntry::createRemote($importName, $type, $path,\
    \ $version, $packageModuleSpecifier, $isEntrypoint);\n# }\n# \n# /**\n# * Converts\
    \ the \"path\" string from an importmap entry to the filesystem path.\n# *\n#\
    \ * The path may already be a filesystem path. But if it starts with \".\",\n\
    # * then the path is relative and the root directory is prepended."
- name: convertFilesystemPathToPath
  visibility: public
  parameters:
  - name: filesystemPath
  comment: '# * Converts a filesystem path to a relative path that can be used in
    the importmap.

    # *

    # * If no relative path could be created - e.g. because the path is not in

    # * the same directory/subdirectory as the root importmap.php file - null is returned.'
- name: getRootDirectory
  visibility: private
  parameters: []
  comment: null
- name: splitPackageNameAndFilePath
  visibility: public
  parameters:
  - name: packageName
  comment: '# * @deprecated since Symfony 7.1, use ImportMapEntry::splitPackageNameAndFilePath()
    instead'
traits:
- Symfony\Component\AssetMapper\Exception\RuntimeException
- Symfony\Component\Filesystem\Path
- Symfony\Component\VarExporter\VarExporter
interfaces: []