Exemplo n.º 1
0
 /**
  * Get the primary namespace for a plugin package.
  *
  * @param \Composer\Package\PackageInterface $package composer object
  * @return string The package's primary namespace.
  * @throws \RuntimeException When the package's primary namespace cannot be determined.
  */
 public function primaryNamespace($package)
 {
     $primaryNs = null;
     $autoLoad = $package->getAutoload();
     foreach ($autoLoad as $type => $pathMap) {
         if ($type !== 'psr-4') {
             continue;
         }
         $count = count($pathMap);
         if ($count === 1) {
             $primaryNs = key($pathMap);
             break;
         }
         $matches = preg_grep('#^(\\./)?src/?$#', $pathMap);
         if ($matches) {
             $primaryNs = key($matches);
             break;
         }
         foreach (['', '.'] as $path) {
             $key = array_search($path, $pathMap, true);
             if ($key !== false) {
                 $primaryNs = $key;
             }
         }
         break;
     }
     if (!$primaryNs) {
         throw new RuntimeException(sprintf("Unable to get primary namespace for package %s." . "\nEnsure you have added proper 'autoload' section to your plugin's config", $package->getName()));
     }
     return trim($primaryNs, '\\');
 }
Exemplo n.º 2
0
 /**
  * {@inheritDoc}
  *
  * @throws \RuntimeException
  */
 public function getPackageBasePath(PackageInterface $package)
 {
     $extra = $package->getExtra();
     if (!empty($extra['installer-name'])) {
         return 'plugins/' . $extra['installer-name'];
     }
     $primaryNS = null;
     $autoLoad = $package->getAutoload();
     foreach ($autoLoad as $type => $pathMap) {
         if ($type !== 'psr-4') {
             continue;
         }
         $count = count($pathMap);
         if ($count === 1) {
             $primaryNS = key($pathMap);
             break;
         }
         $matches = preg_grep('#^(\\./)?src/?$#', $pathMap);
         if ($matches) {
             $primaryNS = key($matches);
             break;
         }
         foreach (['', '.'] as $path) {
             $key = array_search($path, $pathMap, true);
             if ($key !== false) {
                 $primaryNS = $key;
             }
         }
         break;
     }
     if (!$primaryNS) {
         throw new \RuntimeException('Unable to get CakePHP plugin name.');
     }
     return 'plugins/' . trim(str_replace('\\', '/', $primaryNS), '/');
 }
Exemplo n.º 3
0
 /**
  * Returns the path name for given package.
  *
  * @param PackageInterface $package Package to install.
  * @return string path name.
  */
 public static function getPathName(PackageInterface $package)
 {
     $autoload = $package->getAutoload();
     if (isset($autoload['psr-4']) && is_array($autoload['psr-4'])) {
         $namespace = key($autoload['psr-4']);
     } elseif (isset($autoload['psr-0']) && is_array($autoload['psr-0'])) {
         $namespace = key($autoload['psr-0']);
     } else {
         throw new \InvalidArgumentException('Your Package needs to support PSR-4 or at least PSR-0.');
     }
     return rtrim(str_replace('\\', '.', $namespace), '.');
 }
Exemplo n.º 4
0
 /**
  * {@inheritDoc}
  */
 public function uninstall(InstalledRepositoryInterface $repo, PackageInterface $package)
 {
     $installedModules = $this->getInstalledModules();
     $filePath = $this->getConfigFilePath();
     // Get package information
     $name = $package->getPrettyName();
     $autoload = $package->getAutoload();
     // Remove the module from the list
     $installedModules = $this->array_remove_object($installedModules, $name, 'name');
     // Remove duplicated entries
     $installedModules = array_unique($installedModules, SORT_REGULAR);
     // Write to file
     $prettyJson = json_encode($installedModules, JSON_PRETTY_PRINT | JSON_NUMERIC_CHECK);
     file_put_contents($filePath, $prettyJson);
     parent::uninstall($repo, $package);
 }
