@@ -74,19 +74,48 @@ protected function execute(InputInterface $input, OutputInterface $output)
7474
7575 foreach ($ this ->container ->get ('kernel ' )->getBundles () as $ bundle ) {
7676 if (is_dir ($ originDir = $ bundle ->getPath ().'/Resources/public ' )) {
77- $ targetDir = $ input ->getArgument ('target ' ).'/bundles/ ' .preg_replace ('/bundle$/ ' , '' , strtolower ($ bundle ->getName ()));
77+ $ bundlesDir = $ input ->getArgument ('target ' ).'/bundles/ ' ;
78+ $ targetDir = $ bundlesDir .preg_replace ('/bundle$/ ' , '' , strtolower ($ bundle ->getName ()));
7879
7980 $ output ->writeln (sprintf ('Installing assets for <comment>%s</comment> into <comment>%s</comment> ' , $ bundle ->getNamespace (), $ targetDir ));
8081
8182 $ filesystem ->remove ($ targetDir );
8283
8384 if ($ input ->getOption ('symlink ' )) {
84- $ filesystem ->symlink ($ originDir , $ targetDir );
85+ $ relativeOriginDir = $ this ->makePathRelative ($ originDir , realpath ($ bundlesDir ));
86+ $ filesystem ->symlink ($ relativeOriginDir , $ targetDir );
8587 } else {
8688 $ filesystem ->mkdir ($ targetDir , 0777 );
8789 $ filesystem ->mirror ($ originDir , $ targetDir );
8890 }
8991 }
9092 }
9193 }
94+
95+ /**
96+ * Given an existing path, convert it to a path relative to a given starting path
97+ *
98+ * @var string Absolute path of target
99+ * @var string Absolute path where traversal begins
100+ * @return string Path of target relative to starting path
101+ */
102+ protected function makePathRelative ($ endPath , $ startPath )
103+ {
104+ // Find for which character the the common path stops
105+ $ offset = 0 ;
106+ while ($ startPath [$ offset ] === $ endPath [$ offset ]) {
8620
107+ $ offset ++;
108+ }
109+
110+ // Determine how deep the start path is relative to the common path (ie, "web/bundles" = 2 levels)
111+ $ depth = substr_count (substr ($ startPath , $ offset ), DIRECTORY_SEPARATOR ) + 1 ;
112+
113+ // Repeated "../" for each level need to reach the common path
114+ $ traverser = str_repeat ('../ ' , $ depth );
115+
116+ // Construct $endPath from traversing to the common path, then to the remaining $endPath
117+ $ relativePath = $ traverser .substr ($ endPath , $ offset );
118+
119+ return $ relativePath ;
120+ }
92121}
0 commit comments