Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to config AssetBundle in ViewFactory. #49

Closed
terabytesoftw opened this issue Aug 6, 2019 · 9 comments
Closed

How to config AssetBundle in ViewFactory. #49

terabytesoftw opened this issue Aug 6, 2019 · 9 comments
Assignees
Labels
type:feature New feature

Comments

@terabytesoftw
Copy link
Member

How to config AssetBundle in ViewFactory.

Thks,

Q A
Version 3.0
PHP version 7.23
Operating system Windows 10
@samdark
Copy link
Member

samdark commented Aug 7, 2019

Asset bundles part is not finished so I guess it's impossible at this point.

@terabytesoftw
Copy link
Member Author

terabytesoftw commented Aug 18, 2019

Hi, i have managed to put it to work but before opening a pr I want to point out some important things, and I want yours opinions about it:

1.- Yiisoft\Files\FileHelper: [add functions filterPath(), normalizeOptions(), copyDirectory()]
2.- I think we should add a simple UrlHelper and add functions that we need, for example in the assets we need to check if the urls is relative UrlHelper::isRelative(), so we may have to return this check.
3.- AssetManager creates a configuration function instead of passing it all through the constructor.

Example:

web.php:

AssetManager::class => function (ContainerInterface $container) {
    $aliases = $container->get(Aliases::class);
    $assetConfig = [
        'appendTimestamp' => true,
        'forceCopy' => true
    ];
    $assetManager = new AssetManager($aliases);
    $assetManager->setConfig($assetConfig);

    return $assetManager;
 },

AssetManager.php

/**
  * @var array $assetManagerOptions.
  */
private $assetManagerOptions = [
    'afterCopy',
    'appendTimestamp',
    'assetMap',
    'basePath',
    'baseUrl',
    'beforeCopy',
    'bundles',
    'dirMode',
    'fileMode',
    'forceCopy',
    'hashCallback',
    'linkAssets'
 ];

/**
 * setConfig AssetManager.
 *
 * @param array $config
 *
 * @throws \InvalidArgumentException
 *
 * @return void
 */
public function setConfig(array $config): void
{
    foreach ($config as $key => $item) {
        if (in_array($key, $this->assetManagerOptions)) {
            $this->$key = $config["$key"];
        } else {
            throw new \InvalidArgumentException("AssetManager options: ($key) no exist.");
        }
    }
}

ViewFactory.php:

class ViewFactory
{
    public function __invoke(ContainerInterface $container)
    {
        $aliases = $container->get(Aliases::class);
        $assetManager = $container->get(AssetManager::class);
        $eventDispatcher = $container->get(EventDispatcherInterface::class);
        $logger = $container->get(LoggerInterface::class);
        $theme = $container->get(Theme::class);
        $view = $aliases->get('@views');
        $webView = new WebView($view, $theme, $eventDispatcher, $logger);
        $webView->setAssetManager($assetManager);

        return $webView;
    }
}

@samdark
Copy link
Member

samdark commented Aug 19, 2019

  1. OK.
  2. How to access ServerRequest and router in there?
  3. Is there any benefit in it? It could be:
AssetManager::class => [
    '__class' => AssetManager::class,
    '__construct()' => [Reference::to(Aliases::class)],
    'appendTimestamp()' => [true],
    'forceCopy()' => [true],
],

@terabytesoftw
Copy link
Member Author

Hi,

(2): It is correct we do not have access to server request or router, but we need some basic things such as checking a url is relative, absolute, gethostinfo, many of them can be done with $ _REQUEST and $_SERVER, it would be good not to access them directly in views or controllers, these are basic things that I think can be offered, as was done with Method::POST, Method::GET.

(3) With respect to the three if it is better to use simple setters and getters, and change the properties to private.

@samdark
Copy link
Member

samdark commented Aug 19, 2019

$_REQUEST and $_SERVER may not be available. It depends very much of how PSR-7 objects are filled and what environment we are running with. See https://forum.yiiframework.com/t/using-roadrunner-as-a-server/127060/3. I want to still be able doing that so want to avoid assuming anything about where request is coming from.

@terabytesoftw
Copy link
Member Author

terabytesoftw commented Aug 20, 2019

I've been reviewing the issues in Yii2 and beforeCopy is only used to avoid empty directories, because they don't use the option [only] that only copies the files we need, the assets are full of a lot of complex logic, I think it should be simpler, that They think.

    public $publishOptions = [
        'only' => [
            'css/all.css',
            'css/fontawesome.css',
            'webfonts/*',
        ],
    ];

@samdark
Copy link
Member

samdark commented Aug 20, 2019

Yes. Could be like that.

@terabytesoftw
Copy link
Member Author

terabytesoftw commented Aug 23, 2019

I have advanced a lot and I am going with the tests and I want to consult you some things.

For example the position tests in the views use:

public function testPositionDependency($pos, $jqAlreadyRegistered)
{
    $view->getAssetManager()->bundles['TestAssetBundle'] = [
        'jsOptions' => [
            'position' => $pos,
        ],
    ];
}

Now the propierties is private and we cannot dynamically change the behavior which seems to me to be the right thing, if you configure the position within the asset the tests work correctly, I think that to change the position dynamically it must be used.

WebView->registerJsFile()
WebView->registerJCssFile()

So if this will be the correct way to do it, I will modify the tests to upload the modifications.

@samdark
Copy link
Member

samdark commented Aug 23, 2019

  1. jQuery should not be our dependency.
  2. Your changes seem to be OK.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:feature New feature
Projects
None yet
Development

No branches or pull requests

2 participants