-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
[5.4] Refactoring mix method (bis). #19666
Changes from 7 commits
66ddbca
ddbad7a
e683b62
c31cc24
6fa8a17
b6e2d33
5de7ef5
7127ec3
5111036
f8fb58b
1828cc4
c16c217
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,271 @@ | ||
<?php | ||
|
||
namespace Illuminate\View\Mix; | ||
|
||
use Illuminate\Support\Str; | ||
use Illuminate\Support\HtmlString; | ||
|
||
class Mix | ||
{ | ||
/** | ||
* The sanitized path to the asset. | ||
* | ||
* @var string | ||
*/ | ||
protected $path; | ||
|
||
/** | ||
* The cached manifest. | ||
* | ||
* @var array | ||
*/ | ||
protected $manifest; | ||
|
||
/** | ||
* The directory where the assets and the manifest file are. | ||
* | ||
* @var string | ||
*/ | ||
protected $manifestDirectory; | ||
|
||
/** | ||
* The cache of mix state. | ||
* | ||
* @var bool | ||
*/ | ||
protected $disabled = false; | ||
|
||
/** | ||
* The URI of HMR server. | ||
* | ||
* @var string | ||
*/ | ||
protected $hmrURI = '//localhost:8080'; | ||
|
||
/** | ||
* The name of file which prove that HMR is enabled. | ||
* | ||
* @var string | ||
*/ | ||
protected $hmrFilename = '/hot'; | ||
|
||
/** | ||
* The name of Mix Manifest file. | ||
* | ||
* @var string | ||
*/ | ||
protected $manifestFilename = '/mix-manifest.json'; | ||
|
||
/** | ||
* Get the path to a versioned Mix asset or a simple message if mix is disabled. | ||
* | ||
* @param string $path | ||
* @param string $manifestDirectory | ||
* @return \Illuminate\Support\HtmlString | ||
*/ | ||
public function mix($path, $manifestDirectory = '') | ||
{ | ||
if ($this->disabled) { | ||
return $this->disabledPath(); | ||
} | ||
|
||
return $this->getRealPath($path, $manifestDirectory); | ||
} | ||
|
||
/** | ||
* Get the path to a versioned Mix file. | ||
* | ||
* @param string $path | ||
* @param string $manifestDirectory | ||
* @return \Illuminate\Support\HtmlString | ||
*/ | ||
protected function getRealPath($path, $manifestDirectory) | ||
{ | ||
$this->init($path, $manifestDirectory); | ||
|
||
if ($this->hmrModeEnabled()) { | ||
return $this->hmrPath(); | ||
} | ||
|
||
return $this->compiledPath(); | ||
} | ||
|
||
/** | ||
* Set a sanitized version of assets. | ||
* | ||
* @param string $path | ||
* @param string $manifestDirectory | ||
* @return void | ||
*/ | ||
protected function init($path, $manifestDirectory) | ||
{ | ||
$this->path = $this->sanitize($path); | ||
$this->manifestDirectory = $this->sanitize($manifestDirectory); | ||
} | ||
|
||
/** | ||
* Get a sanitized version of a path. | ||
* | ||
* @param string $path | ||
* @return string | ||
*/ | ||
protected function sanitize($path) | ||
{ | ||
if ($path && ! Str::startsWith($path, '/')) { | ||
$path = "/{$path}"; | ||
} | ||
|
||
return $path; | ||
} | ||
|
||
/** | ||
* Check if the HRM mode of Mix is enabled. | ||
* | ||
* @return bool | ||
*/ | ||
protected function hmrModeEnabled() | ||
{ | ||
return file_exists(public_path($this->manifestDirectory.$this->hmrFilename)); | ||
} | ||
|
||
/** | ||
* Get the full path to the file through the HMR server. | ||
* | ||
* @return \Illuminate\Support\HtmlString | ||
*/ | ||
protected function hmrPath() | ||
{ | ||
return new HtmlString($this->hmrURI.$this->path); | ||
} | ||
|
||
/** | ||
* Get the full path to the compiled file. | ||
* | ||
* @return \Illuminate\Support\HtmlString | ||
*/ | ||
protected function compiledPath() | ||
{ | ||
return new HtmlString($this->manifestDirectory.$this->getPathFromManifest()); | ||
} | ||
|
||
/** | ||
* Get a message instead of the path when mix is disabled. | ||
* | ||
* @return \Illuminate\Support\HtmlString | ||
*/ | ||
protected function disabledPath() | ||
{ | ||
return new HtmlString('Mix is disabled!'); | ||
} | ||
|
||
/** | ||
* Get the path from the manifest file. | ||
* | ||
* @return string | ||
* | ||
* @throws \Illuminate\View\Mix\MixException | ||
*/ | ||
protected function getPathFromManifest() | ||
{ | ||
if (! array_key_exists($this->path, $manifest = $this->getManifest())) { | ||
throw new MixException( | ||
"Unable to locate Mix file: {$this->path}. Please check your ". | ||
'webpack.mix.js output paths and try again.' | ||
); | ||
} | ||
|
||
return $manifest[$this->path]; | ||
} | ||
|
||
/** | ||
* Load the manifest file. | ||
* | ||
* @return array | ||
* | ||
* @throws \Illuminate\View\Mix\MixException | ||
*/ | ||
protected function getManifest() | ||
{ | ||
if (! $this->manifest) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to go further if if ($this->manifest) {
return $this->manifest;
}
if (! file_exists($manifestPath = public_path($this->manifestDirectory.$this->manifestFilename))) {
throw new MixException('The Mix manifest does not exist.');
}
$this->manifest = json_decode(file_get_contents($manifestPath), true);
if (json_last_error() === JSON_ERROR_NONE) {
return $this->manifest;
}
throw new MixException("The Mix manifest isn't a proper json file."); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's your code right here, you made me change mine to yours for no valid reason, and now you're again changing your mind. It's just about a basic refactoring and you're commenting every line I'm writing for 3 days each time I make a new commit. I have work to do, and I haven't not enough time to satisfy all your dreams on a basic function like this one. So, I don't know what's your goal, but please stop, or do it yourself! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It's a great PR but a critical function. |
||
if (! file_exists($manifestPath = public_path($this->manifestDirectory.$this->manifestFilename))) { | ||
throw new MixException('The Mix manifest does not exist.'); | ||
} | ||
|
||
$this->manifest = json_decode(file_get_contents($manifestPath), true); | ||
|
||
if (json_last_error() !== JSON_ERROR_NONE) { | ||
throw new MixException('The Mix manifest isn\'t a proper json file.'); | ||
} | ||
} | ||
|
||
return $this->manifest; | ||
} | ||
|
||
/** | ||
* Disable the mix function (in case of tests for example). | ||
* | ||
* @param bool $disabled | ||
* @return $this | ||
*/ | ||
public function disable($disabled = true) | ||
{ | ||
$this->disabled = $disabled; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Enable the mix function (in case of it was disabled before). | ||
* | ||
* @param bool $enabled | ||
* @return $this | ||
*/ | ||
public function enable($enabled = true) | ||
{ | ||
$this->disabled = ! $enabled; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Set the URI of HMR sever. | ||
* | ||
* @param string $hmrURI | ||
* | ||
* @return $this | ||
*/ | ||
public function setHmrURI($hmrURI) | ||
{ | ||
$this->hmrURI = $hmrURI; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Set the Mix Manifest filename. | ||
* | ||
* @param string $manifestFilename | ||
* | ||
* @return Mix | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We use |
||
*/ | ||
public function setManifestFilename($manifestFilename) | ||
{ | ||
$this->manifestFilename = $this->sanitize($manifestFilename); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Set the HMR hot filename. | ||
* | ||
* @param string $hmrFilename | ||
* | ||
* @return Mix | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We use |
||
*/ | ||
public function setHmrFilename($hmrFilename) | ||
{ | ||
$this->hmrFilename = $this->sanitize($hmrFilename); | ||
|
||
return $this; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace Illuminate\View\Mix; | ||
|
||
use Exception; | ||
|
||
class MixException extends Exception | ||
{ | ||
// | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it looks strange to do
(new Mix)->mix()
why not something more like(new Mix)->create()
..There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep or
generate
maybe?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I thought about that.
create
andgenerate
aren't good ones either because you don't create or generate anything, you just find the good path in a file, sofind
? I didn't find something I liked, so I kept that because in any way, the method will always be called by themix()
helper, as usual.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about
load
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could use
resolve()
. If Taylor merges it, he could always rename it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I love
resolve
thank you! 🎉