Skip to content

Commit 822f737

Browse files
committed
MAGETWO-21072: Entry point responsiveness
- added abillity to publicate symlinks
1 parent 9a7c0a4 commit 822f737

File tree

7 files changed

+121
-14
lines changed

7 files changed

+121
-14
lines changed

lib/internal/Magento/Framework/App/StaticResource.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class StaticResource implements \Magento\Framework\AppInterface
3030
private $request;
3131

3232
/**
33-
* @var \Magento\Framework\App\View\Asset\Publisher
33+
* @var View\Asset\Publisher\Symlink
3434
*/
3535
private $publisher;
3636

@@ -62,7 +62,7 @@ class StaticResource implements \Magento\Framework\AppInterface
6262
* @param State $state
6363
* @param Response\FileInterface $response
6464
* @param Request\Http $request
65-
* @param \Magento\Framework\App\View\Asset\Publisher $publisher
65+
* @param View\Asset\Publisher\Symlink $publisher
6666
* @param \Magento\Framework\View\Asset\Repository $assetRepo
6767
* @param \Magento\Framework\Module\ModuleList $moduleList
6868
* @param \Magento\Framework\ObjectManagerInterface $objectManager
@@ -73,7 +73,7 @@ public function __construct(
7373
State $state,
7474
Response\FileInterface $response,
7575
Request\Http $request,
76-
View\Asset\Publisher $publisher,
76+
View\Asset\Publisher\Symlink $publisher,
7777
\Magento\Framework\View\Asset\Repository $assetRepo,
7878
\Magento\Framework\Module\ModuleList $moduleList,
7979
\Magento\Framework\ObjectManagerInterface $objectManager,

lib/internal/Magento/Framework/App/View/Asset/Publisher.php

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
use Magento\Framework\App\Filesystem\DirectoryList;
1010
use Magento\Framework\View\Asset;
11+
use Magento\Framework\Filesystem\Directory\WriteInterface;
1112

1213
/**
1314
* A publishing service for view assets
@@ -41,28 +42,33 @@ public function __construct(
4142
*/
4243
public function publish(Asset\LocalInterface $asset)
4344
{
44-
if ($this->appState->getMode() === \Magento\Framework\App\State::MODE_DEVELOPER) {
45-
return false;
46-
}
4745
$dir = $this->filesystem->getDirectoryRead(DirectoryList::STATIC_VIEW);
4846
if ($dir->isExist($asset->getPath())) {
4947
return true;
5048
}
51-
return $this->publishAsset($asset);
49+
50+
$rootDir = $this->filesystem->getDirectoryWrite(DirectoryList::ROOT);
51+
$source = $rootDir->getRelativePath($asset->getSourceFile());
52+
53+
return $this->publishFile(
54+
$rootDir,
55+
$source,
56+
$asset->getPath(),
57+
$this->filesystem->getDirectoryWrite(DirectoryList::STATIC_VIEW)
58+
);
5259
}
5360

5461
/**
55-
* Publish the asset
62+
* Publish file
5663
*
57-
* @param Asset\LocalInterface $asset
64+
* @param WriteInterface $rootDir
65+
* @param string $source
66+
* @param string $destination
67+
* @param WriteInterface $dir
5868
* @return bool
5969
*/
60-
private function publishAsset(Asset\LocalInterface $asset)
70+
protected function publishFile($rootDir, $source, $destination, $dir)
6171
{
62-
$dir = $this->filesystem->getDirectoryWrite(DirectoryList::STATIC_VIEW);
63-
$rootDir = $this->filesystem->getDirectoryWrite(DirectoryList::ROOT);
64-
$source = $rootDir->getRelativePath($asset->getSourceFile());
65-
$destination = $asset->getPath();
6672
return $rootDir->copyFile($source, $destination, $dir);
6773
}
6874
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Framework\App\View\Asset\Publisher;
8+
9+
use Magento\Framework\Filesystem\Directory\WriteInterface;
10+
11+
use Magento\Framework\App\View\Asset;
12+
13+
class Symlink extends Asset\Publisher
14+
{
15+
/**
16+
* Publish file
17+
*
18+
* @param WriteInterface $rootDir
19+
* @param string $source
20+
* @param string $destination
21+
* @param WriteInterface $dir
22+
* @return bool
23+
*/
24+
protected function publishFile($rootDir, $source, $destination, $dir)
25+
{
26+
return $rootDir->createSymlink($source, $destination, $dir);
27+
}
28+
}

lib/internal/Magento/Framework/Filesystem/Directory/Write.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,29 @@ public function copyFile($path, $destination, WriteInterface $targetDirectory =
129129
return $this->driver->copy($absolutePath, $absoluteDestination, $targetDirectory->driver);
130130
}
131131

132+
/**
133+
* Creates symlink on a file and places it to destination
134+
*
135+
* @param string $path
136+
* @param string $destination
137+
* @param WriteInterface $targetDirectory [optional]
138+
* @return bool
139+
* @throws \Magento\Framework\Filesystem\FilesystemException
140+
*/
141+
public function createSymlink($path, $destination, WriteInterface $targetDirectory = null)
142+
{
143+
$this->assertIsFile($path);
144+
145+
$targetDirectory = $targetDirectory ?: $this;
146+
if (!$targetDirectory->isExist($this->driver->getParentDirectory($destination))) {
147+
$targetDirectory->create($this->driver->getParentDirectory($destination));
148+
}
149+
$absolutePath = $this->driver->getAbsolutePath($this->path, $path);
150+
$absoluteDestination = $targetDirectory->getAbsolutePath($destination);
151+
152+
return $this->driver->symlink($absolutePath, $absoluteDestination, $targetDirectory->driver);
153+
}
154+
132155
/**
133156
* Delete given path
134157
*

lib/internal/Magento/Framework/Filesystem/Directory/WriteInterface.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,17 @@ public function renameFile($path, $newPath, WriteInterface $targetDirectory = nu
4747
*/
4848
public function copyFile($path, $destination, WriteInterface $targetDirectory = null);
4949

50+
/**
51+
* Creates symlink on a file and places it to destination
52+
*
53+
* @param string $path
54+
* @param string $destination
55+
* @param WriteInterface $targetDirectory [optional]
56+
* @return bool
57+
* @throws \Magento\Framework\Filesystem\FilesystemException
58+
*/
59+
public function createSymlink($path, $destination, WriteInterface $targetDirectory = null);
60+
5061
/**
5162
* Change permissions of given path
5263
*

lib/internal/Magento/Framework/Filesystem/Driver/File.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,34 @@ public function copy($source, $destination, DriverInterface $targetDriver = null
284284
return $result;
285285
}
286286

287+
/**
288+
* Create symlink on source and place it into destination
289+
*
290+
* @param string $source
291+
* @param string $destination
292+
* @param DriverInterface|null $targetDriver
293+
* @return bool
294+
* @throws FilesystemException
295+
*/
296+
public function symlink($source, $destination, DriverInterface $targetDriver = null)
297+
{
298+
$result = false;
299+
if (get_class($targetDriver) == get_class($this)) {
300+
$result = @symlink($this->getScheme() . $source, $destination);
301+
}
302+
if (!$result) {
303+
throw new FilesystemException(
304+
sprintf(
305+
'Cannot create a symlink for "%s" and place it to "%s" %s',
306+
$source,
307+
$destination,
308+
$this->getWarningMessage()
309+
)
310+
);
311+
}
312+
return $result;
313+
}
314+
287315
/**
288316
* Delete file
289317
*

lib/internal/Magento/Framework/Filesystem/DriverInterface.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,17 @@ public function rename($oldPath, $newPath, DriverInterface $targetDriver = null)
144144
*/
145145
public function copy($source, $destination, DriverInterface $targetDriver = null);
146146

147+
/**
148+
* Create symlink on source and place it into destination
149+
*
150+
* @param string $source
151+
* @param string $destination
152+
* @param DriverInterface|null $targetDriver
153+
* @return bool
154+
* @throws FilesystemException
155+
*/
156+
public function symlink($source, $destination, DriverInterface $targetDriver = null);
157+
147158
/**
148159
* Delete file
149160
*

0 commit comments

Comments
 (0)