Skip to content

Commit

Permalink
Merge pull request #472 from magento-extensibility/MAGETWO-50657-stat…
Browse files Browse the repository at this point in the history
…ic-files-are-not-loaded

[Extensibility] Magetwo 50657 static files are not loaded
  • Loading branch information
Melnikov, Igor(imelnikov) committed Mar 23, 2016
2 parents ea652b2 + 02b4c31 commit 3cc19fe
Show file tree
Hide file tree
Showing 6 changed files with 268 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Magento\Framework\Validator\Locale;

/**
* Command for deploy static content
* Deploy static content command
*/
class DeployStaticContentCommand extends Command
{
Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/Deploy/Console/Command/SetModeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Magento\Framework\App\State;

/**
* Command for change the Magento mode
* Command to set application mode
*/
class SetModeCommand extends Command
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Magento\Framework\App\State;

/**
* Command for change the Magento mode
* Command to show application mode
*/
class ShowModeCommand extends Command
{
Expand Down
108 changes: 87 additions & 21 deletions app/code/Magento/Deploy/Model/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@
*/
namespace Magento\Deploy\Model;

use Symfony\Component\Console\Output\OutputInterface;
use Magento\Framework\App\State;
use Magento\Framework\App\DeploymentConfig\Writer;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Exception\LocalizedException;
use Symfony\Component\Console\Output\OutputInterface;
use Magento\User\Model\ResourceModel\User\Collection as UserCollection;

/**
* Class Filesystem
* Generate static files, compile; clear var/generation, var/di/, var/view_preprocessed and pub/static directories
*
* A class to manage Magento modes
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Filesystem
{
Expand All @@ -37,33 +38,56 @@ class Filesystem
*/
const DEFAULT_THEME = 'Magento/blank';

/** @var \Magento\Framework\App\DeploymentConfig\Writer */
/**
* @var \Magento\Framework\App\DeploymentConfig\Writer
*/
private $writer;

/** @var \Magento\Framework\App\DeploymentConfig\Reader */
/**
* @var \Magento\Framework\App\DeploymentConfig\Reader
*/
private $reader;

/** @var \Magento\Framework\ObjectManagerInterface */
/**
* @var \Magento\Framework\ObjectManagerInterface
*/
private $objectManager;

/** @var \Magento\Framework\Filesystem */
/**
* @var \Magento\Framework\Filesystem
*/
private $filesystem;

/** @var \Magento\Framework\App\Filesystem\DirectoryList */
/**
* @var \Magento\Framework\App\Filesystem\DirectoryList
*/
private $directoryList;

/** @var \Magento\Framework\Filesystem\Driver\File */
/**
* @var \Magento\Framework\Filesystem\Driver\File
*/
private $driverFile;

/** @var \Magento\Store\Model\Config\StoreView */
/**
* @var \Magento\Store\Model\Config\StoreView
*/
private $storeView;

/** @var \Magento\Framework\ShellInterface */
/**
* @var \Magento\Framework\ShellInterface
*/
private $shell;

/** @var string */
/**
* @var string
*/
private $functionCallPath;

/**
* @var UserCollection
*/
private $userCollection;

/**
* @param \Magento\Framework\App\DeploymentConfig\Writer $writer
* @param \Magento\Framework\App\DeploymentConfig\Reader $reader
Expand Down Expand Up @@ -104,7 +128,7 @@ public function __construct(
public function regenerateStatic(
OutputInterface $output
) {
// Сlean up /var/generation, /var/di/, /var/view_preprocessed and /pub/static directories
// Сlear var/generation, var/di/, var/view_preprocessed and pub/static directories
$this->cleanupFilesystem(
[
DirectoryList::CACHE,
Expand All @@ -129,12 +153,12 @@ public function regenerateStatic(
protected function deployStaticContent(
OutputInterface $output
) {
$output->writeln('Starting static content deployment');
$output->writeln('Starting deployment of static content');
$cmd = $this->functionCallPath . 'setup:static-content:deploy '
. implode(' ', $this->storeView->retrieveLocales());
. implode(' ', $this->getUsedLocales());

/**
* @todo build a solution that does not depend on exec
* @todo eliminate exec
*/
try {
$execOutput = $this->shell->execute($cmd);
Expand All @@ -143,11 +167,55 @@ protected function deployStaticContent(
throw $e;
}
$output->writeln($execOutput);
$output->writeln('Static content deployment complete');
$output->writeln('Deployment of static content complete');
}

/**
* Runs code multi-tenant compiler to generate code and DI information
* Get admin user locales
*
* @return []string
*/
private function getAdminUserInterfaceLocales()
{
$locales = [];
foreach ($this->getUserCollection() as $user) {
$locales[] = $user->getInterfaceLocale();
}
return $locales;
}

/**
* Get used store and admin user locales
*
* @return []string
*/
private function getUsedLocales()
{
$usedLocales = array_merge(
$this->storeView->retrieveLocales(),
$this->getAdminUserInterfaceLocales()
);
return array_unique($usedLocales);
}

/**
* Get user collection
*
* @return UserCollection
* @deprecated
*/
private function getUserCollection()
{
if (!($this->userCollection instanceof UserCollection)) {
return \Magento\Framework\App\ObjectManager::getInstance()->get(
UserCollection::class
);
}
return $this->userCollection;
}

/**
* Runs compiler
*
* @param OutputInterface $output
* @return void
Expand All @@ -169,7 +237,7 @@ protected function compile(OutputInterface $output)
* exec command is necessary for now to isolate the autoloaders in the compiler from the memory state
* of this process, which would prevent some classes from being generated
*
* @todo build a solution that does not depend on exec
* @todo eliminate exec
*/
try {
$execOutput = $this->shell->execute($cmd);
Expand Down Expand Up @@ -222,7 +290,6 @@ public function cleanupFilesystem($directoryCodeList)
* @param int $dirPermissions
* @param int $filePermissions
* @return void
*
* @deprecated
*/
protected function changePermissions($directoryCodeList, $dirPermissions, $filePermissions)
Expand All @@ -242,7 +309,6 @@ protected function changePermissions($directoryCodeList, $dirPermissions, $fileP
* Chenge permissions on static resources
*
* @return void
*
* @deprecated
*/
public function lockStaticResources()
Expand Down
176 changes: 176 additions & 0 deletions app/code/Magento/Deploy/Test/Unit/Model/FilesystemTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Deploy\Test\Unit\Model;

class FilesystemTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \Magento\Store\Model\Config\StoreView
*/
private $storeViewMock;

/**
* @var \Magento\Framework\ShellInterface
*/
private $shellMock;

/**
* @var \Magento\User\Model\ResourceModel\User\Collection
*/
private $userCollectionMock;

/**
* @var \Symfony\Component\Console\Output\OutputInterface
*/
private $outputMock;

/**
* @var \Magento\Framework\Filesystem
*/
private $filesystemMock;

/**
* @var \Magento\Framework\Filesystem\Directory\WriteInterface
*/
private $directoryWriteMock;

/**
* @var \Magento\Framework\ObjectManagerInterface
*/
private $objectManagerMock;

/**
* @var \Magento\Deploy\Model\Filesystem
*/
private $filesystem;

/**
* @var string
*/
private $cmdPrefix;

protected function setUp()
{
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);

$this->storeViewMock = $this->getMock(
\Magento\Store\Model\Config\StoreView::class,
[],
[],
'',
false
);
$this->shellMock = $this->getMock(
\Magento\Framework\ShellInterface::class,
[],
[],
'',
false
);
$this->userCollectionMock = $this->getMock(
\Magento\User\Model\ResourceModel\User\Collection::class,
[],
[],
'',
false
);
$this->outputMock = $this->getMock(
\Symfony\Component\Console\Output\OutputInterface::class,
[],
[],
'',
false
);
$this->objectManagerMock = $this->getMock(
\Magento\Framework\ObjectManagerInterface::class,
[],
[],
'',
false
);
$this->filesystemMock = $this->getMock(
\Magento\Framework\Filesystem::class,
[],
[],
'',
false
);
$this->directoryWriteMock = $this->getMock(
\Magento\Framework\Filesystem\Directory\WriteInterface::class,
[],
[],
'',
false
);
$this->filesystemMock->expects($this->any())
->method('getDirectoryWrite')
->willReturn($this->directoryWriteMock);
$this->filesystem = $objectManager->getObject(
\Magento\Deploy\Model\Filesystem::class,
[
'storeView' => $this->storeViewMock,
'shell' => $this->shellMock,
'filesystem' => $this->filesystemMock
]
);

$userCollection = new \ReflectionProperty(\Magento\Deploy\Model\Filesystem::class, 'userCollection');
$userCollection->setAccessible(true);
$userCollection->setValue($this->filesystem, $this->userCollectionMock);

$this->cmdPrefix = 'php -f '. BP . DIRECTORY_SEPARATOR . 'bin' . DIRECTORY_SEPARATOR . 'magento ';
}

public function testRegenerateStatic()
{
$storeLocales = ['fr_FR', 'de_DE', 'nl_NL'];
$adminUserInterfaceLocales = ['de_DE', 'en_US'];
$this->storeViewMock->expects($this->once())
->method('retrieveLocales')
->willReturn($storeLocales);
$userMock = $this->getMock(
\Magento\User\Model\User::class,
[],
[],
'',
false
);
$userMock->expects($this->once())
->method('getInterfaceLocale')
->willReturn('en_US');
$this->userCollectionMock->expects($this->once())
->method('getIterator')
->willReturn(new \ArrayIterator([$userMock]));

$usedLocales = array_unique(
array_merge($storeLocales, $adminUserInterfaceLocales)
);
$staticContentDeployCmd = $this->cmdPrefix . 'setup:static-content:deploy '
. implode(' ', $usedLocales);
$setupDiCompileCmd = $this->cmdPrefix . 'setup:di:compile';
$this->shellMock->expects($this->at(0))
->method('execute')
->with($staticContentDeployCmd);
$this->shellMock->expects($this->at(1))
->method('execute')
->with($setupDiCompileCmd);

$this->outputMock->expects($this->at(0))
->method('writeln')
->with('Starting deployment of static content');
$this->outputMock->expects($this->at(2))
->method('writeln')
->with('Deployment of static content complete');
$this->outputMock->expects($this->at(3))
->method('writeln')
->with('Starting compilation');
$this->outputMock->expects($this->at(5))
->method('writeln')
->with('Compilation complete');

$this->filesystem->regenerateStatic($this->outputMock);
}
}
Loading

0 comments on commit 3cc19fe

Please sign in to comment.