Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Pôle IS
Bundles Symfony 2
ng-model-generator-bundle
Commits
8abab8fd
Commit
8abab8fd
authored
Aug 21, 2018
by
Guillaume Perréal
Browse files
Refactoring massif.
parent
150feea3
Changes
29
Hide whitespace changes
Inline
Side-by-side
src/Models/Context.php
View file @
8abab8fd
...
...
@@ -25,7 +25,7 @@ use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
/**
* Class Context.
*/
class
Context
final
class
Context
{
/**
* @var string
...
...
@@ -250,6 +250,10 @@ class Context
*/
public
function
getSuffix
():
string
{
if
(
empty
(
$this
->
groups
))
{
return
$this
->
isNormalization
()
?
'Response'
:
'Request'
;
}
return
implode
(
''
,
$this
->
groups
);
}
...
...
@@ -258,7 +262,7 @@ class Context
*/
public
function
getKeySuffix
():
string
{
return
sha1
(
$this
->
get
Suffix
(
));
return
sha1
(
$this
->
get
OperationName
()
.
$this
->
getOperationType
()
.
(
$this
->
isNormalization
()
?
'Response'
:
'Request'
));
}
/**
...
...
src/Models/Named.php
deleted
100644 → 0
View file @
150feea3
<?php
declare
(
strict_types
=
1
);
/**
* Copyright (C) 2018 IRSTEA
* All rights reserved.
* @copyright 2018 IRSTEA
* @author guillaume.perreal
*/
namespace
Irstea\NgModelGeneratorBundle\Models
;
/**
* Interface Named
*/
interface
Named
{
/**
* @return string
*/
public
function
getName
():
string
;
}
src/Models/NamedTrait.php
View file @
8abab8fd
...
...
@@ -48,7 +48,7 @@ trait NamedTrait
/**
* @return bool
*/
public
function
get
Referenc
e
():
string
public
function
get
Usag
e
():
string
{
return
$this
->
getName
();
}
...
...
src/Models/Operation.php
View file @
8abab8fd
...
...
@@ -21,12 +21,15 @@ namespace Irstea\NgModelGeneratorBundle\Models;
use
ApiPlatform\Core\Api\OperationType
;
use
Doctrine\Common\Inflector\Inflector
;
use
Irstea\NgModelGeneratorBundle\Models\Types\Type
;
/**
* Class Operation.
*/
class
Operation
final
class
Operation
implements
Walkable
{
use
WalkableTrait
;
/**
* @var string
*/
...
...
@@ -77,8 +80,14 @@ class Operation
* @param array $pathParameters
* @param array $optionalParameters
*/
public
function
__construct
(
string
$name
,
string
$type
,
string
$method
,
string
$path
,
array
$pathParameters
=
[],
array
$optionalParameters
=
[])
{
public
function
__construct
(
string
$name
,
string
$type
,
string
$method
,
string
$path
,
array
$pathParameters
=
[],
array
$optionalParameters
=
[]
)
{
$this
->
name
=
$name
;
$this
->
type
=
$type
;
$this
->
method
=
$method
;
...
...
@@ -215,7 +224,7 @@ class Operation
if
(
$this
->
responseBody
===
null
)
{
return
'HttpResponseBase'
;
}
$type
=
$this
->
responseBody
->
get
Referenc
e
();
$type
=
$this
->
responseBody
->
get
Usag
e
();
if
(
$this
->
isCollectionOperation
())
{
return
"HydraCollection<
$type
>"
;
}
...
...
@@ -336,4 +345,23 @@ class Operation
return
null
;
}
/**
* {@inheritdoc}
*/
protected
function
doWalk
(
callable
$walker
,
array
&
$seen
=
[]):
void
{
if
(
$this
->
requestBody
)
{
$this
->
requestBody
->
walk
(
$walker
,
$seen
);
}
if
(
$this
->
responseBody
)
{
$this
->
responseBody
->
walk
(
$walker
,
$seen
);
}
foreach
(
$this
->
pathParameters
as
$param
)
{
$param
->
walk
(
$walker
,
$seen
);
}
foreach
(
$this
->
optionalParameters
as
$param
)
{
$param
->
walk
(
$walker
,
$seen
);
}
}
}
src/Models/Parameter.php
View file @
8abab8fd
...
...
@@ -19,12 +19,15 @@
namespace
Irstea\NgModelGeneratorBundle\Models
;
use
Irstea\NgModelGeneratorBundle\Models\Types\Type
;
/**
* Class Parameter.
*/
class
Parameter
implements
Referencable
,
Declar
able
class
Parameter
implements
Walk
able
{
use
NamedTrait
;
use
WalkableTrait
;
/**
* @var Type
...
...
@@ -41,6 +44,7 @@ class Parameter implements Referencable, Declarable
*
* @param string $name
* @param Type $type
* @param bool $optional
*/
public
function
__construct
(
string
$name
,
Type
$type
,
bool
$optional
=
false
)
{
...
...
@@ -50,6 +54,7 @@ class Parameter implements Referencable, Declarable
/**
* Get type.
*
* @return Type
*/
public
function
getType
():
Type
...
...
@@ -62,11 +67,12 @@ class Parameter implements Referencable, Declarable
*/
public
function
getDeclaration
():
string
{
return
sprintf
(
'%s%s: %s'
,
$this
->
getEscapedName
(),
$this
->
optional
?
'?'
:
''
,
$this
->
type
->
get
Referenc
e
());
return
sprintf
(
'%s%s: %s'
,
$this
->
getEscapedName
(),
$this
->
optional
?
'?'
:
''
,
$this
->
type
->
get
Usag
e
());
}
/**
* Get optional.
*
* @return bool
*/
public
function
isOptional
():
bool
...
...
@@ -75,11 +81,10 @@ class Parameter implements Referencable, Declarable
}
/**
* @inherit
D
oc
*
{
@inherit
d
oc
}
*/
p
ublic
function
getDescription
():
string
p
rotected
function
doWalk
(
callable
$walker
,
array
&
$seen
=
[]):
void
{
return
''
;
$this
->
type
->
walk
(
$walker
,
$seen
)
;
}
}
src/Models/Property.php
View file @
8abab8fd
...
...
@@ -19,17 +19,20 @@
namespace
Irstea\NgModelGeneratorBundle\Models
;
use
Irstea\NgModelGeneratorBundle\Models\Types\Type
;
/**
* Class Property.
*/
class
Property
implements
Referencable
,
Declar
able
final
class
Property
implements
Walk
able
{
use
NamedTrait
;
use
WalkableTrait
;
/** @var string */
private
$description
;
/** @var Type
|null
*/
/** @var Type */
private
$type
;
/** @var bool */
...
...
@@ -54,7 +57,7 @@ class Property implements Referencable, Declarable
public
function
__construct
(
string
$name
,
string
$description
,
Type
$type
=
null
,
Type
$type
,
bool
$isIdentifier
=
false
,
bool
$isNullable
=
false
,
bool
$isReadonly
=
false
...
...
@@ -70,23 +73,13 @@ class Property implements Referencable, Declarable
/**
* Get type.
*
* @return Type
|null
* @return Type
*/
public
function
getType
():
?
Type
public
function
getType
():
Type
{
return
$this
->
type
;
}
/**
* Set type.
*
* @param Type|null $type
*/
public
function
setType
(
Type
$type
):
void
{
$this
->
type
=
$type
;
}
/**
* Get description.
*
...
...
@@ -147,7 +140,7 @@ class Property implements Referencable, Declarable
$this
->
isReadonly
?
'readonly '
:
''
,
$this
->
getEscapedName
(),
$this
->
isNullable
?
'?'
:
''
,
$this
->
type
->
get
Referenc
e
()
$this
->
type
->
get
Usag
e
()
);
}
...
...
@@ -189,4 +182,12 @@ class Property implements Referencable, Declarable
return
$a
->
name
>
$b
->
name
;
}
/**
* {@inheritdoc}
*/
protected
function
doWalk
(
callable
$walker
,
array
&
$seen
=
[]):
void
{
$this
->
type
->
walk
(
$walker
,
$seen
);
}
}
src/Models/Reference.php
deleted
100644 → 0
View file @
150feea3
<?php
declare
(
strict_types
=
1
);
/*
* irstea/ng-model-generator-bundle generates Typescript interfaces for Angular using api-platform metadata.
* Copyright (C) 2018 IRSTEA
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License and the GNU
* Lesser General Public License along with this program. If not, see
* <https://www.gnu.org/licenses/>.
*/
namespace
Irstea\NgModelGeneratorBundle\Models
;
use
Irstea\NgModelGeneratorBundle\TypeFactoryInterface
;
/**
* Class Ref.
*/
class
Reference
extends
Type
implements
Declarable
{
/**
* @var Type|null
*/
private
$target
=
null
;
/**
* @var string
*/
private
$key
;
/**
* Reference constructor.
*
* @param Type|null $target
* @param string $key
*/
public
function
__construct
(
string
$key
,
Type
$target
=
null
)
{
$this
->
key
=
$key
;
$this
->
target
=
$target
;
}
/**
* {@inheritdoc}
*/
public
function
getReference
():
string
{
return
$this
->
target
?
$this
->
target
->
getReference
()
:
'never'
;
}
/**
* {@inheritdoc}
*/
public
function
hasSimpleReference
():
bool
{
return
$this
->
target
!==
null
&&
$this
->
target
->
hasSimpleReference
();
}
/**
* {@inheritdoc}
*/
public
function
getComment
():
string
{
return
$this
->
target
?
$this
->
target
->
getComment
()
:
'unresolved reference'
;
}
/**
* Get target.
* @return Type|null
*/
public
function
getTarget
():
?Type
{
return
$this
->
target
;
}
/**
* Set target.
*
* @param Type $target
*/
public
function
resolve
(
Type
$target
):
void
{
$this
->
target
=
$target
;
}
/**
* {@inheritDoc}
*/
public
function
optimized
(
TypeFactoryInterface
$factory
):
Type
{
if
(
$this
->
target
)
{
$this
->
target
=
$this
->
target
->
optimized
(
$factory
);
}
return
parent
::
optimized
(
$factory
);
}
/**
* @inheritDoc
*/
public
function
getDeclaration
():
string
{
if
(
$this
->
target
instanceof
Declarable
)
{
return
$this
->
target
->
getDeclaration
();
}
if
(
$this
->
target
instanceof
Referencable
)
{
return
$this
->
target
->
getReference
();
}
return
'never;'
;
}
/**
* @inheritDoc
*/
public
function
getDescription
():
string
{
if
(
$this
->
target
instanceof
Declarable
)
{
return
$this
->
target
->
getDescription
();
}
if
(
$this
->
target
)
{
return
sprintf
(
'reference to undeclarable type %s (%s)'
,
$this
->
target
->
getReference
(),
$this
->
key
);
}
return
sprintf
(
'unresolved reference (%s)'
,
$this
->
key
);
}
}
src/Models/Repository.php
View file @
8abab8fd
...
...
@@ -22,8 +22,10 @@ namespace Irstea\NgModelGeneratorBundle\Models;
/**
* Class Repository.
*/
class
Repository
final
class
Repository
implements
Walkable
{
use
WalkableTrait
;
/**
* @var string
*/
...
...
@@ -72,6 +74,16 @@ class Repository
$this
->
operations
[]
=
$operation
;
}
/**
* {@inheritdoc}
*/
protected
function
doWalk
(
callable
$walker
,
array
&
$seen
=
[]):
void
{
foreach
(
$this
->
operations
as
$op
)
{
$op
->
walk
(
$walker
,
$seen
);
}
}
/**
* @param self $a
* @param self $b
...
...
src/Models/TransformedParameter.php
View file @
8abab8fd
...
...
@@ -19,15 +19,18 @@
namespace
Irstea\NgModelGeneratorBundle\Models
;
use
Irstea\NgModelGeneratorBundle\Models\Types\Object
;
use
Irstea\NgModelGeneratorBundle\Models\Types\Type
;
/**
* Class TransformedParameter.
*/
class
TransformedParameter
extends
Parameter
final
class
TransformedParameter
extends
Parameter
{
/**
* {@inheritdoc}
*/
public
function
__construct
(
string
$name
,
AnonymousObject
$type
,
bool
$optional
=
false
)
public
function
__construct
(
string
$name
,
Type
$type
,
bool
$optional
=
false
)
{
parent
::
__construct
(
$name
,
$type
,
$optional
);
}
...
...
@@ -39,7 +42,7 @@ class TransformedParameter extends Parameter
{
$components
=
[];
/** @var
AnonymousO
bject $type */
/** @var
o
bject $type */
$type
=
$this
->
getType
();
$paramName
=
$this
->
getName
();
...
...
@@ -48,7 +51,7 @@ class TransformedParameter extends Parameter
$rhs
=
$propertyName
!==
$property
->
getName
()
?
"${paramName}[${propertyName}]"
:
"${paramName}.${propertyName}"
;
if
(
$property
->
getType
()
->
get
Referenc
e
()
!==
'string'
)
{
if
(
$property
->
getType
()
->
get
Usag
e
()
!==
'string'
)
{
$rhs
=
"''+${rhs}"
;
}
...
...
src/Models/TypeAlias.php
deleted
100644 → 0
View file @
150feea3
<?php
declare
(
strict_types
=
1
);
/**
* Copyright (C) 2018 IRSTEA
* All rights reserved.
* @copyright 2018 IRSTEA
* @author guillaume.perreal
*/
namespace
Irstea\NgModelGeneratorBundle\Models
;
/**
* Class TypeAlias
*/
class
TypeAlias
extends
NamedType
implements
Declarable
{
/**
* @var Type
*/
private
$target
;
/**
* @var string
*/
private
$description
;
/**
* TypeAlias constructor.
*
* @param string $name
* @param Type $target
* @param string $description
*/
public
function
__construct
(
string
$name
,
Type
$target
,
string
$description
=
''
)
{
parent
::
__construct
(
$name
);
$this
->
target
=
$target
;
$this
->
description
=
$description
;
}
/**
* @inheritDoc
*/
public
function
getDeclaration
():
string
{
return
sprintf
(
'type %s = %s;'
,
$this
->
getName
(),
$this
->
target
?
$this
->
target
->
getReference
()
:
'never'
);
}
/**
* @inheritDoc
*/
public
function
getDescription
():
string
{
return
$this
->
description
;
}
}
src/Models/BuiltinType.php
→
src/Models/
Types/
BuiltinType.php
View file @
8abab8fd
...
...
@@ -17,13 +17,35 @@
* <https://www.gnu.org/licenses/>.
*/
namespace
Irstea\NgModelGeneratorBundle\Models
;
namespace
Irstea\NgModelGeneratorBundle\Models\Types
;
use
Irstea\NgModelGeneratorBundle\Models\NamedTrait
;
/**
* Class BuiltinType.
*/
class
BuiltinType
extends
Named
Type
final
class
BuiltinType
extends
Type
{
use
NamedTrait
;
/**
* BuiltinType constructor.
*
* @param string $name
*/
public
function
__construct
(
string
$name
)
{
$this
->
setName
(
$name
);
}
/**
* {@inheritdoc}
*/
public
function
getUsage
():
string
{
return
$this
->
getName
();
}
/**
* {@inheritdoc}
*/
...
...
src/Models/Collection.php
→
src/Models/
Types/
Collection.php
View file @
8abab8fd
...
...
@@ -17,56 +17,31 @@
* <https://www.gnu.org/licenses/>.
*/
namespace
Irstea\NgModelGeneratorBundle\Models
;
namespace
Irstea\NgModelGeneratorBundle\Models
\Types
;
/**
* Class Collection.
*/
class
Collection
extends
Type
final
class
Collection
extends
Type
{
/** @var Type */
private
$itemType
;
use
DecoratorTrait
;
/**
* Collection constructor.
*
* @param Type $item
Type
* @param Type $item
*/
public
function
__construct
(
Type
$item
Type
)
public
function
__construct
(
Type
$item
)
{
$this
->
itemType
=
$item
Type
;
$this
->
setDecorated
(
$item
)
;
}
/**
* {@inheritdoc}
*/
public
function
get
Referenc
e
():
string
public
function
get
Usag
e
():
string
{
return
sprintf
(
$this
->
itemType
->
hasSimpleReference
()
?
'%s[]'
:
'Array<%s>'
,
$this
->
itemType
->
getReference
());
}
/**
* {@inheritdoc}
*/
public
function
hasSimpleReference
():
bool
{
return
false
;
}
/**
* {@inheritdoc}
*/
public
function
getComment
():
string
{
return
$this
->
itemType
->
getComment
();
}
/**
* @return Type
*/
public
function
getItemType
():
Type
{
return
$this
->
itemType
;
return
sprintf
(
'Array<%s>'
,
$this
->
getDecorated
()
->
getUsage
());
}
/**
...
...
@@ -77,7 +52,7 @@ class Collection extends Type
public
function
mergeWith
(
Type
$t
):
Type
{
if
(
$t
instanceof
self
)
{
return
new
self
(
$this
->
itemType
->
mergeWith
(
$t
->
itemType
));
return
new
self
(
$this
->
getDecorated
()
->
mergeWith
(
$t
->
getDecorated
()
));
}
return
parent
::
mergeWith
(
$t
);
...
...