From fcbfcd1d87c7dba6011d732b407e543d0cb2b1c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Egyed?= <1ed@mailbox.hu> Date: Sun, 21 Dec 2014 09:47:24 +0100 Subject: [PATCH 1/7] [FrameworkBundle] Refactored assets:install command, tweaked output --- .../Command/AssetsInstallCommand.php | 107 ++++++++++++------ 1 file changed, 70 insertions(+), 37 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/AssetsInstallCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/AssetsInstallCommand.php index 92a84affd633e..2150e8cc750d9 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Command/AssetsInstallCommand.php +++ b/src/Symfony/Bundle/FrameworkBundle/Command/AssetsInstallCommand.php @@ -11,6 +11,7 @@ namespace Symfony\Bundle\FrameworkBundle\Command; +use Symfony\Component\Console\Helper\Table; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputInterface; @@ -22,9 +23,15 @@ * Command that places bundle web assets into a given directory. * * @author Fabien Potencier + * @author Gábor Egyed */ class AssetsInstallCommand extends ContainerAwareCommand { + /** + * @var \Symfony\Component\Filesystem\Filesystem + */ + private $filesystem; + /** * {@inheritdoc} */ @@ -74,11 +81,11 @@ protected function execute(InputInterface $input, OutputInterface $output) throw new \InvalidArgumentException(sprintf('The target directory "%s" does not exist.', $input->getArgument('target'))); } - $filesystem = $this->getContainer()->get('filesystem'); + $this->filesystem = $this->getContainer()->get('filesystem'); // Create the bundles directory otherwise symlink will fail. $bundlesDir = $targetArg.'/bundles/'; - $filesystem->mkdir($bundlesDir, 0777); + $this->filesystem->mkdir($bundlesDir, 0777); // relative implies symlink $symlink = $input->getOption('symlink') || $input->getOption('relative'); @@ -89,50 +96,78 @@ protected function execute(InputInterface $input, OutputInterface $output) $output->writeln('Installing assets as hard copies.'); } + $table = new Table($output); + $table->setHeaders(array('Source', 'Target', 'Method / Error')); + + $ret = 0; foreach ($this->getContainer()->get('kernel')->getBundles() as $bundle) { if (is_dir($originDir = $bundle->getPath().'/Resources/public')) { $targetDir = $bundlesDir.preg_replace('/bundle$/', '', strtolower($bundle->getName())); - $output->writeln(sprintf('Installing assets for %s into %s', $bundle->getNamespace(), $targetDir)); - - $filesystem->remove($targetDir); + $this->filesystem->remove($targetDir); if ($symlink) { - if ($input->getOption('relative')) { - $relativeOriginDir = $filesystem->makePathRelative($originDir, realpath($bundlesDir)); - } else { - $relativeOriginDir = $originDir; - } - try { - $filesystem->symlink($relativeOriginDir, $targetDir); - if (!file_exists($targetDir)) { - throw new IOException('Symbolic link is broken'); - } - $output->writeln('The assets were installed using symbolic links.'); + $relative = $this->symlink($originDir, $targetDir, $input->getOption('relative')); + $table->addRow(array( + $bundle->getNamespace(), + $targetDir, + sprintf('%s symbolic link', $relative ? 'relative' : 'absolute'), + )); + + continue; } catch (IOException $e) { - if (!$input->getOption('relative')) { - $this->hardCopy($originDir, $targetDir); - $output->writeln('It looks like your system doesn\'t support symbolic links, so the assets were installed by copying them.'); - } - - // try again without the relative option - try { - $filesystem->symlink($originDir, $targetDir); - if (!file_exists($targetDir)) { - throw new IOException('Symbolic link is broken'); - } - $output->writeln('It looks like your system doesn\'t support relative symbolic links, so the assets were installed by using absolute symbolic links.'); - } catch (IOException $e) { - $this->hardCopy($originDir, $targetDir); - $output->writeln('It looks like your system doesn\'t support symbolic links, so the assets were installed by copying them.'); - } + // fall back to hard copy } - } else { + } + + try { $this->hardCopy($originDir, $targetDir); + $table->addRow(array($bundle->getNamespace(), $targetDir, 'hard copy')); + } catch (IOException $e) { + $table->addRow(array($bundle->getNamespace(), $targetDir, sprintf('%s', $e->getMessage()))); + $ret = 1; } } } + + $table->render(); + + return $ret; + } + + /** + * Creates links with absolute as a fallback. + * + * @param string $origin + * @param string $target + * @param bool $relative + * + * @throws IOException If link can not be created. + * + * @return bool Created a relative link or not. + */ + private function symlink($origin, $target, $relative = true) + { + try { + $this->filesystem->symlink( + $relative ? $this->filesystem->makePathRelative($origin, realpath(dirname($target))) : $origin, + $target + ); + + if (!file_exists($target)) { + throw new IOException(sprintf('Symbolic link "%s" is created but appears to be broken.', $target), 0, null, $target); + } + } catch (IOException $e) { + if ($relative) { + // try again with absolute + return $this->symlink($origin, $target, false); + } + + throw $e; + } + + return $relative; } /** @@ -141,10 +176,8 @@ protected function execute(InputInterface $input, OutputInterface $output) */ private function hardCopy($originDir, $targetDir) { - $filesystem = $this->getContainer()->get('filesystem'); - - $filesystem->mkdir($targetDir, 0777); + $this->filesystem->mkdir($targetDir, 0777); // We use a custom iterator to ignore VCS files - $filesystem->mirror($originDir, $targetDir, Finder::create()->ignoreDotFiles(false)->in($originDir)); + $this->filesystem->mirror($originDir, $targetDir, Finder::create()->ignoreDotFiles(false)->in($originDir)); } } From f7f8f8542539a11b23fef0dada03f6f69ebd9dfc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Egyed?= <1ed@mailbox.hu> Date: Sun, 28 Dec 2014 01:05:31 +0100 Subject: [PATCH 2/7] Simplify foreach body --- .../Command/AssetsInstallCommand.php | 51 ++++++++++--------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/AssetsInstallCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/AssetsInstallCommand.php index 2150e8cc750d9..ce42430d09dc0 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Command/AssetsInstallCommand.php +++ b/src/Symfony/Bundle/FrameworkBundle/Command/AssetsInstallCommand.php @@ -99,41 +99,42 @@ protected function execute(InputInterface $input, OutputInterface $output) $table = new Table($output); $table->setHeaders(array('Source', 'Target', 'Method / Error')); - $ret = 0; + $failed = 0; foreach ($this->getContainer()->get('kernel')->getBundles() as $bundle) { - if (is_dir($originDir = $bundle->getPath().'/Resources/public')) { - $targetDir = $bundlesDir.preg_replace('/bundle$/', '', strtolower($bundle->getName())); - - $this->filesystem->remove($targetDir); - - if ($symlink) { - try { - $relative = $this->symlink($originDir, $targetDir, $input->getOption('relative')); - $table->addRow(array( - $bundle->getNamespace(), - $targetDir, - sprintf('%s symbolic link', $relative ? 'relative' : 'absolute'), - )); - - continue; - } catch (IOException $e) { - // fall back to hard copy - } - } + if (!is_dir($originDir = $bundle->getPath().'/Resources/public')) { + continue; + } + + $targetDir = $bundlesDir.preg_replace('/bundle$/', '', strtolower($bundle->getName())); + $this->filesystem->remove($targetDir); + if ($symlink) { try { - $this->hardCopy($originDir, $targetDir); - $table->addRow(array($bundle->getNamespace(), $targetDir, 'hard copy')); + $relative = $this->symlink($originDir, $targetDir, $input->getOption('relative')); + $table->addRow(array( + $bundle->getNamespace(), + $targetDir, + sprintf('%s symbolic link', $relative ? 'relative' : 'absolute'), + )); + + continue; } catch (IOException $e) { - $table->addRow(array($bundle->getNamespace(), $targetDir, sprintf('%s', $e->getMessage()))); - $ret = 1; + // fall back to hard copy } } + + try { + $this->hardCopy($originDir, $targetDir); + $table->addRow(array($bundle->getNamespace(), $targetDir, 'hard copy')); + } catch (IOException $e) { + $table->addRow(array($bundle->getNamespace(), $targetDir, sprintf('%s', $e->getMessage()))); + $failed = 1; + } } $table->render(); - return $ret; + return $failed; } /** From a4f1a0a879017cc072404ab74e5c3c146a6961d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Egyed?= <1ed@mailbox.hu> Date: Sun, 28 Dec 2014 01:06:51 +0100 Subject: [PATCH 3/7] Remove recursion --- .../FrameworkBundle/Command/AssetsInstallCommand.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/AssetsInstallCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/AssetsInstallCommand.php index ce42430d09dc0..a6877b684c590 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Command/AssetsInstallCommand.php +++ b/src/Symfony/Bundle/FrameworkBundle/Command/AssetsInstallCommand.php @@ -155,14 +155,18 @@ private function symlink($origin, $target, $relative = true) $relative ? $this->filesystem->makePathRelative($origin, realpath(dirname($target))) : $origin, $target ); - if (!file_exists($target)) { throw new IOException(sprintf('Symbolic link "%s" is created but appears to be broken.', $target), 0, null, $target); } } catch (IOException $e) { if ($relative) { - // try again with absolute - return $this->symlink($origin, $target, false); + // relative link failed, try again with absolute + $this->filesystem->symlink($origin, $target); + if (!file_exists($target)) { + throw new IOException(sprintf('Symbolic link "%s" is created but appears to be broken.', $target), 0, null, $target); + } + + return false; } throw $e; From e8bd5732b699f0df3cdd39db9eef92cb00b03512 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Egyed?= <1ed@mailbox.hu> Date: Sun, 28 Dec 2014 02:15:09 +0100 Subject: [PATCH 4/7] Make the code more explicit, simplify foreach body --- .../Command/AssetsInstallCommand.php | 111 +++++++++++------- 1 file changed, 69 insertions(+), 42 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/AssetsInstallCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/AssetsInstallCommand.php index a6877b684c590..1d6c8db843f28 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Command/AssetsInstallCommand.php +++ b/src/Symfony/Bundle/FrameworkBundle/Command/AssetsInstallCommand.php @@ -108,28 +108,22 @@ protected function execute(InputInterface $input, OutputInterface $output) $targetDir = $bundlesDir.preg_replace('/bundle$/', '', strtolower($bundle->getName())); $this->filesystem->remove($targetDir); - if ($symlink) { - try { - $relative = $this->symlink($originDir, $targetDir, $input->getOption('relative')); - $table->addRow(array( - $bundle->getNamespace(), - $targetDir, - sprintf('%s symbolic link', $relative ? 'relative' : 'absolute'), - )); - - continue; - } catch (IOException $e) { - // fall back to hard copy - } - } - try { - $this->hardCopy($originDir, $targetDir); - $table->addRow(array($bundle->getNamespace(), $targetDir, 'hard copy')); + if ($symlink) { + if ($input->getOption('relative')) { + $methodOrError = $this->relativeSymlinkWithFallback($originDir, $targetDir); + } else { + $methodOrError = $this->absoluteSymlinkWithFallback($originDir, $targetDir); + } + } else { + $methodOrError = $this->hardCopy($originDir, $targetDir); + } } catch (IOException $e) { - $table->addRow(array($bundle->getNamespace(), $targetDir, sprintf('%s', $e->getMessage()))); + $methodOrError = sprintf('%s', $e->getMessage()); $failed = 1; } + + $table->addRow(array($bundle->getNamespace(), $targetDir, $methodOrError)); } $table->render(); @@ -138,51 +132,84 @@ protected function execute(InputInterface $input, OutputInterface $output) } /** - * Creates links with absolute as a fallback. + * Try to create relative symlink. * - * @param string $origin - * @param string $target - * @param bool $relative + * Falling back to absolute symlink and finally hard copy. * - * @throws IOException If link can not be created. + * @param string $originDir + * @param string $targetDir * - * @return bool Created a relative link or not. + * @return string */ - private function symlink($origin, $target, $relative = true) + private function relativeSymlinkWithFallback($originDir, $targetDir) { try { - $this->filesystem->symlink( - $relative ? $this->filesystem->makePathRelative($origin, realpath(dirname($target))) : $origin, - $target - ); - if (!file_exists($target)) { - throw new IOException(sprintf('Symbolic link "%s" is created but appears to be broken.', $target), 0, null, $target); - } + $this->symlink($originDir, $targetDir, true); + $method = 'relative symbolic link'; } catch (IOException $e) { - if ($relative) { - // relative link failed, try again with absolute - $this->filesystem->symlink($origin, $target); - if (!file_exists($target)) { - throw new IOException(sprintf('Symbolic link "%s" is created but appears to be broken.', $target), 0, null, $target); - } + $method = $this->absoluteSymlinkWithFallback($originDir, $targetDir); + } - return false; - } + return $method; + } - throw $e; + /** + * Try to create absolute symlink. + * + * Falling back to hard copy. + * + * @param string $originDir + * @param string $targetDir + * + * @return string + */ + private function absoluteSymlinkWithFallback($originDir, $targetDir) + { + try { + $this->symlink($originDir, $targetDir); + $method = 'absolute symbolic link'; + } catch (IOException $e) { + // fall back to copy + $method = $this->hardCopy($originDir, $targetDir); } - return $relative; + return $method; + } + + /** + * Creates symbolic link. + * + * @param string $originDir + * @param string $targetDir + * @param bool $relative + * + * @throws IOException If link can not be created. + */ + private function symlink($originDir, $targetDir, $relative = false) + { + if ($relative) { + $originDir = $this->filesystem->makePathRelative($originDir, realpath(dirname($targetDir))); + } + $this->filesystem->symlink($originDir, $targetDir); + if (!file_exists($targetDir)) { + throw new IOException(sprintf('Symbolic link "%s" is created but appears to be broken.', $targetDir), 0, null, $targetDir); + } } /** + * Copies origin to target. + * * @param string $originDir * @param string $targetDir + * + * @return string */ private function hardCopy($originDir, $targetDir) { $this->filesystem->mkdir($targetDir, 0777); // We use a custom iterator to ignore VCS files $this->filesystem->mirror($originDir, $targetDir, Finder::create()->ignoreDotFiles(false)->in($originDir)); + + return 'hard copy'; } } From 90d7f06f56cae6f0e6fed54d37787af9e2a03179 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Egyed?= <1ed@mailbox.hu> Date: Sun, 28 Dec 2014 02:30:42 +0100 Subject: [PATCH 5/7] Simplify method names displayed on the output --- .../Bundle/FrameworkBundle/Command/AssetsInstallCommand.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/AssetsInstallCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/AssetsInstallCommand.php index 1d6c8db843f28..10766c8b114d3 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Command/AssetsInstallCommand.php +++ b/src/Symfony/Bundle/FrameworkBundle/Command/AssetsInstallCommand.php @@ -145,7 +145,7 @@ private function relativeSymlinkWithFallback($originDir, $targetDir) { try { $this->symlink($originDir, $targetDir, true); - $method = 'relative symbolic link'; + $method = 'relative symlink'; } catch (IOException $e) { $method = $this->absoluteSymlinkWithFallback($originDir, $targetDir); } @@ -167,7 +167,7 @@ private function absoluteSymlinkWithFallback($originDir, $targetDir) { try { $this->symlink($originDir, $targetDir); - $method = 'absolute symbolic link'; + $method = 'absolute symlink'; } catch (IOException $e) { // fall back to copy $method = $this->hardCopy($originDir, $targetDir); @@ -210,6 +210,6 @@ private function hardCopy($originDir, $targetDir) // We use a custom iterator to ignore VCS files $this->filesystem->mirror($originDir, $targetDir, Finder::create()->ignoreDotFiles(false)->in($originDir)); - return 'hard copy'; + return 'copy'; } } From 736ec74f5b06e935ec655b4d7ddfa299e2ec0861 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Egyed?= <1ed@mailbox.hu> Date: Sun, 28 Dec 2014 02:32:06 +0100 Subject: [PATCH 6/7] Tweaked error handling --- .../Bundle/FrameworkBundle/Command/AssetsInstallCommand.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/AssetsInstallCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/AssetsInstallCommand.php index 10766c8b114d3..0f2186ad2ddef 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Command/AssetsInstallCommand.php +++ b/src/Symfony/Bundle/FrameworkBundle/Command/AssetsInstallCommand.php @@ -11,6 +11,7 @@ namespace Symfony\Bundle\FrameworkBundle\Command; +use Exception; use Symfony\Component\Console\Helper\Table; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputOption; @@ -106,9 +107,10 @@ protected function execute(InputInterface $input, OutputInterface $output) } $targetDir = $bundlesDir.preg_replace('/bundle$/', '', strtolower($bundle->getName())); - $this->filesystem->remove($targetDir); try { + $this->filesystem->remove($targetDir); + if ($symlink) { if ($input->getOption('relative')) { $methodOrError = $this->relativeSymlinkWithFallback($originDir, $targetDir); @@ -118,7 +120,7 @@ protected function execute(InputInterface $input, OutputInterface $output) } else { $methodOrError = $this->hardCopy($originDir, $targetDir); } - } catch (IOException $e) { + } catch (Exception $e) { $methodOrError = sprintf('%s', $e->getMessage()); $failed = 1; } From 226e98f494bd6822d7cda291f8991248003f9060 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Egyed?= <1ed@mailbox.hu> Date: Sun, 28 Dec 2014 09:44:25 +0100 Subject: [PATCH 7/7] Simplify condition --- .../FrameworkBundle/Command/AssetsInstallCommand.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/AssetsInstallCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/AssetsInstallCommand.php index 0f2186ad2ddef..8c817b8446b2b 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Command/AssetsInstallCommand.php +++ b/src/Symfony/Bundle/FrameworkBundle/Command/AssetsInstallCommand.php @@ -111,12 +111,10 @@ protected function execute(InputInterface $input, OutputInterface $output) try { $this->filesystem->remove($targetDir); - if ($symlink) { - if ($input->getOption('relative')) { - $methodOrError = $this->relativeSymlinkWithFallback($originDir, $targetDir); - } else { - $methodOrError = $this->absoluteSymlinkWithFallback($originDir, $targetDir); - } + if ($input->getOption('relative')) { + $methodOrError = $this->relativeSymlinkWithFallback($originDir, $targetDir); + } elseif ($symlink) { + $methodOrError = $this->absoluteSymlinkWithFallback($originDir, $targetDir); } else { $methodOrError = $this->hardCopy($originDir, $targetDir); } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy