{"action":"created","issue":{"url":"https://api.github.com/repos/phalcon/cphalcon/issues/2009","repository_url":"https://api.github.com/repos/phalcon/cphalcon","labels_url":"https://api.github.com/repos/phalcon/cphalcon/issues/2009/labels{/name}","comments_url":"https://api.github.com/repos/phalcon/cphalcon/issues/2009/comments","events_url":"https://api.github.com/repos/phalcon/cphalcon/issues/2009/events","html_url":"https://github.com/phalcon/cphalcon/issues/2009","id":27232929,"number":2009,"title":"\\Assets\\Collection: Helpers for combining JS/CSS","user":{"login":"kolypto","id":2234216,"avatar_url":"https://avatars.githubusercontent.com/u/2234216?v=3","gravatar_id":"","url":"https://api.github.com/users/kolypto","html_url":"https://github.com/kolypto","followers_url":"https://api.github.com/users/kolypto/followers","following_url":"https://api.github.com/users/kolypto/following{/other_user}","gists_url":"https://api.github.com/users/kolypto/gists{/gist_id}","starred_url":"https://api.github.com/users/kolypto/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kolypto/subscriptions","organizations_url":"https://api.github.com/users/kolypto/orgs","repos_url":"https://api.github.com/users/kolypto/repos","events_url":"https://api.github.com/users/kolypto/events{/privacy}","received_events_url":"https://api.github.com/users/kolypto/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":3,"created_at":"2014-02-09T22:21:52Z","updated_at":"2016-12-31T04:48:34Z","closed_at":null,"body":"Most web projects need to serve their JS & CSS in a combined and, possibly, minified state. As this task is very common, Phalcon, being a mature framework, needs to support some automation out of the box.\n\nThe following steps should be taken:\n- Each bundle of assets should be merged into a separate file\n- Merged file name should depend on the input files and, in development mode, their contents\n- No action should be taken if the output file already exists and is up to date.\n\nAs JS is part of the presentation, in comtempt of Phalcon documentation, developers will set JS & CSS links in templates. Thus, this kind of processing can't happen in any event handler: only in the template.\n\nTo solve the above, I had to extend `Manager`:\n\n``` php\n<?php namespace lib\\Phalcon\\Assets;\n\nuse \\Phalcon\\Assets;\n\nclass Manager extends Assets\\Manager {\n    # TODO: outputCssMin()\n\n    function outputJsMin($collectionName = null){\n        $options = $this->getOptions();\n        $sourceBasePath = $options['sourceBasePath'];\n        $targetBasePath = $options['targetBasePath'];\n        $targetBaseUri = $options['targetBaseUri'];\n\n        # Collection\n        $collection = $collectionName\n            ? $this->get($collectionName)\n            : $this->getJs();\n\n        # TODO: remove duplicates\n\n        # Make up the output filename: md5(*filenames)\n        $target_name = '';\n        foreach ($collection as $js)\n            $target_name .= $js->getPath();\n        $target_name = md5($target_name).'.js';\n\n        # Set up the collection so it combines & minifies\n        $collection\n            ->setTargetPath($target_name)\n            ->setPrefix($targetBaseUri)\n            ->setTargetUri($target_name)\n            ->join(true)\n            ->addFilter(new \\Phalcon\\Assets\\Filters\\Jsmin())\n        ;\n\n        # If output file exists, do not recompile it\n        if (file_exists($collection->getTargetPath())){\n            # Replace the collection with a single file: the generated one\n            $col = new Assets\\Collection();\n            $col->addJs($collection->getTargetUri());\n            $collection = $col; # this collection will be outputted instead\n        }\n\n        # Output\n        return $this->output($collection, [\"Phalcon\\\\Tag\", \"javascriptInclude\"], \"js\");\n    }\n}\n```\n\nProvided all assets live in 'web/i/{js,css}', and the generated files go into '/web/assets/', the following initialization should be done:\n\n``` php\n<?php\n$di->setShared('assets', function(){\n    $assets = new \\lib\\Phalcon\\Assets\\Manager([\n        'sourceBasePath' => 'web/',\n        'targetBasePath' => 'web/assets/',\n        'targetBaseUri' => 'assets/'  # new option for the generated files\n    ]);\n    $assets->collection('css')\n        ->setSourcePath('i/css/')\n        ->setPrefix('i/css/');\n    $assets->collection('js')\n        ->setSourcePath('i/js/')\n        ->setPrefix('i/js/');\n    return $assets;\n});\n```\n\nI believe this should be a part of the framework, so one can use a one-liner to do all the above.\n\nSome drawbacks:\n- Does not currently allow to specify another filter class\n- Lacks some flexibility\n## <bountysource-plugin>\n\nWant to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/1896457-assets-collection-helpers-for-combining-js-css?utm_campaign=plugin&utm_content=tracker%2F50707&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F50707&utm_medium=issues&utm_source=github).\n</bountysource-plugin>\n"},"comment":{"url":"https://api.github.com/repos/phalcon/cphalcon/issues/comments/269849340","html_url":"https://github.com/phalcon/cphalcon/issues/2009#issuecomment-269849340","issue_url":"https://api.github.com/repos/phalcon/cphalcon/issues/2009","id":269849340,"user":{"login":"arnoldbird","id":1728052,"avatar_url":"https://avatars.githubusercontent.com/u/1728052?v=3","gravatar_id":"","url":"https://api.github.com/users/arnoldbird","html_url":"https://github.com/arnoldbird","followers_url":"https://api.github.com/users/arnoldbird/followers","following_url":"https://api.github.com/users/arnoldbird/following{/other_user}","gists_url":"https://api.github.com/users/arnoldbird/gists{/gist_id}","starred_url":"https://api.github.com/users/arnoldbird/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/arnoldbird/subscriptions","organizations_url":"https://api.github.com/users/arnoldbird/orgs","repos_url":"https://api.github.com/users/arnoldbird/repos","events_url":"https://api.github.com/users/arnoldbird/events{/privacy}","received_events_url":"https://api.github.com/users/arnoldbird/received_events","type":"User","site_admin":false},"created_at":"2016-12-31T04:48:34Z","updated_at":"2016-12-31T04:48:34Z","body":"I tend to think this should be part of the framework.  Initially, I thought the addCss method did this.  Took me a little while to figure out that all the addCss method does is add the tag to the head."}}
{"actor":{"display_login":"arnoldbird"}}