Exemplo n.º 5
0
 protected function generatePackagetAlias(PackageInterface $package)
 {
     $fs = new Filesystem();
     $autoload = $package->getAutoload();
     $baseDir = $this->baseDir;
     $aliases = [];
     if (!empty($autoload['psr-0'])) {
         foreach ($autoload['psr-0'] as $name => $path) {
             $name = str_replace('\\', '/', trim($name, '\\'));
             if (!$fs->isAbsolutePath($path)) {
                 $path = $baseDir . '/' . $path;
             }
             $path = $fs->normalizePath($path);
             if (strpos($path . '/', $baseDir . '/') === 0) {
                 $aliases["@{$name}"] = '<base-dir>' . substr($path, strlen($baseDir)) . '/' . $name;
             } else {
                 $aliases["@{$name}"] = $path . '/' . $name;
             }
         }
     }
     if (!empty($autoload['psr-4'])) {
         foreach ($autoload['psr-4'] as $name => $path) {
             $name = str_replace('\\', '/', trim($name, '\\'));
             if (!$fs->isAbsolutePath($path)) {
                 $path = $baseDir . '/' . $path;
             }
             $path = $fs->normalizePath($path);
             if (strpos($path . '/', $baseDir . '/') === 0) {
                 $aliases["@{$name}"] = '<base-dir>' . substr($path, strlen($baseDir));
             } else {
                 $aliases["@{$name}"] = $path;
             }
         }
     }
     return $aliases;
 }
Exemplo n.º 6
0
 private function updateInformation(Package $package, PackageInterface $data, $flags)
 {
     $em = $this->doctrine->getManager();
     $version = new Version();
     $normVersion = $data->getVersion();
     $existingVersion = $package->getVersion($normVersion);
     if ($existingVersion) {
         $source = $existingVersion->getSource();
         // update if the right flag is set, or the source reference has changed (re-tag or new commit on branch)
         if ($source['reference'] !== $data->getSourceReference() || $flags & self::UPDATE_EQUAL_REFS) {
             $version = $existingVersion;
         } else {
             // mark it updated to avoid it being pruned
             $existingVersion->setUpdatedAt(new \DateTime());
             return false;
         }
     }
     $version->setName($package->getName());
     $version->setVersion($data->getPrettyVersion());
     $version->setNormalizedVersion($normVersion);
     $version->setDevelopment($data->isDev());
     $em->persist($version);
     $descr = $this->sanitize($data->getDescription());
     $version->setDescription($descr);
     $package->setDescription($descr);
     $version->setHomepage($data->getHomepage());
     $version->setLicense($data->getLicense() ?: array());
     $version->setPackage($package);
     $version->setUpdatedAt(new \DateTime());
     $version->setReleasedAt($data->getReleaseDate());
     if ($data->getSourceType()) {
         $source['type'] = $data->getSourceType();
         $source['url'] = $data->getSourceUrl();
         $source['reference'] = $data->getSourceReference();
         $version->setSource($source);
     } else {
         $version->setSource(null);
     }
     if ($data->getDistType()) {
         $dist['type'] = $data->getDistType();
         $dist['url'] = $data->getDistUrl();
         $dist['reference'] = $data->getDistReference();
         $dist['shasum'] = $data->getDistSha1Checksum();
         $version->setDist($dist);
     } else {
         $version->setDist(null);
     }
     if ($data->getType()) {
         $type = $this->sanitize($data->getType());
         $version->setType($type);
         if ($type !== $package->getType()) {
             $package->setType($type);
         }
     }
     $version->setTargetDir($data->getTargetDir());
     $version->setAutoload($data->getAutoload());
     $version->setExtra($data->getExtra());
     $version->setBinaries($data->getBinaries());
     $version->setIncludePaths($data->getIncludePaths());
     $version->setSupport($data->getSupport());
     if ($data->getKeywords()) {
         $keywords = array();
         foreach ($data->getKeywords() as $keyword) {
             $keywords[mb_strtolower($keyword, 'UTF-8')] = $keyword;
         }
         $existingTags = [];
         foreach ($version->getTags() as $tag) {
             $existingTags[mb_strtolower($tag->getName(), 'UTF-8')] = $tag;
         }
         foreach ($keywords as $tagKey => $keyword) {
             if (isset($existingTags[$tagKey])) {
                 unset($existingTags[$tagKey]);
                 continue;
             }
             $tag = Tag::getByName($em, $keyword, true);
             if (!$version->getTags()->contains($tag)) {
                 $version->addTag($tag);
             }
         }
         foreach ($existingTags as $tag) {
             $version->getTags()->removeElement($tag);
         }
     } elseif (count($version->getTags())) {
         $version->getTags()->clear();
     }
     $authorRepository = $this->doctrine->getRepository('PackagistWebBundle:Author');
     $version->getAuthors()->clear();
     if ($data->getAuthors()) {
         foreach ($data->getAuthors() as $authorData) {
             $author = null;
             foreach (array('email', 'name', 'homepage', 'role') as $field) {
                 if (isset($authorData[$field])) {
                     $authorData[$field] = trim($authorData[$field]);
                     if ('' === $authorData[$field]) {
                         $authorData[$field] = null;
                     }
                 } else {
                     $authorData[$field] = null;
                 }
             }
             // skip authors with no information
             if (!isset($authorData['email']) && !isset($authorData['name'])) {
                 continue;
             }
             $author = $authorRepository->findOneBy(array('email' => $authorData['email'], 'name' => $authorData['name'], 'homepage' => $authorData['homepage'], 'role' => $authorData['role']));
             if (!$author) {
                 $author = new Author();
                 $em->persist($author);
             }
             foreach (array('email', 'name', 'homepage', 'role') as $field) {
                 if (isset($authorData[$field])) {
                     $author->{'set' . $field}($authorData[$field]);
                 }
             }
             // only update the author timestamp once a month at most as the value is kinda unused
             if ($author->getUpdatedAt() === null || $author->getUpdatedAt()->getTimestamp() < time() - 86400 * 30) {
                 $author->setUpdatedAt(new \DateTime());
             }
             if (!$version->getAuthors()->contains($author)) {
                 $version->addAuthor($author);
             }
             if (!$author->getVersions()->contains($version)) {
                 $author->addVersion($version);
             }
         }
     }
     // handle links
     foreach ($this->supportedLinkTypes as $linkType => $opts) {
         $links = array();
         foreach ($data->{$opts['method']}() as $link) {
             $constraint = $link->getPrettyConstraint();
             if (false !== strpos($constraint, ',') && false !== strpos($constraint, '@')) {
                 $constraint = preg_replace_callback('{([><]=?\\s*[^@]+?)@([a-z]+)}i', function ($matches) {
                     if ($matches[2] === 'stable') {
                         return $matches[1];
                     }
                     return $matches[1] . '-' . $matches[2];
                 }, $constraint);
             }
             $links[$link->getTarget()] = $constraint;
         }
         foreach ($version->{'get' . $linkType}() as $link) {
             // clear links that have changed/disappeared (for updates)
             if (!isset($links[$link->getPackageName()]) || $links[$link->getPackageName()] !== $link->getPackageVersion()) {
                 $version->{'get' . $linkType}()->removeElement($link);
                 $em->remove($link);
             } else {
                 // clear those that are already set
                 unset($links[$link->getPackageName()]);
             }
         }
         foreach ($links as $linkPackageName => $linkPackageVersion) {
             $class = 'Packagist\\WebBundle\\Entity\\' . $opts['entity'];
             $link = new $class();
             $link->setPackageName($linkPackageName);
             $link->setPackageVersion($linkPackageVersion);
             $version->{'add' . $linkType . 'Link'}($link);
             $link->setVersion($version);
             $em->persist($link);
         }
     }
     // handle suggests
     if ($suggests = $data->getSuggests()) {
         foreach ($version->getSuggest() as $link) {
             // clear links that have changed/disappeared (for updates)
             if (!isset($suggests[$link->getPackageName()]) || $suggests[$link->getPackageName()] !== $link->getPackageVersion()) {
                 $version->getSuggest()->removeElement($link);
                 $em->remove($link);
             } else {
                 // clear those that are already set
                 unset($suggests[$link->getPackageName()]);
             }
         }
         foreach ($suggests as $linkPackageName => $linkPackageVersion) {
             $link = new SuggestLink();
             $link->setPackageName($linkPackageName);
             $link->setPackageVersion($linkPackageVersion);
             $version->addSuggestLink($link);
             $link->setVersion($version);
             $em->persist($link);
         }
     } elseif (count($version->getSuggest())) {
         // clear existing suggests if present
         foreach ($version->getSuggest() as $link) {
             $em->remove($link);
         }
         $version->getSuggest()->clear();
     }
     if (!$package->getVersions()->contains($version)) {
         $package->addVersions($version);
     }
     return true;
 }
Exemplo n.º 7
0
 /**
  * {@inheritdoc}
  */
 public function getAutoload()
 {
     return $this->package->getAutoload();
 }
