Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
S
sidekick
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Privat - Marco Schmiedel
sidekick
Commits
fe26e9f0
Commit
fe26e9f0
authored
Dec 23, 2021
by
Marco Schmiedel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
finish stage 1
parent
390e32b2
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
1111 additions
and
1048 deletions
+1111
-1048
.gitignore
src/Commands/CodebeautifierCommand/.gitignore
+1
-0
CodebeautifierCommand.php
src/Commands/CodebeautifierCommand/CodebeautifierCommand.php
+16
-1033
BeautifyManager.php
src/Managers/BeautifyManager/BeautifyManager.php
+1055
-0
Template.tpl
src/Managers/BeautifyManager/Template.tpl
+0
-0
TerminalManager.php
src/Managers/TerminalManager/TerminalManager.php
+39
-15
No files found.
src/Commands/CodebeautifierCommand/.gitignore
0 → 100644
View file @
fe26e9f0
BACKUP_*
src/Commands/CodebeautifierCommand/CodebeautifierCommand.php
View file @
fe26e9f0
...
@@ -10,7 +10,7 @@
...
@@ -10,7 +10,7 @@
| sidekick package.
| sidekick package.
|
|
| Author: Marco Schmiedel <marco.schmiedel@itmax.email>
| Author: Marco Schmiedel <marco.schmiedel@itmax.email>
| Date: 2021-12-2
0 16:46:13
| Date: 2021-12-2
3 19:50:42
|
|
*/
*/
namespace
Ceetrox\Commands\CodebeautifierCommand
;
namespace
Ceetrox\Commands\CodebeautifierCommand
;
...
@@ -22,6 +22,7 @@
...
@@ -22,6 +22,7 @@
|------------------------------------------------------------------------------------------------
|------------------------------------------------------------------------------------------------
*/
*/
use
Ceetrox\Managers\TerminalManager\TerminalManager
;
use
Ceetrox\Managers\TerminalManager\TerminalManager
;
use
Ceetrox\Managers\BeautifyManager\BeautifyManager
;
use
Illuminate\Console\Command
;
use
Illuminate\Console\Command
;
...
@@ -64,1062 +65,44 @@
...
@@ -64,1062 +65,44 @@
|----------------------------------------------------------------------------------------
|----------------------------------------------------------------------------------------
| Check if the given file exists.
| Check if the given file exists.
*/
*/
while
(
!
isset
(
$fileexists
))
if
(
!
file_exists
(
$file
))
{
{
if
(
file_exists
(
$file
))
{
$fileexists
=
true
;
}
else
{
/*
|--------------------------------------------------------------------------------
| Ask again if the file does not exist.
*/
$file
=
$this
->
ask
(
'Bitte geben Sie den absoluten Pfad zum gewünschten Script ein'
);
}
}
/*
|----------------------------------------------------------------------------------------
| Boot the Beautifier class.
*/
$beauty
=
new
Beautifier
();
/*
|----------------------------------------------------------------------------------------
| Analyse the given file.
*/
$beauty
->
analyseDocument
(
$file
);
/*
|----------------------------------------------------------------------------------------
| Render the given file.
*/
$beauty
->
renderDocument
(
$file
);
}
}
/*
|------------------------------------------------------------------------------------------------
| Class "Beautifier"
|------------------------------------------------------------------------------------------------
*/
class
Beautifier
{
/*
|--------------------------------------------------------------------------------------------
| Variables
|--------------------------------------------------------------------------------------------
*/
var
$content
=
[];
var
$classes
=
[];
var
$comments
=
[];
var
$linedeph
=
1
;
/*
|--------------------------------------------------------------------------------------------
| Method "analyseDocument"
|--------------------------------------------------------------------------------------------
*/
function
analyseDocument
(
$file
)
{
/*
|----------------------------------------------------------------------------------------
| Read the php file.
*/
$this
->
content
=
file_get_contents
(
$file
);
/*
|----------------------------------------------------------------------------------------
| Read all base parameters.
*/
$this
->
scanBasics
();
/*
|----------------------------------------------------------------------------------------
| Read all comments.
*/
$this
->
scanComments
();
/*
|----------------------------------------------------------------------------------------
| Read all classes.
*/
$this
->
scanClass
();
/*
|----------------------------------------------------------------------------------------
| Read all methods.
*/
$this
->
scanMethods
();
/*
|----------------------------------------------------------------------------------------
| Return the full analyse.
*/
return
$this
->
classes
;
}
/*
|--------------------------------------------------------------------------------------------
| Method "scanBasics"
|--------------------------------------------------------------------------------------------
| This method is used to read the basic parameters from the file (for example namespace and
| dependencies).
*/
function
scanBasics
()
{
/*
|----------------------------------------------------------------------------------------
| read namespace
*/
$preg
=
preg_match
(
'/(^|\s)+namespace\s+[A-Za-z\\\]+;/'
,
$this
->
content
,
$matches
,
PREG_OFFSET_CAPTURE
);
if
(
isset
(
$matches
[
0
]))
{
$this
->
namespace
=
trim
(
str_replace
(
"
\n
"
,
''
,
$matches
[
0
][
0
]));
}
else
{
$this
->
namespace
=
false
;
}
/*
|----------------------------------------------------------------------------------------
| read dependencies
*/
$preg
=
preg_match_all
(
'/(^|\s)+use\s+[^"\'\n]+[;]/'
,
$this
->
content
,
$matches
,
PREG_OFFSET_CAPTURE
);
if
(
isset
(
$matches
[
0
]))
{
$this
->
relations
=
array
();
foreach
(
$matches
[
0
]
as
$relation
)
{
$this
->
relations
[
md5
(
$relation
[
0
])]
=
trim
(
str_replace
(
"
\n
"
,
''
,
$relation
[
0
]));
}
}
else
{
$this
->
relations
=
false
;
}
}
/*
|--------------------------------------------------------------------------------------------
| Method "getCommentFromPregMatch"
|--------------------------------------------------------------------------------------------
| With this method a string that has been hashed by "scanClass" can be converted back.
*/
function
getCommentFromPregMatch
(
$pattern
,
$string
)
{
/*
|----------------------------------------------------------------------------------------
| check pattern
*/
preg_match
(
$pattern
,
$string
,
$description
,
PREG_OFFSET_CAPTURE
);
$result
=
false
;
/*
|----------------------------------------------------------------------------------------
| load description from cache
*/
if
(
isset
(
$description
[
0
][
0
]))
{
preg_match
(
'/(?<=(#COMMENT-)).+(?=(-COMMENT#))/'
,
$description
[
0
][
0
],
$md5
,
PREG_OFFSET_CAPTURE
);
if
(
isset
(
$md5
[
0
][
0
]))
{
$result
=
$this
->
comments
[
$md5
[
0
][
0
]];
}
}
/*
|----------------------------------------------------------------------------------------
| normalize description
*/
$result
=
$this
->
normalize
(
$result
);
return
$result
;
}
/*
|--------------------------------------------------------------------------------------------
| Method "matchBetweenTwoPatterns"
|--------------------------------------------------------------------------------------------
| This method filters a string between 2 regex patterns and returns it.
*/
function
matchBetweenTwoPatterns
(
$string
,
$startPattern
,
$stopPattern
,
$debug
=
false
)
{
/*
|----------------------------------------------------------------------------------------
| Create empty array for the following patterns..
*/
$informations
=
array
();
/*
|----------------------------------------------------------------------------------------
| Filter all start patterns.
*/
$start
=
preg_match_all
(
$startPattern
,
$string
,
$startResults
,
PREG_OFFSET_CAPTURE
);
if
(
$start
==
false
)
{
return
false
;
}
foreach
(
$startResults
[
0
]
as
$result
)
{
preg_match_all
(
'/.*/s'
,
$string
,
$fullResult
,
PREG_OFFSET_CAPTURE
,
$result
[
1
]);
$informations
[
$result
[
1
]
.
'2'
]
=
[
'Offset'
=>
$result
[
1
],
'Type'
=>
'start'
,
'String'
=>
$fullResult
[
0
][
0
][
0
]];
}
/*
|----------------------------------------------------------------------------------------
| Filter all stop patterns.
*/
$stop
=
preg_match_all
(
$stopPattern
,
$string
,
$stopResults
,
PREG_OFFSET_CAPTURE
);
if
(
$stop
==
false
)
{
return
false
;
}
foreach
(
$stopResults
[
0
]
as
$result
)
{
preg_match_all
(
'/.*/s'
,
$string
,
$fullResult
,
PREG_OFFSET_CAPTURE
,
$result
[
1
]);
$informations
[
$result
[
1
]
.
'1'
]
=
[
'Offset'
=>
$result
[
1
],
'Type'
=>
'stop'
,
'String'
=>
$fullResult
[
0
][
0
][
0
]];
}
/*
|----------------------------------------------------------------------------------------
| Sort all informations in the right order.
*/
ksort
(
$informations
);
/*
|----------------------------------------------------------------------------------------
| Create an array for the results.
*/
$result
=
array
();
/*
|----------------------------------------------------------------------------------------
| Loop prepared patterns.
*/
foreach
(
$informations
as
$information
)
{
/*
|------------------------------------------------------------------------------------
| detect start
*/
if
(
$information
[
'Type'
]
==
'start'
)
{
/*
|--------------------------------------------------------------------------------
| fill buffer
*/
$puffer
=
$information
[
'String'
];
}
/*
|------------------------------------------------------------------------------------
| dedect stop
*/
elseif
(
isset
(
$puffer
))
{
/*
|--------------------------------------------------------------------------------
| save result
*/
$result
[]
=
substr
(
$puffer
,
0
,
(
strlen
(
$information
[
'String'
])
*-
1
)
);
/*
|--------------------------------------------------------------------------------
| reset buffer
*/
unset
(
$puffer
);
}
}
/*
|----------------------------------------------------------------------------------------
| Return the results.
*/
return
$result
;
}
/*
|--------------------------------------------------------------------------------------------
| Method "normalize"
|--------------------------------------------------------------------------------------------
| Diese Method normalisiert einen String.
*/
function
normalize
(
$string
)
{
/*
|----------------------------------------------------------------------------------------
| Force the allman style.
*/
$string
=
preg_replace
(
'/{\s*$/m'
,
"
\n
{"
,
$string
);
/*
|----------------------------------------------------------------------------------------
| Remove empty lines and tabs.
*/
$string
=
preg_replace
(
'/^[\s\t]+[\n]*/m'
,
''
,
$string
);
/*
|----------------------------------------------------------------------------------------
| Remove double spaces.
*/
$string
=
preg_replace
(
"#[
\t
]+#"
,
" "
,
$string
);
/*
|----------------------------------------------------------------------------------------
| Return the filtered data.
*/
return
$string
;
}
/*
|--------------------------------------------------------------------------------------------
| Method "renderDocument"
|--------------------------------------------------------------------------------------------
| This method is used to render the template. By default, the output is written to a file.
| With the variable "$fake" the output can also be returned as a variable.
*/
function
renderDocument
(
$file
,
$fake
=
false
)
{
/*
|----------------------------------------------------------------------------------------
| Read the default template.
*/
$this
->
tpl
=
file_get_contents
(
__dir__
.
'/Template.tpl'
);
/*
|----------------------------------------------------------------------------------------
| Render the timestamp.
*/
$this
->
tpl
=
str_replace
(
'%timestasmp%'
,
date
(
'Y-m-d H:i:s'
),
$this
->
tpl
);
/*
|----------------------------------------------------------------------------------------
| Render the GIT user.
*/
$git
=
str_replace
(
"
\n
"
,
''
,
shell_exec
(
"git config user.name"
)
.
' <'
.
shell_exec
(
"git config user.email"
)
.
'>'
);
$this
->
tpl
=
str_replace
(
'%author%'
,
$git
,
$this
->
tpl
);
/*
|----------------------------------------------------------------------------------------
| Render the namespace.
*/
if
(
$this
->
namespace
!=
false
)
{
$this
->
tpl
=
str_replace
(
'%namespace%'
,
$this
->
S
()
.
$this
->
namespace
,
$this
->
tpl
);
}
else
{
$this
->
tpl
=
str_replace
(
"
\n
%namespace%"
,
''
,
$this
->
tpl
);
}
/*
|----------------------------------------------------------------------------------------
| Render all dependencies.
*/
if
(
$this
->
relations
!=
false
)
{
/*
|------------------------------------------------------------------------------------
| sort dependencies by length
*/
usort
(
$this
->
relations
,
function
(
$a
,
$b
)
{
return
strlen
(
$b
)
-
strlen
(
$a
);
});
/*
|------------------------------------------------------------------------------------
| implode dependencies
*/
$implode
=
implode
(
"
\n
"
.
$this
->
S
(),
$this
->
relations
);
/*
|------------------------------------------------------------------------------------
| write dependencies
*/
$this
->
tpl
=
str_replace
(
'%relations%'
,
$this
->
S
()
.
$implode
,
$this
->
tpl
);
}
/*
|----------------------------------------------------------------------------------------
| Remove dependencies placeholder if not used in template.
*/
else
{
$this
->
tpl
=
str_replace
(
"
\n
%relations%"
,
''
,
$this
->
tpl
);
}
/*
|----------------------------------------------------------------------------------------
| Render classes to template.
*/
$stream
=
$this
->
renderDocumentClasses
();
$this
->
tpl
=
str_replace
(
"%classes%"
,
$stream
,
$this
->
tpl
);
/*
|----------------------------------------------------------------------------------------
| Return document as variable.
*/
if
(
$fake
==
true
)
{
return
$this
->
tpl
;
}
/*
|----------------------------------------------------------------------------------------
| Write document to file.
*/
file_put_contents
(
$file
,
$this
->
tpl
);
}
/*
|--------------------------------------------------------------------------------------------
| Method "renderDocumentClasses"
|--------------------------------------------------------------------------------------------
| This method supports "renderDocument" when going through the individual classes.
*/
function
renderDocumentClasses
()
{
/*
|----------------------------------------------------------------------------------------
| create string for informations.
*/
$classstream
=
''
;
/*
|----------------------------------------------------------------------------------------
| Loop all classes.
*/
foreach
(
$this
->
classes
as
$class
)
{
/*
|------------------------------------------------------------------------------------
| cleanup opening tag
*/
$class
[
'OpeningTag'
]
=
preg_replace
(
"/[
\n\t\r
]+/"
,
" "
,
$class
[
'OpeningTag'
]);
$preg
=
preg_match
(
'/(?<=clas{2}.)[A-Za-z]*/'
,
$class
[
'OpeningTag'
],
$matches
,
PREG_OFFSET_CAPTURE
);
$classname
=
str_replace
(
" "
,
""
,
$matches
[
0
][
0
]);
/*
|------------------------------------------------------------------------------------
| add comment to class
*/
$classstream
.=
$this
->
WriteComment
(
$class
[
'Comment'
],
'Class "'
.
$classname
.
'"'
);
/*
|------------------------------------------------------------------------------------
| open class tag
*/
$classstream
.=
$this
->
S
()
.
$class
[
'OpeningTag'
]
.
"
\n
"
;
$classstream
.=
$this
->
S
()
.
'{'
.
"
\n
"
;
/*
|------------------------------------------------------------------------------------
| add optional class variables
*/
if
(
!
empty
(
$class
[
'variables'
]))
{
/*
|--------------------------------------------------------------------------------
| increase linedepth
*/
$this
->
linedeph
++
;
/*
|--------------------------------------------------------------------------------
| write title comment
*/
$classstream
.=
$this
->
WriteComment
(
''
,
'Variables'
);
/*
|--------------------------------------------------------------------------------
| setup variables
*/
$classstream
.=
$this
->
S
()
.
implode
(
"
\n
"
.
$this
->
S
(),
$class
[
'variables'
]);
$classstream
.=
"
\n\n
"
;
/*
|--------------------------------------------------------------------------------
| decrease linedepth
*/
$this
->
linedeph
--
;
}
/*
|------------------------------------------------------------------------------------
| The following method renders the indivual methods to the class.
*/
$this
->
linedeph
++
;
$classstream
.=
$this
->
renderDocumentMethods
(
$class
[
'Methods'
]);
$this
->
linedeph
--
;
/*
|------------------------------------------------------------------------------------
| Close the class tag.
*/
$classstream
.=
$this
->
S
()
.
'}'
.
"
\n\n\n
"
;
}
/*
|----------------------------------------------------------------------------------------
| Return the completed string.
*/
return
$classstream
;
}
/*
|--------------------------------------------------------------------------------------------
| Method "renderDocumentMethods"
|--------------------------------------------------------------------------------------------
| This method supports "renderDocument" when running through the individual methods. It is
| called by the "renderDocumentClasses" method.
*/
function
renderDocumentMethods
(
$methods
)
{
/*
|----------------------------------------------------------------------------------------
| create string for informations.
*/
$methodstream
=
''
;
/*
|----------------------------------------------------------------------------------------
| Loop every method.
*/
foreach
(
$methods
as
$method
)
{
/*
|------------------------------------------------------------------------------------
| Add individual comment to the method.
*/
$methodstream
.=
"
\n
"
;
$methodstream
.=
$this
->
WriteComment
(
$method
[
'Comment'
],
'Method "'
.
$method
[
'Title'
]
.
'"'
);
/*
|------------------------------------------------------------------------------------
| This variable is used to check the indentation depth on the internal method level.
*/
$internaldeph
=
0
;
/*
|------------------------------------------------------------------------------------
| This variable is used to note whether the last element in the subsequent run was a
| comment.
*/
$lastWasComment
=
false
;
/*
|------------------------------------------------------------------------------------
| Each line of the method is separated by explode and checked individually.
*/
foreach
(
explode
(
"
\n
"
,
$method
[
'Body'
])
as
$body
)
{
/*
|--------------------------------------------------------------------------------
| If the "Close" tag is recognized, the indentation depth is reduced. If it is
| the last level (level 0), an additional space is added.
*/
if
(
substr
(
$body
,
0
,
1
)
==
'}'
)
{
$this
->
linedeph
--
;
$internaldeph
--
;
if
(
$internaldeph
!=
0
)
{
$methodstream
.=
$this
->
S
()
.
$body
.
"
\n
"
;
}
else
{
$methodstream
.=
"
\n
"
.
$this
->
S
()
.
$body
.
"
\n
"
;
}
}
/*
|--------------------------------------------------------------------------------
| The folloiwing part does add comments.
*/
elseif
(
substr
(
$body
,
0
,
8
)
==
'#COMMENT'
)
{
$comment
=
$this
->
getCommentFromPregMatch
(
'/#.+-.+-.+#/'
,
$body
);
if
(
$lastWasComment
==
true
)
{
$methodstream
.=
$this
->
WriteComment
(
$comment
);
}
else
{
$methodstream
.=
"
\n
"
.
$this
->
WriteComment
(
$comment
);
}
$lastWasComment
=
true
;
}
/*
|--------------------------------------------------------------------------------
| Another line break is added for if, foreach, while and return if the last
| element was not a comment.
*/
elseif
(
(
substr
(
$body
,
0
,
2
)
==
'if'
or
substr
(
$body
,
0
,
7
)
==
'foreach'
or
substr
(
$body
,
0
,
5
)
==
'while'
or
substr
(
$body
,
0
,
6
)
==
'return'
)
and
$lastWasComment
==
false
)
{
$methodstream
.=
"
\n
"
.
$this
->
S
()
.
$body
.
"
\n
"
;
}
/*
|--------------------------------------------------------------------------------
| In the following, normal text is written that was not previously recognized by
| special treatment.
*/
else
{
$methodstream
.=
$this
->
S
()
.
$body
.
"
\n
"
;
$lastWasComment
=
false
;
}
/*
|--------------------------------------------------------------------------------
| If the "Open" tag is recognized, the indentation level is increased.
| "$lastWasComment" is used here to avoid the line break before the first
| comment.
*/
if
(
substr
(
$body
,
0
,
1
)
==
'{'
)
{
$lastWasComment
=
true
;
$this
->
linedeph
++
;
$internaldeph
++
;
}
}
/*
|------------------------------------------------------------------------------------
| Add another linebreak at the end of the method.
*/
$methodstream
.=
"
\n
"
;
}
/*
|----------------------------------------------------------------------------------------
| Return the completed method stream.
*/
return
$methodstream
;
}
/*
|--------------------------------------------------------------------------------------------
| Method "S"
|--------------------------------------------------------------------------------------------
| This method supports the script at the current indentation level. It returns a string with
| the appropriate spaces.
*/
function
S
()
{
$c
=
0
;
$x
=
''
;
while
(
$c
<
$this
->
linedeph
*
4
)
{
$c
++
;
$x
.=
' '
;
}
return
$x
;
}
/*
|--------------------------------------------------------------------------------------------
| Method "scanClass"
|--------------------------------------------------------------------------------------------
| This method is responsible for scanning the class. With the help of the
| "matchBetweenTwoPatterns" method, the respective area between start and stop is extracted.
*/
function
scanClass
()
{
/*
|----------------------------------------------------------------------------------------
| start Pattern
*/
$OpenPattern
=
'/[^;{}$*"]+class[^a-z\\\[(=-][^{;()[\]]+/s'
;
/*
|----------------------------------------------------------------------------------------
| stop Pattern
*/
$ClosePattern
=
'/([^;{$}>*$()"]+\s*class[^a-z\\[(=-])|[\s\t\n]+$/'
;
/*
|----------------------------------------------------------------------------------------
| extract inbetween
*/
$classes
=
$this
->
matchBetweenTwoPatterns
(
$this
->
content
,
$OpenPattern
,
$ClosePattern
);
/*
|----------------------------------------------------------------------------------------
| loop detected clases.
*/
foreach
(
$classes
as
$Class
)
{
/*
|------------------------------------------------------------------------------------
| Normalize text of the class.
*/
$Class
=
$this
->
normalize
(
$Class
);
/*
|------------------------------------------------------------------------------------
| A check is then carried out to determine whether it is actually a class. If not,
| the element will not be processed any further.
*/
$preg
=
preg_match
(
'/class[^{;()[\]]+/'
,
$Class
,
$matches
,
PREG_OFFSET_CAPTURE
);
if
(
!
isset
(
$matches
[
0
]))
{
continue
;
}
/*
|------------------------------------------------------------------------------------
| The description of the class is extracted and noted.
*/
$description
=
$this
->
getCommentFromPregMatch
(
'/#.+-.+-.+#(?=(\n\s*class))/'
,
$Class
);
/*
|------------------------------------------------------------------------------------
| A check is then carried out to determine whether class variables are available.
| These will be noted later or set to "false".
*/
$preg
=
preg_match_all
(
'/(public|protected|private|var)\s*\$.*\;/'
,
$Class
,
$variables
,
PREG_OFFSET_CAPTURE
);
if
(
isset
(
$variables
[
0
]))
{
$classvariables
=
array
();
foreach
(
$variables
[
0
]
as
$relation
)
{
$classvariables
[
md5
(
$relation
[
0
])]
=
$relation
[
0
];
}
}
else
{
$classvariables
=
false
;
}
/*
|------------------------------------------------------------------------------------
| Save informations to the class.
*/
$this
->
classes
[]
=
[
'OpeningTag'
=>
$matches
[
0
][
0
],
'Body'
=>
$Class
,
'Comment'
=>
$description
,
'Methods'
=>
[]
,
'variables'
=>
$classvariables
];
}
}
/*
|--------------------------------------------------------------------------------------------
| Method "scanComments"
|--------------------------------------------------------------------------------------------
| This method is responsible for extracting comments from the original file and transferring
| them to a simpler, temporary format during the processing process. The original content of
| the comment is buffered in the class.
*/
function
scanComments
()
{
$content
=
$this
->
content
;
/*
|----------------------------------------------------------------------------------------
| Filter
*/
preg_match_all
(
'/^[\s]*(\/\/)[^\n]+|^[\s]*#.+[^\n]|(^|\s)+\/\*(.|\n)+?(?<=(\*\/))/m'
,
$content
,
$Comments
,
PREG_OFFSET_CAPTURE
);
/*
|----------------------------------------------------------------------------------------
| Loop all comments.
*/
foreach
(
$Comments
[
0
]
as
$Com
)
{
/*
|------------------------------------------------------------------------------------
| The comment is hashed for better control.
*/
$md
=
md5
(
$Com
[
0
]);
/*
|------------------------------------------------------------------------------------
| Save comment to the buffer.
*/
$this
->
comments
[
$md
]
=
$Com
[
0
];
}
/*
|----------------------------------------------------------------------------------------
| The comment is then sorted by character length. This prevents a bug which incorrectly
| replaces smaller, identical strings.
*/
usort
(
$this
->
comments
,
function
(
$a
,
$b
)
{
return
strlen
(
$b
)
-
strlen
(
$a
);
});
/*
|----------------------------------------------------------------------------------------
| The comments are removed from the document.
*/
foreach
(
$this
->
comments
as
$key
=>
$value
)
{
$content
=
str_replace
(
$value
,
"
\n
#COMMENT-"
.
$key
.
"-COMMENT#
\n
"
,
$content
);
}
/*
|----------------------------------------------------------------------------------------
| The cleaned content is returned.
*/
$this
->
content
=
$content
;
return
$content
;
}
/*
|--------------------------------------------------------------------------------------------
| Method "scanMethods"
|--------------------------------------------------------------------------------------------
| This method scans all methods of each class and notes them.
*/
function
scanMethods
()
{
/*
|----------------------------------------------------------------------------------------
| Loop all classes.
*/
foreach
(
$this
->
classes
as
$Key
=>
$Class
)
{
/*
|------------------------------------------------------------------------------------
| Create an array for methods.
*/
$Methods
=
array
();
/*
|------------------------------------------------------------------------------------
| In the following, remnants of the class are removed from the string in order to be
| able to search for methods only.
*/
$Classstart
=
strpos
(
$Class
[
'Body'
]
,
'{'
)
+
1
;
$Classstop
=
strrpos
(
$Class
[
'Body'
]
,
'}'
);
$ClassCount
=
strlen
(
$Class
[
'Body'
]);
$Prepared
=
substr
(
$Class
[
'Body'
],
$Classstart
,
(
$ClassCount
-
$Classstart
)
-
(
$ClassCount
-
$Classstop
))
;
/*
|------------------------------------------------------------------------------------
| Similar to extracting the classes, a start and stopPattern is defined for the
| methods. The methods are extracted between these regular expressions.
*/
$OpenPattern
=
'/([^;{}]+function[^a-z\\\[(][^{;()[\]]+)/'
;
$ClosePattern
=
'/([^;{$}>\'"*]+\s*function[^a-z\\[(])|[\s\t\n]+$/'
;
$results
=
$this
->
matchBetweenTwoPatterns
(
$Prepared
,
$OpenPattern
,
$ClosePattern
);
/*
/*
|------------------------------------------------------------------------------------
|------------------------------------------------------------------------------------
| Loop all methods.
| Drop error for a wrong file path.
*/
foreach
(
$results
as
$result
)
{
preg_match
(
'/(private|public|protected|static)*\s*function\s*\w+.*(?<=\))/'
,
$result
,
$opener
,
PREG_OFFSET_CAPTURE
);
if
(
!
isset
(
$opener
[
0
]))
{
continue
;
}
/*
|--------------------------------------------------------------------------------
| With the help of the following nesting, the title of the method is extracted.
*/
$OpenerArray
=
str_replace
(
' '
,
''
,
explode
(
'('
,
str_replace
(
')'
,
''
,
$opener
[
0
][
0
])));
$Title
=
substr
(
$OpenerArray
[
0
],
(
strrpos
(
$OpenerArray
[
0
],
'function'
)
+
8
)
);
$description
=
$this
->
getCommentFromPregMatch
(
'/#.+-.+-.+#(?=(\n(public|protected|private|static|\s)*function))/'
,
$result
);
/*
|--------------------------------------------------------------------------------
| This command removes the comment before the function (duplicate protection).
*/
$result
=
preg_replace
(
'/(#COMMENT.+\s*(?=(\n.*function[^a-z\\[(])))/'
,
''
,
$result
);
/*
|--------------------------------------------------------------------------------
| After multiple changes, the string is normalized.
*/
$result
=
$this
->
normalize
(
$result
);
/*
|--------------------------------------------------------------------------------
| From the parameter array, only all those after the dollar are allowed to be
| used.
*/
$afterdollar
=
[];
foreach
(
explode
(
','
,
$OpenerArray
[
1
])
as
$dollar
)
{
$afterdollar
[]
=
preg_replace
(
'/.+(?=(\$))/'
,
''
,
$dollar
);
}
/*
|--------------------------------------------------------------------------------
| Save the method.
*/
*/
$Methods
[]
=
[
$file
=
$this
->
error
(
'Please enter a valid file path!'
);
'Title'
=>
$Title
,
'Parameter'
=>
$afterdollar
,
'Comment'
=>
$description
,
'Body'
=>
$result
];
}
/*
/*
|------------------------------------------------------------------------------------
|------------------------------------------------------------------------------------
| Save all methods from the class.
| Stop the command...
*/
$this
->
classes
[
$Key
][
'Methods'
]
=
$Methods
;
}
}
/*
|--------------------------------------------------------------------------------------------
| Method "WriteComment"
|--------------------------------------------------------------------------------------------
| This method helps to create a correctly formatted comment. With the variable "$fake" only
| the text of the comment is returned.
*/
function
WriteComment
(
$comment
,
$title
=
null
,
$fake
=
false
)
{
/*
|----------------------------------------------------------------------------------------
| The following variable starts the string for a new comment.
*/
$k
=
''
;
/*
|----------------------------------------------------------------------------------------
| Subsequently the transmitted comment is cleaned up and normalized.
*/
$comment
=
preg_replace
(
'/[^a-zA-Z:,.0-9_=$+!? äÄüÜÖöß"\'()]/'
,
''
,
$comment
);
$comment
=
$this
->
normalize
(
$comment
);
/*
|----------------------------------------------------------------------------------------
| The beautifier uses certain keywords for e.g. classes and methods. These are filtered
| here in order not to create duplicates in multiple render document processes.
*/
if
(
strpos
(
$comment
,
$title
)
===
0
)
{
$comment
=
substr
(
$comment
,
strlen
(
$title
));
$comment
=
$this
->
normalize
(
$comment
);
}
/*
|----------------------------------------------------------------------------------------
| The comment is returned in fake mode without formatting.
*/
*/
if
(
$fake
==
true
)
return
false
;
{
return
$comment
;
}
}
/*
/*
|----------------------------------------------------------------------------------------
|----------------------------------------------------------------------------------------
| The comment is split over several lines (if there are several lines).
| Copy a backup to the vendor folder.
*/
$comment
=
wordwrap
(
$comment
,
98
-
(
strlen
(
$this
->
S
())));
$comment
=
explode
(
"
\n
"
,
$comment
);
/*
|----------------------------------------------------------------------------------------
| At the moment, only large comments are supportet.
*/
*/
$large
=
true
;
copy
(
$file
,
__DIR__
.
'/BACKUP_'
.
time
()
)
;
/*
/*
|----------------------------------------------------------------------------------------
|----------------------------------------------------------------------------------------
|
Large comment..
.
|
Boot the Beautifier class
.
*/
*/
if
(
$large
==
true
)
$beauty
=
new
BeautifyManager
();
{
$k
=
$this
->
S
()
.
'/*'
.
"
\n
"
;
if
(
$title
!=
null
)
{
$k
.=
$this
->
S
()
.
'|'
.
str_repeat
(
'-'
,
100
-
(
strlen
(
$this
->
S
()))
)
.
"
\n
"
;
$k
.=
$this
->
S
()
.
'| '
.
$title
.
"
\n
"
;
}
$k
.=
$this
->
S
()
.
'|'
.
str_repeat
(
'-'
,
100
-
(
strlen
(
$this
->
S
()))
)
.
"
\n
"
;
foreach
(
$comment
as
$com
)
{
if
(
!
empty
(
$com
))
{
$k
.=
$this
->
S
()
.
'| '
.
$com
.
"
\n
"
;
}
}
$k
.=
$this
->
S
()
.
'*/'
.
"
\n
"
;
}
/*
/*
|----------------------------------------------------------------------------------------
|----------------------------------------------------------------------------------------
|
Small comment..
.
|
Analyse the given file
.
*/
*/
else
$beauty
->
analyseDocument
(
$file
);
{
$k
.=
$this
->
S
()
.
'# '
.
$comment
[
0
]
.
"
\n
"
;
}
/*
/*
|----------------------------------------------------------------------------------------
|----------------------------------------------------------------------------------------
| Re
turn the comment
.
| Re
nder the given file
.
*/
*/
return
$k
;
$beauty
->
renderDocument
(
$file
)
;
}
}
...
...
src/Managers/BeautifyManager/BeautifyManager.php
0 → 100644
View file @
fe26e9f0
<?php
/*
|------------------------------------------------------------------------------------------------
| Information
|------------------------------------------------------------------------------------------------
|
| This file is beautified by the command "sidekick:CodebeautifierCommand" of the ceetrox
| sidekick package.
|
| Author: Marco Schmiedel <marco.schmiedel@itmax.email>
| Date: 2021-12-23 19:59:15
|
*/
namespace
Ceetrox\Managers\BeautifyManager
;
/*
|------------------------------------------------------------------------------------------------
| Dependencies
|------------------------------------------------------------------------------------------------
*/
/*
|------------------------------------------------------------------------------------------------
| Class "BeautifyManager"
|------------------------------------------------------------------------------------------------
*/
class
BeautifyManager
{
/*
|--------------------------------------------------------------------------------------------
| Variables
|--------------------------------------------------------------------------------------------
*/
var
$content
=
[];
var
$classes
=
[];
var
$comments
=
[];
var
$linedeph
=
1
;
/*
|--------------------------------------------------------------------------------------------
| Method "analyseDocument"
|--------------------------------------------------------------------------------------------
| This method do scan the different aspects of the source php file.
*/
function
analyseDocument
(
$file
)
{
/*
|----------------------------------------------------------------------------------------
| Read the php file.
*/
$this
->
content
=
file_get_contents
(
$file
);
/*
|----------------------------------------------------------------------------------------
| Read all base parameters.
*/
$this
->
scanBasics
();
/*
|----------------------------------------------------------------------------------------
| Read all comments.
*/
$this
->
scanComments
();
/*
|----------------------------------------------------------------------------------------
| Read all classes.
*/
$this
->
scanClass
();
/*
|----------------------------------------------------------------------------------------
| Read all methods.
*/
$this
->
scanMethods
();
/*
|----------------------------------------------------------------------------------------
| Return the full analyse.
*/
return
$this
->
classes
;
}
/*
|--------------------------------------------------------------------------------------------
| Method "scanBasics"
|--------------------------------------------------------------------------------------------
| This method is used to read some basic parameters from the file (for example namespace and
| dependencies).
*/
function
scanBasics
()
{
/*
|----------------------------------------------------------------------------------------
| read namespace
*/
$preg
=
preg_match
(
'/(^|\s)+namespace\s+[A-Za-z\\\]+;/'
,
$this
->
content
,
$matches
,
PREG_OFFSET_CAPTURE
);
if
(
isset
(
$matches
[
0
]))
{
$this
->
namespace
=
trim
(
str_replace
(
"
\n
"
,
''
,
$matches
[
0
][
0
]));
}
else
{
$this
->
namespace
=
false
;
}
/*
|----------------------------------------------------------------------------------------
| read dependencies
*/
$preg
=
preg_match_all
(
'/(^|\s)+use\s+[^"\'\n]+[;]/'
,
$this
->
content
,
$matches
,
PREG_OFFSET_CAPTURE
);
if
(
isset
(
$matches
[
0
]))
{
$this
->
relations
=
array
();
foreach
(
$matches
[
0
]
as
$relation
)
{
$this
->
relations
[
md5
(
$relation
[
0
])]
=
trim
(
str_replace
(
"
\n
"
,
''
,
$relation
[
0
]));
}
}
else
{
$this
->
relations
=
false
;
}
}
/*
|--------------------------------------------------------------------------------------------
| Method "scanComments"
|--------------------------------------------------------------------------------------------
| This method is responsible for extracting comments from the original file and transferring
| them to a simpler, temporary format during the processing process. The original content of
| the comment is buffered in the class.
*/
function
scanComments
()
{
$content
=
$this
->
content
;
/*
|----------------------------------------------------------------------------------------
| Filter
*/
preg_match_all
(
'/^[\s]*(\/\/)[^\n]+|^[\s]*#.+[^\n]|(^|\s)+\/\*(.|\n)+?(?<=(\*\/))/m'
,
$content
,
$Comments
,
PREG_OFFSET_CAPTURE
);
/*
|----------------------------------------------------------------------------------------
| Loop all comments.
*/
foreach
(
$Comments
[
0
]
as
$Com
)
{
/*
|------------------------------------------------------------------------------------
| The comment is hashed for better control.
*/
$md
=
md5
(
$Com
[
0
]);
/*
|------------------------------------------------------------------------------------
| Save comment to the buffer.
*/
$this
->
comments
[
$md
]
=
$Com
[
0
];
}
/*
|----------------------------------------------------------------------------------------
| The comment is then sorted by character length. This prevents a bug which incorrectly
| replaces smaller, identical strings.
*/
usort
(
$this
->
comments
,
function
(
$a
,
$b
)
{
return
strlen
(
$b
)
-
strlen
(
$a
);
});
/*
|----------------------------------------------------------------------------------------
| The comments are removed from the document.
*/
foreach
(
$this
->
comments
as
$key
=>
$value
)
{
$content
=
str_replace
(
$value
,
"
\n
#COMMENT-"
.
$key
.
"-COMMENT#
\n
"
,
$content
);
}
/*
|----------------------------------------------------------------------------------------
| The cleaned content is returned.
*/
$this
->
content
=
$content
;
return
$content
;
}
/*
|--------------------------------------------------------------------------------------------
| Method "scanClass"
|--------------------------------------------------------------------------------------------
| This method is responsible for scanning the class. With the help of the
| "matchBetweenTwoPatterns" method, the respective area between start and stop is extracted.
*/
function
scanClass
()
{
/*
|----------------------------------------------------------------------------------------
| start Pattern
*/
$OpenPattern
=
'/[^;{}$*"]+class[^a-z\\\[(=-][^{;()[\]]+/s'
;
/*
|----------------------------------------------------------------------------------------
| stop Pattern
*/
$ClosePattern
=
'/([^;{$}>*$()"]+\s*class[^a-z\\[(=-])|[\s\t\n]+$/'
;
/*
|----------------------------------------------------------------------------------------
| extract inbetween
*/
$classes
=
$this
->
matchBetweenTwoPatterns
(
$this
->
content
,
$OpenPattern
,
$ClosePattern
);
/*
|----------------------------------------------------------------------------------------
| loop detected clases.
*/
foreach
(
$classes
as
$Class
)
{
/*
|------------------------------------------------------------------------------------
| Normalize text of the class.
*/
$Class
=
$this
->
normalize
(
$Class
);
/*
|------------------------------------------------------------------------------------
| A check is then carried out to determine whether it is actually a class. If not,
| the element will not be processed any further.
*/
$preg
=
preg_match
(
'/class[^{;()[\]]+/'
,
$Class
,
$matches
,
PREG_OFFSET_CAPTURE
);
if
(
!
isset
(
$matches
[
0
]))
{
continue
;
}
/*
|------------------------------------------------------------------------------------
| The description of the class is extracted and noted.
*/
$description
=
$this
->
getCommentFromPregMatch
(
'/#.+-.+-.+#(?=(\n\s*class))/'
,
$Class
);
/*
|------------------------------------------------------------------------------------
| A check is then carried out to determine whether class variables are available.
| These will be noted later or set to "false".
*/
$preg
=
preg_match_all
(
'/(public|protected|private|var)\s*\$.*\;/'
,
$Class
,
$variables
,
PREG_OFFSET_CAPTURE
);
if
(
isset
(
$variables
[
0
]))
{
$classvariables
=
array
();
foreach
(
$variables
[
0
]
as
$relation
)
{
$classvariables
[
md5
(
$relation
[
0
])]
=
$relation
[
0
];
}
}
else
{
$classvariables
=
false
;
}
/*
|------------------------------------------------------------------------------------
| Save informations to the class.
*/
$this
->
classes
[]
=
[
'OpeningTag'
=>
$matches
[
0
][
0
],
'Body'
=>
$Class
,
'Comment'
=>
$description
,
'Methods'
=>
[]
,
'variables'
=>
$classvariables
];
}
}
/*
|--------------------------------------------------------------------------------------------
| Method "scanMethods"
|--------------------------------------------------------------------------------------------
| This method scans all methods of each class and notes them.
*/
function
scanMethods
()
{
/*
|----------------------------------------------------------------------------------------
| Loop all classes.
*/
foreach
(
$this
->
classes
as
$Key
=>
$Class
)
{
/*
|------------------------------------------------------------------------------------
| Create an array for methods.
*/
$Methods
=
array
();
/*
|------------------------------------------------------------------------------------
| In the following, remnants of the class are removed from the string in order to be
| able to search for methods only.
*/
$Classstart
=
strpos
(
$Class
[
'Body'
]
,
'{'
)
+
1
;
$Classstop
=
strrpos
(
$Class
[
'Body'
]
,
'}'
);
$ClassCount
=
strlen
(
$Class
[
'Body'
]);
$Prepared
=
substr
(
$Class
[
'Body'
],
$Classstart
,
(
$ClassCount
-
$Classstart
)
-
(
$ClassCount
-
$Classstop
))
;
/*
|------------------------------------------------------------------------------------
| Similar to extracting the classes, a start and stopPattern is defined for the
| methods. The methods are extracted between these regular expressions.
*/
$OpenPattern
=
'/([^;{}]+function[^a-z\\\[(][^{;()[\]]+)/'
;
$ClosePattern
=
'/([^;{$}>\'"*]+\s*function[^a-z\\[(])|[\s\t\n]+$/'
;
$results
=
$this
->
matchBetweenTwoPatterns
(
$Prepared
,
$OpenPattern
,
$ClosePattern
);
/*
|------------------------------------------------------------------------------------
| Loop all methods.
*/
if
(
$results
!=
false
)
{
foreach
(
$results
as
$result
)
{
preg_match
(
'/(private|public|protected|static)*\s*function\s*\w+.*(?<=\))/'
,
$result
,
$opener
,
PREG_OFFSET_CAPTURE
);
if
(
!
isset
(
$opener
[
0
]))
{
continue
;
}
/*
|----------------------------------------------------------------------------
| With the help of the following nesting, the title of the method is
| extracted.
*/
$OpenerArray
=
str_replace
(
' '
,
''
,
explode
(
'('
,
str_replace
(
')'
,
''
,
$opener
[
0
][
0
])));
$Title
=
substr
(
$OpenerArray
[
0
],
(
strrpos
(
$OpenerArray
[
0
],
'function'
)
+
8
)
);
$description
=
$this
->
getCommentFromPregMatch
(
'/#.+-.+-.+#(?=(\n(public|protected|private|static|\s)*function))/'
,
$result
);
/*
|----------------------------------------------------------------------------
| This command removes the comment before the function (duplicate
| protection).
*/
$result
=
preg_replace
(
'/(#COMMENT.+\s*(?=(\n.*function[^a-z\\[(])))/'
,
''
,
$result
);
/*
|----------------------------------------------------------------------------
| After multiple changes, the string is normalized.
*/
$result
=
$this
->
normalize
(
$result
);
/*
|----------------------------------------------------------------------------
| From the parameter array, only all those after the dollar are allowed to
| be used.
*/
$afterdollar
=
[];
foreach
(
explode
(
','
,
$OpenerArray
[
1
])
as
$dollar
)
{
$afterdollar
[]
=
preg_replace
(
'/.+(?=(\$))/'
,
''
,
$dollar
);
}
/*
|----------------------------------------------------------------------------
| Save the method.
*/
$Methods
[]
=
[
'Title'
=>
$Title
,
'Parameter'
=>
$afterdollar
,
'Comment'
=>
$description
,
'Body'
=>
$result
];
}
}
/*
|------------------------------------------------------------------------------------
| Save all methods from the class.
*/
$this
->
classes
[
$Key
][
'Methods'
]
=
$Methods
;
}
}
/*
|--------------------------------------------------------------------------------------------
| Method "renderDocument"
|--------------------------------------------------------------------------------------------
| This method is used to render the template. By default, the output is written to a file.
| With the variable "$fake" the output can also be returned as a variable.
*/
function
renderDocument
(
$file
,
$fake
=
false
)
{
/*
|----------------------------------------------------------------------------------------
| Read the default template.
*/
$this
->
tpl
=
file_get_contents
(
__dir__
.
'/Template.tpl'
);
/*
|----------------------------------------------------------------------------------------
| Render the timestamp.
*/
$this
->
tpl
=
str_replace
(
'%timestasmp%'
,
date
(
'Y-m-d H:i:s'
),
$this
->
tpl
);
/*
|----------------------------------------------------------------------------------------
| Render the GIT user.
*/
$git
=
str_replace
(
"
\n
"
,
''
,
shell_exec
(
"git config user.name"
)
.
' <'
.
shell_exec
(
"git config user.email"
)
.
'>'
);
$this
->
tpl
=
str_replace
(
'%author%'
,
$git
,
$this
->
tpl
);
/*
|----------------------------------------------------------------------------------------
| Render the namespace.
*/
if
(
$this
->
namespace
!=
false
)
{
$this
->
tpl
=
str_replace
(
'%namespace%'
,
$this
->
s
()
.
$this
->
namespace
,
$this
->
tpl
);
}
else
{
$this
->
tpl
=
str_replace
(
"
\n
%namespace%"
,
''
,
$this
->
tpl
);
}
/*
|----------------------------------------------------------------------------------------
| Render all dependencies.
*/
if
(
$this
->
relations
!=
false
)
{
/*
|------------------------------------------------------------------------------------
| sort dependencies by length
*/
usort
(
$this
->
relations
,
function
(
$a
,
$b
)
{
return
strlen
(
$b
)
-
strlen
(
$a
);
});
/*
|------------------------------------------------------------------------------------
| implode dependencies
*/
$implode
=
implode
(
"
\n
"
.
$this
->
s
(),
$this
->
relations
);
/*
|------------------------------------------------------------------------------------
| write dependencies
*/
$this
->
tpl
=
str_replace
(
'%relations%'
,
$this
->
s
()
.
$implode
,
$this
->
tpl
);
}
/*
|----------------------------------------------------------------------------------------
| Remove dependencies placeholder if not used in template.
*/
else
{
$this
->
tpl
=
str_replace
(
"
\n
%relations%"
,
''
,
$this
->
tpl
);
}
/*
|----------------------------------------------------------------------------------------
| Render classes to template.
*/
$stream
=
$this
->
renderDocumentClasses
();
$this
->
tpl
=
str_replace
(
"%classes%"
,
$stream
,
$this
->
tpl
);
/*
|----------------------------------------------------------------------------------------
| Return document as variable.
*/
if
(
$fake
==
true
)
{
return
$this
->
tpl
;
}
/*
|----------------------------------------------------------------------------------------
| Write document to file.
*/
file_put_contents
(
$file
,
$this
->
tpl
);
}
/*
|--------------------------------------------------------------------------------------------
| Method "renderDocumentClasses"
|--------------------------------------------------------------------------------------------
| This method supports "renderDocument" when going through the individual classes.
*/
function
renderDocumentClasses
()
{
/*
|----------------------------------------------------------------------------------------
| create string for informations.
*/
$classstream
=
''
;
/*
|----------------------------------------------------------------------------------------
| Loop all classes.
*/
foreach
(
$this
->
classes
as
$class
)
{
/*
|------------------------------------------------------------------------------------
| cleanup opening tag
*/
$class
[
'OpeningTag'
]
=
preg_replace
(
"/[
\n\t\r
]+/"
,
" "
,
$class
[
'OpeningTag'
]);
$preg
=
preg_match
(
'/(?<=clas{2}.)[A-Za-z]*/'
,
$class
[
'OpeningTag'
],
$matches
,
PREG_OFFSET_CAPTURE
);
$classname
=
str_replace
(
" "
,
""
,
$matches
[
0
][
0
]);
/*
|------------------------------------------------------------------------------------
| add comment to class
*/
$classstream
.=
$this
->
writeComment
(
$class
[
'Comment'
],
'Class "'
.
$classname
.
'"'
);
/*
|------------------------------------------------------------------------------------
| open class tag
*/
$classstream
.=
$this
->
s
()
.
$class
[
'OpeningTag'
]
.
"
\n
"
;
$classstream
.=
$this
->
s
()
.
'{'
.
"
\n
"
;
/*
|------------------------------------------------------------------------------------
| add optional class variables
*/
if
(
!
empty
(
$class
[
'variables'
]))
{
/*
|--------------------------------------------------------------------------------
| increase linedepth
*/
$this
->
linedeph
++
;
/*
|--------------------------------------------------------------------------------
| write title comment
*/
$classstream
.=
$this
->
writeComment
(
''
,
'Variables'
);
/*
|--------------------------------------------------------------------------------
| setup variables
*/
$classstream
.=
$this
->
s
()
.
implode
(
"
\n
"
.
$this
->
s
(),
$class
[
'variables'
]);
$classstream
.=
"
\n\n
"
;
/*
|--------------------------------------------------------------------------------
| decrease linedepth
*/
$this
->
linedeph
--
;
}
/*
|------------------------------------------------------------------------------------
| The following method renders the indivual methods to the class.
*/
$this
->
linedeph
++
;
$classstream
.=
$this
->
renderDocumentMethods
(
$class
[
'Methods'
]);
$this
->
linedeph
--
;
/*
|------------------------------------------------------------------------------------
| Close the class tag.
*/
$classstream
.=
$this
->
s
()
.
'}'
.
"
\n\n\n
"
;
}
/*
|----------------------------------------------------------------------------------------
| Return the completed string.
*/
return
$classstream
;
}
/*
|--------------------------------------------------------------------------------------------
| Method "renderDocumentMethods"
|--------------------------------------------------------------------------------------------
| This method supports "renderDocument" when running through the individual methods. It is
| called by the "renderDocumentClasses" method.
*/
function
renderDocumentMethods
(
$methods
)
{
/*
|----------------------------------------------------------------------------------------
| create string for informations.
*/
$methodstream
=
''
;
/*
|----------------------------------------------------------------------------------------
| Loop every method.
*/
foreach
(
$methods
as
$method
)
{
/*
|------------------------------------------------------------------------------------
| Add individual comment to the method.
*/
$methodstream
.=
"
\n
"
;
$methodstream
.=
$this
->
writeComment
(
$method
[
'Comment'
],
'Method "'
.
$method
[
'Title'
]
.
'"'
);
/*
|------------------------------------------------------------------------------------
| This variable is used to check the indentation depth on the internal method level.
*/
$internaldeph
=
0
;
/*
|------------------------------------------------------------------------------------
| This variable is used to note whether the last element in the subsequent run was a
| comment.
*/
$lastWasComment
=
false
;
/*
|------------------------------------------------------------------------------------
| Each line of the method is separated by explode and checked individually.
*/
foreach
(
explode
(
"
\n
"
,
$method
[
'Body'
])
as
$body
)
{
/*
|--------------------------------------------------------------------------------
| If the "Close" tag is recognized, the indentation depth is reduced. If it is
| the last level (level 0), an additional space is added.
*/
if
(
substr
(
$body
,
0
,
1
)
==
'}'
)
{
$this
->
linedeph
--
;
$internaldeph
--
;
if
(
$internaldeph
!=
0
)
{
$methodstream
.=
$this
->
s
()
.
$body
.
"
\n
"
;
}
else
{
$methodstream
.=
"
\n
"
.
$this
->
s
()
.
$body
.
"
\n
"
;
}
}
/*
|--------------------------------------------------------------------------------
| The folloiwing part does add comments.
*/
elseif
(
substr
(
$body
,
0
,
8
)
==
'#COMMENT'
)
{
$comment
=
$this
->
getCommentFromPregMatch
(
'/#.+-.+-.+#/'
,
$body
);
if
(
$lastWasComment
==
true
)
{
$methodstream
.=
$this
->
writeComment
(
$comment
);
}
else
{
$methodstream
.=
"
\n
"
.
$this
->
writeComment
(
$comment
);
}
$lastWasComment
=
true
;
}
/*
|--------------------------------------------------------------------------------
| Another line break is added for if, foreach, while and return if the last
| element was not a comment.
*/
elseif
(
(
substr
(
$body
,
0
,
2
)
==
'if'
or
substr
(
$body
,
0
,
7
)
==
'foreach'
or
substr
(
$body
,
0
,
5
)
==
'while'
or
substr
(
$body
,
0
,
6
)
==
'return'
)
and
$lastWasComment
==
false
)
{
$methodstream
.=
"
\n
"
.
$this
->
s
()
.
$body
.
"
\n
"
;
}
/*
|--------------------------------------------------------------------------------
| In the following, normal text is written that was not previously recognized by
| special treatment.
*/
else
{
$methodstream
.=
$this
->
s
()
.
$body
.
"
\n
"
;
$lastWasComment
=
false
;
}
/*
|--------------------------------------------------------------------------------
| If the "Open" tag is recognized, the indentation level is increased.
| "$lastWasComment" is used here to avoid the line break before the first
| comment.
*/
if
(
substr
(
$body
,
0
,
1
)
==
'{'
)
{
$lastWasComment
=
true
;
$this
->
linedeph
++
;
$internaldeph
++
;
}
}
/*
|------------------------------------------------------------------------------------
| Add another linebreak at the end of the method.
*/
$methodstream
.=
"
\n
"
;
}
/*
|----------------------------------------------------------------------------------------
| Return the completed method stream.
*/
return
$methodstream
;
}
/*
|--------------------------------------------------------------------------------------------
| Method "getCommentFromPregMatch"
|--------------------------------------------------------------------------------------------
| With this method a string that has been hashed by "scanClass" can be converted back.
*/
private
function
getCommentFromPregMatch
(
$pattern
,
$string
)
{
/*
|----------------------------------------------------------------------------------------
| check pattern
*/
preg_match
(
$pattern
,
$string
,
$description
,
PREG_OFFSET_CAPTURE
);
$result
=
false
;
/*
|----------------------------------------------------------------------------------------
| load description from cache
*/
if
(
isset
(
$description
[
0
][
0
]))
{
preg_match
(
'/(?<=(#COMMENT-)).+(?=(-COMMENT#))/'
,
$description
[
0
][
0
],
$md5
,
PREG_OFFSET_CAPTURE
);
if
(
isset
(
$md5
[
0
][
0
]))
{
$result
=
$this
->
comments
[
$md5
[
0
][
0
]];
}
}
/*
|----------------------------------------------------------------------------------------
| normalize description
*/
$result
=
$this
->
normalize
(
$result
);
return
$result
;
}
/*
|--------------------------------------------------------------------------------------------
| Method "matchBetweenTwoPatterns"
|--------------------------------------------------------------------------------------------
| This method filters a string between 2 regex patterns and returns it.
*/
private
function
matchBetweenTwoPatterns
(
$string
,
$startPattern
,
$stopPattern
,
$debug
=
false
)
{
/*
|----------------------------------------------------------------------------------------
| Create empty array for the following patterns..
*/
$informations
=
array
();
/*
|----------------------------------------------------------------------------------------
| Filter all start patterns.
*/
$start
=
preg_match_all
(
$startPattern
,
$string
,
$startResults
,
PREG_OFFSET_CAPTURE
);
if
(
$start
==
false
)
{
return
false
;
}
foreach
(
$startResults
[
0
]
as
$result
)
{
preg_match_all
(
'/.*/s'
,
$string
,
$fullResult
,
PREG_OFFSET_CAPTURE
,
$result
[
1
]);
$informations
[
$result
[
1
]
.
'2'
]
=
[
'Offset'
=>
$result
[
1
],
'Type'
=>
'start'
,
'String'
=>
$fullResult
[
0
][
0
][
0
]];
}
/*
|----------------------------------------------------------------------------------------
| Filter all stop patterns.
*/
$stop
=
preg_match_all
(
$stopPattern
,
$string
,
$stopResults
,
PREG_OFFSET_CAPTURE
);
if
(
$stop
==
false
)
{
return
false
;
}
foreach
(
$stopResults
[
0
]
as
$result
)
{
preg_match_all
(
'/.*/s'
,
$string
,
$fullResult
,
PREG_OFFSET_CAPTURE
,
$result
[
1
]);
$informations
[
$result
[
1
]
.
'1'
]
=
[
'Offset'
=>
$result
[
1
],
'Type'
=>
'stop'
,
'String'
=>
$fullResult
[
0
][
0
][
0
]];
}
/*
|----------------------------------------------------------------------------------------
| Sort all informations in the right order.
*/
ksort
(
$informations
);
/*
|----------------------------------------------------------------------------------------
| Create an array for the results.
*/
$result
=
array
();
/*
|----------------------------------------------------------------------------------------
| Loop prepared patterns.
*/
foreach
(
$informations
as
$information
)
{
/*
|------------------------------------------------------------------------------------
| detect start
*/
if
(
$information
[
'Type'
]
==
'start'
)
{
/*
|--------------------------------------------------------------------------------
| fill buffer
*/
$puffer
=
$information
[
'String'
];
}
/*
|------------------------------------------------------------------------------------
| dedect stop
*/
elseif
(
isset
(
$puffer
))
{
/*
|--------------------------------------------------------------------------------
| save result
*/
$result
[]
=
substr
(
$puffer
,
0
,
(
strlen
(
$information
[
'String'
])
*-
1
)
);
/*
|--------------------------------------------------------------------------------
| reset buffer
*/
unset
(
$puffer
);
}
}
/*
|----------------------------------------------------------------------------------------
| Return the results.
*/
return
$result
;
}
/*
|--------------------------------------------------------------------------------------------
| Method "writeComment"
|--------------------------------------------------------------------------------------------
| This method helps to create a correctly formatted comment. With the variable "$fake" only
| the text of the comment is returned.
*/
function
writeComment
(
$comment
,
$title
=
null
,
$fake
=
false
)
{
/*
|----------------------------------------------------------------------------------------
| The following variable starts the string for a new comment.
*/
$k
=
''
;
/*
|----------------------------------------------------------------------------------------
| Subsequently the transmitted comment is cleaned up and normalized.
*/
$comment
=
preg_replace
(
'/[^a-zA-Z:,.0-9_=$+!? äÄüÜÖöß"\'()]/'
,
''
,
$comment
);
$comment
=
$this
->
normalize
(
$comment
);
/*
|----------------------------------------------------------------------------------------
| The beautifier uses certain keywords for e.g. classes and methods. These are filtered
| here in order not to create duplicates in multiple render document processes.
*/
if
(
strpos
(
$comment
,
$title
)
===
0
)
{
$comment
=
substr
(
$comment
,
strlen
(
$title
));
$comment
=
$this
->
normalize
(
$comment
);
}
/*
|----------------------------------------------------------------------------------------
| The comment is returned in fake mode without formatting.
*/
if
(
$fake
==
true
)
{
return
$comment
;
}
/*
|----------------------------------------------------------------------------------------
| The comment is split over several lines (if there are several lines).
*/
$comment
=
wordwrap
(
$comment
,
98
-
(
strlen
(
$this
->
s
())));
$comment
=
explode
(
"
\n
"
,
$comment
);
/*
|----------------------------------------------------------------------------------------
| At the moment, only large comments are supportet.
*/
$large
=
true
;
/*
|----------------------------------------------------------------------------------------
| Large comment...
*/
if
(
$large
==
true
)
{
$k
=
$this
->
s
()
.
'/*'
.
"
\n
"
;
if
(
$title
!=
null
)
{
$k
.=
$this
->
s
()
.
'|'
.
str_repeat
(
'-'
,
100
-
(
strlen
(
$this
->
s
()))
)
.
"
\n
"
;
$k
.=
$this
->
s
()
.
'| '
.
$title
.
"
\n
"
;
}
$k
.=
$this
->
s
()
.
'|'
.
str_repeat
(
'-'
,
100
-
(
strlen
(
$this
->
s
()))
)
.
"
\n
"
;
foreach
(
$comment
as
$com
)
{
if
(
!
empty
(
$com
))
{
$k
.=
$this
->
s
()
.
'| '
.
$com
.
"
\n
"
;
}
}
$k
.=
$this
->
s
()
.
'*/'
.
"
\n
"
;
}
/*
|----------------------------------------------------------------------------------------
| Small comment...
*/
else
{
$k
.=
$this
->
s
()
.
'# '
.
$comment
[
0
]
.
"
\n
"
;
}
/*
|----------------------------------------------------------------------------------------
| Return the comment.
*/
return
$k
;
}
/*
|--------------------------------------------------------------------------------------------
| Method "normalize"
|--------------------------------------------------------------------------------------------
| Diese Method normalisiert einen String.
*/
private
function
normalize
(
$string
)
{
/*
|----------------------------------------------------------------------------------------
| Force the allman style.
*/
$string
=
preg_replace
(
'/{\s*$/m'
,
"
\n
{"
,
$string
);
/*
|----------------------------------------------------------------------------------------
| Remove empty lines and tabs.
*/
$string
=
preg_replace
(
'/^[\s\t]+[\n]*/m'
,
''
,
$string
);
/*
|----------------------------------------------------------------------------------------
| Remove double spaces.
*/
$string
=
preg_replace
(
"#[
\t
]+#"
,
" "
,
$string
);
/*
|----------------------------------------------------------------------------------------
| Return the filtered data.
*/
return
$string
;
}
/*
|--------------------------------------------------------------------------------------------
| Method "s"
|--------------------------------------------------------------------------------------------
| Method "S" This method supports the script at the current indentation level. It returns a
| string with the appropriate spaces.
*/
private
function
s
()
{
$c
=
0
;
$x
=
''
;
while
(
$c
<
$this
->
linedeph
*
4
)
{
$c
++
;
$x
.=
' '
;
}
return
$x
;
}
}
src/
Commands/CodebeautifierCommand
/Template.tpl
→
src/
Managers/BeautifyManager
/Template.tpl
View file @
fe26e9f0
File moved
src/Managers/TerminalManager/TerminalManager.php
View file @
fe26e9f0
...
@@ -10,7 +10,7 @@
...
@@ -10,7 +10,7 @@
| sidekick package.
| sidekick package.
|
|
| Author: Marco Schmiedel <marco.schmiedel@itmax.email>
| Author: Marco Schmiedel <marco.schmiedel@itmax.email>
| Date: 2021-12-2
0 15:38:03
| Date: 2021-12-2
3 19:51:17
|
|
*/
*/
namespace
Ceetrox\Managers\TerminalManager
;
namespace
Ceetrox\Managers\TerminalManager
;
...
@@ -43,6 +43,30 @@
...
@@ -43,6 +43,30 @@
}
}
/*
|--------------------------------------------------------------------------------------------
| Method "brightYellow"
|--------------------------------------------------------------------------------------------
*/
static
function
brightYellow
(
$text
)
{
return
TerminalManager
::
brightColor
(
$text
,
11
);
}
/*
|--------------------------------------------------------------------------------------------
| Method "darkWhite"
|--------------------------------------------------------------------------------------------
*/
static
function
darkWhite
(
$text
)
{
return
TerminalManager
::
darkColor
(
$text
,
15
);
}
/*
/*
|--------------------------------------------------------------------------------------------
|--------------------------------------------------------------------------------------------
| Method "darkColor"
| Method "darkColor"
...
@@ -85,9 +109,9 @@
...
@@ -85,9 +109,9 @@
/*
/*
|----------------------------------------------------------------------------------------
|----------------------------------------------------------------------------------------
| Create
the
counter value.
| Create
a
counter value.
*/
*/
$i1
=
16
;
$i1
=
16
;
/*
/*
|----------------------------------------------------------------------------------------
|----------------------------------------------------------------------------------------
...
@@ -97,15 +121,15 @@
...
@@ -97,15 +121,15 @@
{
{
/*
/*
|------------------------------------------------------------------------------------
|------------------------------------------------------------------------------------
| Calculate 7 columns per line.
| Calculate 7 col
or col
umns per line.
*/
*/
$i1
=
$i1
;
$i1
=
$i1
;
$i2
=
$i1
+
36
;
$i2
=
$i1
+
36
;
$i3
=
$i2
+
36
;
$i3
=
$i2
+
36
;
$i4
=
$i3
+
36
;
$i4
=
$i3
+
36
;
$i5
=
$i4
+
36
;
$i5
=
$i4
+
36
;
$i6
=
$i5
+
36
;
$i6
=
$i5
+
36
;
$i7
=
$i6
+
36
;
$i7
=
$i6
+
36
;
if
(
$i7
>
250
)
{
$i7
=
$i7
-
251
;
}
if
(
$i7
>
250
)
{
$i7
=
$i7
-
251
;
}
...
@@ -130,20 +154,20 @@
...
@@ -130,20 +154,20 @@
/*
/*
|------------------------------------------------------------------------------------
|------------------------------------------------------------------------------------
| Add break at the end of
the
line.
| Add break at the end of
each
line.
*/
*/
$text
.=
"
\n
"
;
$text
.=
"
\n
"
;
/*
/*
|------------------------------------------------------------------------------------
|------------------------------------------------------------------------------------
| Increase value to the
line
counter.
| Increase value to the
color
counter.
*/
*/
$i1
+=
1
;
$i1
+=
1
;
}
}
/*
/*
|----------------------------------------------------------------------------------------
|----------------------------------------------------------------------------------------
| Return string for terminal output.
| Return
a
string for terminal output.
*/
*/
return
$text
;
return
$text
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment