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

PHP ^7.1 and CssMap Trait #2

Merged
merged 6 commits into from
Apr 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
{
"name": "adt/utils",
"description": "Utilities",

"authors": [
{
"name": "studnaa",
"email": "[email protected]"
}
],

"autoload": {
"classmap": ["src/"]
"name": "adt/utils",
"description": "Utilities",
"authors": [
{
"name": "studnaa",
"email": "[email protected]"
}

],
"autoload": {
"classmap": [
"src/"
]
},
"require": {
"php": "^7.1",
"ext-json": "*"
}
}
46 changes: 46 additions & 0 deletions src/CssMap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
namespace ADT\Utils;


trait CssMap {
/**
* Automatically adds className map to template on attached
*/
public function injectCssMapFromWebpack() {
$this->onAfterAttached[] = function() {
$this->template->className = $this->loadFromWebpack();
};
}

/**
* Looks for $moduleName.module.scss.json in current directory and returns
* content as array on success
* @param string $moduleName
* @param string|null $dir
* @return array
*/
private function loadFromWebpack(string $moduleName = 'index', string $dir = NULL): array {
$dir = $dir ?? dirname($this->getReflection()->getFileName());
$filePath = $dir . "/$moduleName.module.scss.json";

if (! file_exists($filePath)) {
throw new \InvalidArgumentException("Css module file not found in ". $filePath);
}

$file = file_get_contents($filePath);

if ($file === false) {
throw new \InvalidArgumentException('Css Module file not found in ' . $filePath);
}

$decodedModule = json_decode($file, true);

if (json_last_error() !== JSON_ERROR_NONE) {
throw new \UnexpectedValueException(
'Css Module file is not valid json file: ' . json_last_error_msg()
);
}

return $decodedModule;
}
}