Exemplo n.º 8
0
 protected function generateDefaultAlias(PackageInterface $package)
 {
     $fs = new Filesystem();
     $vendorDir = $fs->normalizePath($this->vendorDir);
     $autoload = $package->getAutoload();
     $aliases = [];
     if (!empty($autoload['psr-0'])) {
         foreach ($autoload['psr-0'] as $name => $path) {
             $name = str_replace('\\', '/', trim($name, '\\'));
             if (!$fs->isAbsolutePath($path)) {
                 $path = $this->vendorDir . '/' . $package->getPrettyName() . '/' . $path;
             }
             $path = $fs->normalizePath($path);
             if (strpos($path . '/', $vendorDir . '/') === 0) {
                 $aliases["@{$name}"] = '<vendor-dir>' . substr($path, strlen($vendorDir)) . '/' . $name;
             } else {
                 $aliases["@{$name}"] = $path . '/' . $name;
             }
         }
     }
     if (!empty($autoload['psr-4'])) {
         foreach ($autoload['psr-4'] as $name => $path) {
             if (is_array($path)) {
                 // ignore psr-4 autoload specifications with multiple search paths
                 // we can not convert them into aliases as they are ambiguous
                 continue;
             }
             $name = str_replace('\\', '/', trim($name, '\\'));
             if (!$fs->isAbsolutePath($path)) {
                 $path = $this->vendorDir . '/' . $package->getPrettyName() . '/' . $path;
             }
             $path = $fs->normalizePath($path);
             if (strpos($path . '/', $vendorDir . '/') === 0) {
                 $aliases["@{$name}"] = '<vendor-dir>' . substr($path, strlen($vendorDir));
             } else {
                 $aliases["@{$name}"] = $path;
             }
         }
     }
     return $aliases;
 }
 /**
  * @param PackageInterface $package
  * @param $output
  *
  * Register a Bundle with package given
  */
 private static function registerWithPackage(PackageInterface $package, $output)
 {
     if ('symfony-bundle' !== $package->getType()) {
         $output->write('<error>' . $package->getName() . ' is not a symfony bundle</error>', true);
         return;
     }
     // get package directory
     $dir = self::getRootDir() . 'vendor/' . $package->getName();
     // scan directory for Bundle file
     $needle = 'Bundle.php';
     $bundleFile = array_merge(array_filter(scandir($dir), function ($haystack) use($needle) {
         return $needle === "" || ($temp = strlen($haystack) - strlen($needle)) >= 0 && strpos($haystack, $needle, $temp) !== false;
     }));
     if (count($bundleFile) == 0) {
         $output->write('<error>Error: Bundle not fount in folder ' . $dir . '</error>', true);
         return;
     }
     // create fully qualified name from bundle class
     $bundleClass = str_replace('.php', '', $bundleFile[0]);
     $autoload = $package->getAutoload();
     if (isset($autoload['psr-4'])) {
         $fullyQualifiedNamespace = array_keys($package->getAutoload()['psr-4'])[0] . $bundleClass;
     } elseif (isset($autoload['psr-0'])) {
         $fullyQualifiedNamespace = array_keys($package->getAutoload()['psr-0'])[0] . $bundleClass;
     } else {
         $output->write('<error>Error: Cannot read package namespace from autoloading section</error>', true);
         return;
     }
     self::registerWithNamespace($fullyQualifiedNamespace, $output);
 }
Exemplo n.º 10
0
 /**
  * Get primary namespace
  *
  * @param PackageInterface $package
  *
  * @return string
  */
 private function getPrimaryNamespace(PackageInterface $package)
 {
     $primaryNs = null;
     foreach ($package->getAutoload() as $type => $pathMap) {
         if ($type !== 'psr-4') {
             continue;
         }
         if (count($pathMap) === 1) {
             $primaryNs = key($pathMap);
             break;
         }
         $matches = preg_grep('#^(\\./)?src/?$#', $pathMap);
         if ($matches) {
             $primaryNs = key($matches);
             break;
         }
         if (false !== ($key = array_search('', $pathMap, true)) || false !== ($key = array_search('.', $pathMap, true))) {
             $primaryNs = $key;
         }
         break;
     }
     if (!$primaryNs) {
         throw new RuntimeException(sprintf("Unable to get primary namespace for package %s.", $package->getName()));
     }
     return $primaryNs;
 }
Exemplo n.º 11
0
 /**
  * Set installer-name based on namespace for the source path
  *
  * With one autoload path this will be used as installer-name.
  * With two autoload paths the first non `tests` foldername will be set.
  * If more then 2 autoload paths are provided, installer-name will only be
  * set for if `src` folder is used.
  *
  * @param PackageInterface $package
  */
 protected function setInstallerName(PackageInterface $package)
 {
     $autoLoad = $package->getAutoload();
     foreach ($autoLoad as $type => $typeConfig) {
         if ($type !== 'psr-4') {
             continue;
         }
         $count = count($typeConfig);
         foreach ($typeConfig as $namespace => $path) {
             if ($path === 'tests') {
                 continue;
             }
             if ($count > 2 && $path !== 'src') {
                 continue;
             }
             $installerName = trim(str_replace('\\', '/', $namespace), '/');
             $package->setExtra(array('installer-name' => $installerName));
         }
         break;
     }
 }
Exemplo n.º 12
0
 /**
  * Prepare aliases.
  *
  * @param PackageInterface $package
  * @param string 'psr-0' or 'psr-4'
  * @return array
  */
 protected function prepareAliases(PackageInterface $package, $psr)
 {
     $autoload = $package->getAutoload();
     if (empty($autoload[$psr])) {
         return [];
     }
     $aliases = [];
     foreach ($autoload[$psr] as $name => $path) {
         if (is_array($path)) {
             // ignore psr-4 autoload specifications with multiple search paths
             // we can not convert them into aliases as they are ambiguous
             continue;
         }
         $name = str_replace('\\', '/', trim($name, '\\'));
         $path = $this->preparePath($package, $path);
         $path = $this->substitutePath($path, $this->getBaseDir(), self::BASE_DIR_SAMPLE);
         if ('psr-0' === $psr) {
             $path .= '/' . $name;
         }
         $aliases["@{$name}"] = $path;
     }
     return $aliases;
 }
Exemplo n.º 13
0
 private function updateInformation(OutputInterface $output, RegistryInterface $doctrine, $package, PackageInterface $data)
 {
     $em = $doctrine->getEntityManager();
     $version = new Version();
     $version->setName($package->getName());
     $version->setNormalizedVersion(preg_replace('{-dev$}i', '', $data->getVersion()));
     // check if we have that version yet
     foreach ($package->getVersions() as $existingVersion) {
         if ($existingVersion->equals($version)) {
             // avoid updating newer versions, in case two branches have the same version in their composer.json
             if ($existingVersion->getReleasedAt() > $data->getReleaseDate()) {
                 return;
             }
             if ($existingVersion->getDevelopment()) {
                 $version = $existingVersion;
                 break;
             }
             return;
         }
     }
     $version->setVersion($data->getPrettyVersion());
     $version->setDevelopment(substr($data->getVersion(), -4) === '-dev');
     $em->persist($version);
     $version->setDescription($data->getDescription());
     $package->setDescription($data->getDescription());
     $version->setHomepage($data->getHomepage());
     $version->setLicense($data->getLicense() ?: array());
     $version->setPackage($package);
     $version->setUpdatedAt(new \DateTime());
     $version->setReleasedAt($data->getReleaseDate());
     if ($data->getSourceType()) {
         $source['type'] = $data->getSourceType();
         $source['url'] = $data->getSourceUrl();
         $source['reference'] = $data->getSourceReference();
         $version->setSource($source);
     }
     if ($data->getDistType()) {
         $dist['type'] = $data->getDistType();
         $dist['url'] = $data->getDistUrl();
         $dist['reference'] = $data->getDistReference();
         $dist['shasum'] = $data->getDistSha1Checksum();
         $version->setDist($dist);
     }
     if ($data->getType()) {
         $version->setType($data->getType());
         if ($data->getType() && $data->getType() !== $package->getType()) {
             $package->setType($data->getType());
         }
     }
     $version->setTargetDir($data->getTargetDir());
     $version->setAutoload($data->getAutoload());
     $version->setExtra($data->getExtra());
     $version->setBinaries($data->getBinaries());
     $version->getTags()->clear();
     if ($data->getKeywords()) {
         foreach ($data->getKeywords() as $keyword) {
             $version->addTag(Tag::getByName($em, $keyword, true));
         }
     }
     $version->getAuthors()->clear();
     if ($data->getAuthors()) {
         foreach ($data->getAuthors() as $authorData) {
             $author = null;
             // skip authors with no information
             if (empty($authorData['email']) && empty($authorData['name'])) {
                 continue;
             }
             if (!empty($authorData['email'])) {
                 $author = $doctrine->getRepository('PackagistWebBundle:Author')->findOneByEmail($authorData['email']);
             }
             if (!$author && !empty($authorData['homepage'])) {
                 $author = $doctrine->getRepository('PackagistWebBundle:Author')->findOneBy(array('name' => $authorData['name'], 'homepage' => $authorData['homepage']));
             }
             if (!$author && !empty($authorData['name'])) {
                 $author = $doctrine->getRepository('PackagistWebBundle:Author')->findOneByNameAndPackage($authorData['name'], $package);
             }
             if (!$author) {
                 $author = new Author();
                 $em->persist($author);
             }
             foreach (array('email', 'name', 'homepage') as $field) {
                 if (isset($authorData[$field])) {
                     $author->{'set' . $field}($authorData[$field]);
                 }
             }
             $author->setUpdatedAt(new \DateTime());
             if (!$version->getAuthors()->contains($author)) {
                 $version->addAuthor($author);
             }
             if (!$author->getVersions()->contains($version)) {
                 $author->addVersion($version);
             }
         }
     }
     foreach ($this->supportedLinkTypes as $linkType => $linkEntity) {
         $links = array();
         foreach ($data->{'get' . $linkType . 's'}() as $link) {
             $links[$link->getTarget()] = $link->getPrettyConstraint();
         }
         foreach ($version->{'get' . $linkType}() as $link) {
             // clear links that have changed/disappeared (for updates)
             if (!isset($links[$link->getPackageName()]) || $links[$link->getPackageName()] !== $link->getPackageVersion()) {
                 $version->{'get' . $linkType}()->removeElement($link);
                 $em->remove($link);
             } else {
                 // clear those that are already set
                 unset($links[$link->getPackageName()]);
             }
         }
         foreach ($links as $linkPackageName => $linkPackageVersion) {
             $class = 'Packagist\\WebBundle\\Entity\\' . $linkEntity;
             $link = new $class();
             $link->setPackageName($linkPackageName);
             $link->setPackageVersion($linkPackageVersion);
             $version->{'add' . $linkType . 'Link'}($link);
             $link->setVersion($version);
             $em->persist($link);
         }
     }
     if (!$package->getVersions()->contains($version)) {
         $package->addVersions($version);
     }
 }
Exemplo n.º 14
0
 public function getAutoload()
 {
     return $this->aliasOf->getAutoload();
 }
Exemplo n.º 15
0
 /**
  * Try to determine the package name with the filename and
  * the package autoload definition.
  * This won't work 100%, f.e. with Bundles that have only have
  * an autoloader prefix, instead of the full namespace untill the BundleFile
  *
  * @param  \Composer\Package\PackageInterface $package
  * @param  type                               $bundleFileName
  * @return string
  */
 public function getFQCN(PackageInterface $package, $bundleFileName)
 {
     $bundleName = substr(basename($bundleFileName), 0, -4);
     $autoload = $package->getAutoload();
     if (isset($autoload['psr-0'])) {
         $namespace = key($autoload['psr-0']);
         return rtrim($namespace, '\\') . '\\' . $bundleName;
     }
     if (isset($autoload['psr-4'])) {
         $namespace = key($autoload['psr-4']);
         return $namespace . $bundleName;
     }
     return $bundleName;
 }
 /**
  * Set installer-name based on namespace for the source path checking in
  * following order:
  *
  * - With only one autoload path the namespace for that path will be used.
  * - With multiple paths if path 'src' exists it's namespace will be used.
  * - With two autoload paths provided, the namespace of path other than
  *   'tests' will be used.
  *
  * No installer-name is set if PSR-4 autoload block is not found or if none
  * of the above conditions are met.
  *
  * @param PackageInterface $package
  */
 protected function setInstallerName(PackageInterface $package)
 {
     $primaryNS = null;
     $autoLoad = $package->getAutoload();
     foreach ($autoLoad as $type => $typeConfig) {
         if ($type !== 'psr-4') {
             continue;
         }
         $count = count($typeConfig);
         if ($count === 1) {
             $primaryNS = key($typeConfig);
             break;
         }
         $matches = preg_grep('#^(\\./)?src/?$#', $typeConfig);
         if ($matches) {
             $primaryNS = key($matches);
             break;
         }
         if ($count === 2) {
             reset($typeConfig);
             if (preg_match('#^(\\./)?tests/?$#', current($typeConfig))) {
                 next($typeConfig);
             }
             $primaryNS = key($typeConfig);
             break;
         }
         break;
     }
     if ($primaryNS) {
         $package->setExtra(array('installer-name' => trim(str_replace('\\', '/', $primaryNS), '/')));
     }
 }
