Skip to content

Commit

Permalink
Merge pull request #4 from ishanvyas22/feedback-changes
Browse files Browse the repository at this point in the history
Plugin name change, minor enhancements
  • Loading branch information
ishanvyas22 authored Oct 31, 2019
2 parents 680fbab + bc7ac2d commit 2baa9fb
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 82 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Mix plugin for CakePHP
# AssetMix plugin for CakePHP

Provides integration with your CakePHP application & [Laravel Mix](https://laravel-mix.com).

Expand All @@ -12,11 +12,11 @@ Provides integration with your CakePHP application & [Laravel Mix](https://larav
```
2. Load plugin using below command:
```bash
bin/cake plugin load Mix -b
bin/cake plugin load AssetMix
```
3. Load `Mix` helper from the plugin into your `AppView.php` file:
3. Load `AssetMix` helper from the plugin into your `AppView.php` file:
```php
$this->loadHelper('Mix.Mix');
$this->loadHelper('AssetMix.AssetMix');
```

## Usage
Expand All @@ -26,21 +26,21 @@ After compiling your assets(js, css) with laravel mix, it creates a `mix-manifes
- To generate script tag for compiled javascript file(s):

```php
echo $this->Mix->script('app');
echo $this->AssetMix->script('app');
```

Above code will render:

```html
<script src="/js/app.js"></script>
<script src="/js/app.js" defer="defer"></script>
```

As you can see it works same as [Html helper](https://book.cakephp.org/3.0/en/views/helpers/html.html#linking-to-javascript-files). There is not need to provide full path or even file extension.

- To generate style tag for compiled css file(s):

```php
echo $this->Mix->css('main');
echo $this->AssetMix->css('main');
```

Output:
Expand Down
11 changes: 8 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,18 @@
},
"autoload": {
"psr-4": {
"Mix\\": "src/"
}
"AssetMix\\": "src/"
},
"files": ["src/functions.php"]
},
"autoload-dev": {
"psr-4": {
"Mix\\Test\\": "tests/",
"AssetMix\\Test\\": "tests/",
"Cake\\Test\\": "vendor/cakephp/cakephp/tests/"
}
},
"scripts": {
"test": "phpunit --colors=always",
"test-setup": "cp composer.json composer.backup && composer require --dev phpunit/phpunit:\"^5.7.14|^6.0\" && mv composer.backup composer.json"
}
}
6 changes: 0 additions & 6 deletions config/bootstrap.php

This file was deleted.

4 changes: 2 additions & 2 deletions config/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
use Cake\Routing\Route\DashedRoute;

Router::plugin(
'Mix',
['path' => '/mix'],
'AssetMix',
['path' => '/asset-mix'],
function (RouteBuilder $routes) {
$routes->fallbacks(DashedRoute::class);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Mix.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace Mix;
namespace AssetMix;

use Exception;

Expand All @@ -20,7 +20,7 @@ class Mix
public function __invoke($path, $manifestDirectory = '')
{
static $manifests = [];
$publicPath = $_SERVER['DOCUMENT_ROOT'];
$publicPath = env('DOCUMENT_ROOT');

if (! starts_with($path, '/')) {
$path = "/{$path}";
Expand Down Expand Up @@ -50,7 +50,7 @@ public function __invoke($path, $manifestDirectory = '')

$manifest = $manifests[$manifestPath];
if (! isset($manifest[$path])) {
throw new Exception("Unable to locate Mix file: {$path}.");
throw new Exception("Unable to locate AssetMix file: {$path}.");
}

return $manifestDirectory . $manifest[$path];
Expand Down
4 changes: 2 additions & 2 deletions src/Plugin.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

namespace Mix;
namespace AssetMix;

use Cake\Core\BasePlugin;

/**
* Plugin for Mix
* Plugin for AssetMix
*/
class Plugin extends BasePlugin
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<?php
namespace Mix\View\Helper;
namespace AssetMix\View\Helper;

use Cake\View\Helper;
use Mix\Mix;
use AssetMix\Mix;

/**
* Mix helper
* AssetMix helper
*
* @property \Cake\View\Helper\HtmlHelper $Html
* @property \Cake\View\Helper\UrlHelper $Url
*/
class MixHelper extends Helper
class AssetMixHelper extends Helper
{
/**
* Default configuration.
Expand Down Expand Up @@ -56,6 +56,8 @@ public function css(string $path, array $options = [])
*/
public function script(string $url, array $options = [])
{
$options = array_merge($options, ['defer' => true]);

// Get css file path, add extension if not provided, skip if url provided
if (strpos($url, '//') !== false) {
return $this->Html->script($url, $options);
Expand Down
File renamed without changes.
56 changes: 56 additions & 0 deletions tests/TestCase/View/Helper/AssetMixHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
namespace AssetMix\Test\TestCase\View\Helper;

use Cake\View\View;
use Cake\TestSuite\TestCase;
use AssetMix\View\Helper\AssetMixHelper;

/**
* AssetMix\View\Helper\AssetMixHelper Test Case
*/
class AssetMixHelperTest extends TestCase
{
/**
* Test subject
*
* @var \AssetMix\View\Helper\AssetMixHelper
*/
public $AssetMix;

/**
* setUp method
*
* @return void
*/
public function setUp()
{
$this->_compareBasePath = APP . 'tests' . DS . 'comparisons' . DS;

parent::setUp();

$view = new View();
$this->AssetMix = new AssetMixHelper($view);
}

/**
* tearDown method
*
* @return void
*/
public function tearDown()
{
unset($this->AssetMix);

parent::tearDown();
}

/**
* Test script function returns proper tag without versioning
*
* @return void
*/
public function testScriptTagWithoutVersion()
{
$this->AssetMix->css('main');
}
}
53 changes: 0 additions & 53 deletions tests/TestCase/View/Helper/MixHelperTest.php

This file was deleted.

4 changes: 2 additions & 2 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Test suite bootstrap for Mix.
* Test suite bootstrap for AssetMix.
*
* This function is used to find the location of CakePHP whether CakePHP
* has been installed as a dependency of the plugin, or the plugin is itself
Expand Down Expand Up @@ -35,4 +35,4 @@
require $root . '/config/bootstrap.php';

return;
}
}

0 comments on commit 2baa9fb

Please sign in to comment.