69 lines
1.8 KiB
Text
69 lines
1.8 KiB
Text
edit:add-var create-php~ {|@argv|
|
|
use flag
|
|
use os
|
|
use str
|
|
|
|
var flags rest = (flag:parse $argv [
|
|
[a $false 'Classe abstraite']
|
|
[c $false 'Classe standard']
|
|
[i $false 'Interface']
|
|
[t $false 'Trait']
|
|
])
|
|
|
|
if (!= (count $rest) 1) {
|
|
echo 'Usage: create-php [-a|-c|-i|-t] <path>'
|
|
return
|
|
}
|
|
|
|
var name = $@rest
|
|
var gitRoot = (git rev-parse --show-toplevel)
|
|
var currentDir = (pwd -P)
|
|
var currentDirRelative = (str:replace &max=1 $gitRoot '' $currentDir)
|
|
var isRoot = (==s $currentDirRelative '')
|
|
var isSrc = (==s $currentDirRelative '/src')
|
|
|
|
if (and (not $isRoot) (not $isSrc) (!= (str:index $currentDirRelative '/src/') 0)) {
|
|
fail 'Vous devez être à la racine du projet ou dans un sous-répertoire de src/'
|
|
}
|
|
|
|
var fromRoot = (== (str:index $name 'src/') 0)
|
|
if $fromRoot {
|
|
set name = (str:replace &max=1 'src/' '' $name)
|
|
}
|
|
|
|
var @spl = (str:split '/' $name)
|
|
var @path className = $@spl
|
|
set path = (str:join '/' $path)
|
|
if (not (or $isRoot $isSrc $fromRoot)) {
|
|
set path = (printf '%s/%s' $currentDirRelative $path | str:trim-prefix '/')
|
|
}
|
|
|
|
var fileName = (printf '%s/%s.php' $path $className)
|
|
var fullPath = (printf '%s/src/%s' $gitRoot $fileName)
|
|
if (os:exists &follow-symlink=$true $fullPath) {
|
|
fail 'Le fichier '$fullPath' existe déjà.'
|
|
}
|
|
|
|
os:mkdir-all $gitRoot'/src/'$path
|
|
|
|
var namespace = (str:replace '/' '\' $path)
|
|
var type = 'class'
|
|
if $flags['a'] {
|
|
set type = 'abstract class'
|
|
} elif $flags['i'] {
|
|
set type = 'interface'
|
|
} elif $flags['t'] {
|
|
set type = 'trait'
|
|
}
|
|
var content = [
|
|
'<?php'
|
|
''
|
|
(printf 'namespace App\%s;' $namespace)
|
|
''
|
|
(printf '%s %s' $type $className)
|
|
'{'
|
|
'}'
|
|
]
|
|
|
|
echo (str:join "\n" $content) > $fullPath
|
|
}
|