Exemplo n.º 17
0
 private function updateInformation(Package $package, PackageInterface $data, $flags)
 {
     $em = $this->doctrine->getEntityManager();
     $version = new Version();
     $version->setNormalizedVersion($data->getVersion());
     // check if we have that version yet
     foreach ($package->getVersions() as $existingVersion) {
         if ($existingVersion->getNormalizedVersion() === $version->getNormalizedVersion()) {
             if ($existingVersion->getDevelopment() || $flags & self::UPDATE_TAGS) {
                 $version = $existingVersion;
                 break;
             }
             // mark it updated to avoid it being pruned
             $existingVersion->setUpdatedAt(new \DateTime());
             return;
         }
     }
     $version->setName($package->getName());
     $version->setVersion($data->getPrettyVersion());
     $version->setDevelopment($data->isDev());
     $em->persist($version);
     $version->setDescription($data->getDescription());
     $package->setDescription($data->getDescription());
     $version->setHomepage($data->getHomepage());
     $version->setLicense($data->getLicense() ?: array());
     $version->setPackage($package);
     $version->setUpdatedAt(new \DateTime());
     $version->setReleasedAt($data->getReleaseDate());
     if ($data->getSourceType()) {
         $source['type'] = $data->getSourceType();
         $source['url'] = $data->getSourceUrl();
         $source['reference'] = $data->getSourceReference();
         $version->setSource($source);
     }
     if ($data->getDistType()) {
         $dist['type'] = $data->getDistType();
         $dist['url'] = $data->getDistUrl();
         $dist['reference'] = $data->getDistReference();
         $dist['shasum'] = $data->getDistSha1Checksum();
         $version->setDist($dist);
     }
     if ($data->getType()) {
         $version->setType($data->getType());
         if ($data->getType() && $data->getType() !== $package->getType()) {
             $package->setType($data->getType());
         }
     }
     $version->setTargetDir($data->getTargetDir());
     $version->setAutoload($data->getAutoload());
     $version->setExtra($data->getExtra());
     $version->setBinaries($data->getBinaries());
     $version->setIncludePaths($data->getIncludePaths());
     $version->setSupport($data->getSupport());
     $version->getTags()->clear();
     if ($data->getKeywords()) {
         foreach ($data->getKeywords() as $keyword) {
             $tag = Tag::getByName($em, $keyword, true);
             if (!$version->getTags()->contains($tag)) {
                 $version->addTag($tag);
             }
         }
     }
     $authorRepository = $this->doctrine->getRepository('PackagistWebBundle:Author');
     $version->getAuthors()->clear();
     if ($data->getAuthors()) {
         foreach ($data->getAuthors() as $authorData) {
             $author = null;
             // skip authors with no information
             if (empty($authorData['email']) && empty($authorData['name'])) {
                 continue;
             }
             if (!empty($authorData['email'])) {
                 $author = $authorRepository->findOneByEmail($authorData['email']);
             }
             if (!$author && !empty($authorData['homepage'])) {
                 $author = $authorRepository->findOneBy(array('name' => $authorData['name'], 'homepage' => $authorData['homepage']));
             }
             if (!$author && !empty($authorData['name'])) {
                 $author = $authorRepository->findOneByNameAndPackage($authorData['name'], $package);
             }
             if (!$author) {
                 $author = new Author();
                 $em->persist($author);
             }
             foreach (array('email', 'name', 'homepage', 'role') as $field) {
                 if (isset($authorData[$field])) {
                     $author->{'set' . $field}($authorData[$field]);
                 }
             }
             $author->setUpdatedAt(new \DateTime());
             if (!$version->getAuthors()->contains($author)) {
                 $version->addAuthor($author);
             }
             if (!$author->getVersions()->contains($version)) {
                 $author->addVersion($version);
             }
         }
     }
     // handle links
     foreach ($this->supportedLinkTypes as $linkType => $opts) {
         $links = array();
         foreach ($data->{$opts['method']}() as $link) {
             $constraint = $link->getPrettyConstraint();
             if (false !== strpos($constraint, '~')) {
                 $constraint = str_replace(array('[', ']'), '', $link->getConstraint());
                 $constraint = preg_replace('{(\\d\\.\\d)(\\.0)+(?=$|,|-)}', '$1', $constraint);
                 $constraint = preg_replace('{([><=,]) }', '$1', $constraint);
                 $constraint = preg_replace('{(<[0-9.]+)-dev}', '$1', $constraint);
             }
             if (false !== strpos($constraint, ',') && false !== strpos($constraint, '@')) {
                 $constraint = preg_replace_callback('{([><]=?\\s*[^@]+?)@([a-z]+)}i', function ($matches) {
                     if ($matches[2] === 'stable') {
                         return $matches[1];
                     }
                     return $matches[1] . '-' . $matches[2];
                 }, $constraint);
             }
             $links[$link->getTarget()] = $constraint;
         }
         foreach ($version->{'get' . $linkType}() as $link) {
             // clear links that have changed/disappeared (for updates)
             if (!isset($links[$link->getPackageName()]) || $links[$link->getPackageName()] !== $link->getPackageVersion()) {
                 $version->{'get' . $linkType}()->removeElement($link);
                 $em->remove($link);
             } else {
                 // clear those that are already set
                 unset($links[$link->getPackageName()]);
             }
         }
         foreach ($links as $linkPackageName => $linkPackageVersion) {
             $class = 'Packagist\\WebBundle\\Entity\\' . $opts['entity'];
             $link = new $class();
             $link->setPackageName($linkPackageName);
             $link->setPackageVersion($linkPackageVersion);
             $version->{'add' . $linkType . 'Link'}($link);
             $link->setVersion($version);
             $em->persist($link);
         }
     }
     // handle suggests
     if ($suggests = $data->getSuggests()) {
         foreach ($version->getSuggest() as $link) {
             // clear links that have changed/disappeared (for updates)
             if (!isset($suggests[$link->getPackageName()]) || $suggests[$link->getPackageName()] !== $link->getPackageVersion()) {
                 $version->getSuggest()->removeElement($link);
                 $em->remove($link);
             } else {
                 // clear those that are already set
                 unset($suggests[$link->getPackageName()]);
             }
         }
         foreach ($suggests as $linkPackageName => $linkPackageVersion) {
             $link = new SuggestLink();
             $link->setPackageName($linkPackageName);
             $link->setPackageVersion($linkPackageVersion);
             $version->addSuggestLink($link);
             $link->setVersion($version);
             $em->persist($link);
         }
     }
     if (!$package->getVersions()->contains($version)) {
         $package->addVersions($version);
     }
 }
