Skip to content

Commit 0bcda45

Browse files
fix(theme): Create same theme name in another vendor
fix #17
1 parent 4f22912 commit 0bcda45

File tree

2 files changed

+48
-33
lines changed

2 files changed

+48
-33
lines changed

src/Console/Generators/MakeTheme.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ protected function validateName()
122122
}
123123
}
124124

125-
if (ThemesManager::has($this->theme['name'])) {
125+
if (ThemesManager::has("{$this->theme['vendor']}/{$this->theme['name']}")) {
126126
$this->error("Theme with name {$this->theme['vendor']}/{$this->theme['name']} already exists!");
127127

128128
return false;
@@ -173,7 +173,7 @@ private function generateTheme()
173173
* Replace placeholders in generated file.
174174
*
175175
* @param \Symfony\Component\Finder\SplFileInfo $file
176-
*
176+
*
177177
* @return string
178178
*/
179179
protected function replacePlaceholders($file)
@@ -233,15 +233,9 @@ protected function askDescription()
233233
*/
234234
protected function askName()
235235
{
236-
while (empty(Arr::get($this->theme, 'name', null))) {
236+
do {
237237
$this->theme['name'] = $this->ask('Theme Name');
238-
239-
if (ThemesManager::has($this->theme['name'])) {
240-
$this->error("Theme with name {$this->theme['name']} already exists!");
241-
242-
unset($this->theme['name']);
243-
}
244-
}
238+
} while(!strlen($this->theme['name']));
245239
}
246240

247241
/**
@@ -264,7 +258,9 @@ protected function askParent()
264258
*/
265259
protected function askVendor()
266260
{
267-
$this->theme['vendor'] = mb_strtolower($this->config->get('themes-manager.composer.vendor') ?? $this->ask('Vendor name'));
261+
do {
262+
$this->theme['vendor'] = mb_strtolower($this->config->get('themes-manager.composer.vendor') ?? $this->ask('Vendor name'));
263+
} while(!strlen($this->theme['vendor']));
268264
}
269265

270266
/**
@@ -273,5 +269,9 @@ protected function askVendor()
273269
protected function askVersion()
274270
{
275271
$this->theme['version'] = $this->ask('Version number');
272+
273+
if (!strlen($this->theme['version'])) {
274+
$this->theme['version'] = null;
275+
}
276276
}
277277
}

src/ThemesManager.php

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -101,22 +101,13 @@ public function clearCache(): bool
101101
* Check if theme with given name exists.
102102
*
103103
* @param string $name
104+
* @param string $vendor
104105
*
105106
* @return boolean
106107
*/
107108
public function has(string $name = null)
108109
{
109-
return !is_null($this->themes->first(function ($theme) use ($name) {
110-
// normalize module name
111-
$name = str_replace(['-theme', 'theme-'], '', $name);
112-
// Check if $name contains vendor
113-
if (strpos($name, '/') !== false) {
114-
return Str::lower($theme->getName()) === Str::lower(substr($name, $pos + 1, strlen($name)));
115-
;
116-
} else {
117-
return $theme->getLowerName() === Str::lower($name);
118-
}
119-
}));
110+
return !is_null($this->findByName($name));
120111
}
121112

122113
/**
@@ -131,17 +122,7 @@ public function get(string $name = null)
131122
if (is_null($name)) {
132123
return $this->themes;
133124
} else {
134-
return $this->themes->first(function ($theme) use ($name) {
135-
// normalize module name
136-
$name = str_replace(['-theme', 'theme-'], '', $name);
137-
// Check if $name contains vendor
138-
if (strpos($name, '/') !== false) {
139-
return Str::lower($theme->getName()) === Str::lower(substr($name, $pos + 1, strlen($name)));
140-
;
141-
} else {
142-
return $theme->getLowerName() === Str::lower($name);
143-
}
144-
});
125+
return $this->findByName($name);
145126
}
146127
}
147128

@@ -373,6 +354,40 @@ private function htmlAttributes($attributes): string
373354
}, array_keys($attributes)));
374355
}
375356

357+
/**
358+
* Find a theme by given name and vendor (optional)
359+
* name can include vendor prefix (ie: hexadog/default)
360+
* If no vendor provided and name not prefixed by vendor
361+
* the first theme with given name is returned
362+
*
363+
* @param string $name
364+
* @return void
365+
*/
366+
protected function findByName(string $name = null, string $vendor = null)
367+
{
368+
if (is_null($name)) {
369+
return null;
370+
}
371+
372+
return $this->themes->first(function ($theme) use ($name, $vendor) {
373+
// normalize module name
374+
$name = str_replace(['-theme', 'theme-'], '', $name);
375+
// Check if $name contains vendor
376+
if (($pos = strpos($name, '/')) !== false) {
377+
$vendor = substr($name, 0, $pos);
378+
$name = substr($name, $pos + 1, strlen($name));
379+
380+
return Str::lower($theme->getName()) === Str::lower($name) && $theme->getLowerVendor() === Str::lower($vendor);
381+
} else {
382+
if (is_null($vendor)) {
383+
return $theme->getLowerName() === Str::lower($name);
384+
} else {
385+
return $theme->getLowerName() === Str::lower($name) && $theme->getLowerVendor() === Str::lower($vendor);
386+
}
387+
}
388+
});
389+
}
390+
376391
/**
377392
* Find all available themes.
378393
*

0 commit comments

Comments
 (0)