Exemplo n.º 18
0
 protected function getAutoloadMapForPackage(PackageInterface $package)
 {
     $dir = $this->composer->getConfig()->get('vendor-dir');
     $autoload = $package->getAutoload();
     $map = array();
     if (isset($autoload['psr-0']) && is_array($autoload['psr-0'])) {
         foreach ($autoload['psr-0'] as $ns => $src) {
             $map[$ns] = $dir . '/' . $package->getName() . '/' . $src;
         }
     }
     return $map;
 }
Exemplo n.º 19
0
 /**
  * @param PackageInterface $package
  *
  * @throws \InvalidArgumentException Throws an exception, if the package has illegal settings.
  */
 protected function validatePackage(PackageInterface $package)
 {
     $autoload = $package->getAutoload();
     if (!empty($autoload['psr-4']) && null !== $package->getTargetDir()) {
         $name = $package->getName();
         $package->getTargetDir();
         throw new \InvalidArgumentException("PSR-4 autoloading is incompatible with the target-dir property, remove the target-dir in package '{$name}'.");
     }
     if (!empty($autoload['psr-4'])) {
         foreach ($autoload['psr-4'] as $namespace => $dirs) {
             if ($namespace !== '' && '\\' !== substr($namespace, -1)) {
                 throw new \InvalidArgumentException("psr-4 namespaces must end with a namespace separator, '{$namespace}' does not, use '{$namespace}\\'.");
             }
         }
     }
 }
Exemplo n.º 20
0
 /**
  * Find the correct Flow package name for the given package.
  * Will try the following order:
  *
  * - composer manifest "extras.installer-name"
  * - first PSR-0 autoloading namespace
  * - first PSR-4 autoloading namespace
  * - composer manifest "extras.package-key"
  * - composer package name (Does not work in all cases but common cases should be fine. Eg. "foo/bar" => "Foo.Bar", "foo/bar-baz" => "Foo.Bar.Baz")
  *
  * @param PackageInterface $package
  * @return string
  */
 protected function deriveFlowPackageName(PackageInterface $package)
 {
     $extras = $package->getExtra();
     $autoload = $package->getAutoload();
     if (isset($extras['installer-name'])) {
         $flowPackageName = $extras['installer-name'];
     } elseif (isset($autoload['psr-0']) && is_array($autoload['psr-0'])) {
         $namespace = key($autoload['psr-0']);
         $flowPackageName = str_replace('\\', '.', $namespace);
     } elseif (isset($autoload['psr-4']) && is_array($autoload['psr-4'])) {
         $namespace = key($autoload['psr-4']);
         $flowPackageName = rtrim(str_replace('\\', '.', $namespace), '.');
     } else {
         if (isset($extras['neos']['package-key']) && $this->isPackageKeyValid($extras['neos']['package-key'])) {
             $name = $extras['neos']['package-key'];
         } else {
             $name = $package->getName();
         }
         $nameParts = explode('/', $name);
         $nameParts = array_map(function ($element) {
             $subParts = explode('-', $element);
             $subParts = array_map('ucfirst', $subParts);
             return implode('.', $subParts);
         }, $nameParts);
         $flowPackageName = implode('.', $nameParts);
     }
     return $flowPackageName;
 }