Skip to content

Commit

Permalink
- Added Literal support for default values
Browse files Browse the repository at this point in the history
- Replaced PopoConfigurator with generated PopoConfigurator
  • Loading branch information
oliwierptak committed Mar 9, 2022
1 parent 46f456f commit e517998
Show file tree
Hide file tree
Showing 14 changed files with 666 additions and 178 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ vendor
!.gitignore
.DS_Store
phpunit.xml
composer.lock
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
language: php
php:
- 7.4
- 8.0

before_script:
Expand Down
14 changes: 12 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
"phpunit/phpunit": "^9",
"symfony/var-dumper": "^5|^6",
"phpstan/phpstan": "^1.4",
"phpmd/phpmd": "^2.11",
"everon/coding-standard": "^2.0"
"phpmd/phpmd": "^2.11"
},
"autoload": {
"psr-4": {
Expand Down Expand Up @@ -52,12 +51,14 @@
"vendor/bin/phpunit --testdox --group unit,functional"
],
"report": [
"bin/popo report -s tests/popo.yml",
"bin/popo report -s tests/fixtures/popo.yml",
"bin/popo report -s tests/fixtures/popo-namespace-root.yml",
"bin/popo report -s tests/fixtures/popo-readme.yml",
"bin/popo report -c tests/fixtures/bundles/project.config.yml -s tests/fixtures/bundles/"
],
"popo": [
"bin/popo generate -s popo.yml",
"bin/popo generate -s tests/fixtures/popo.yml",
"bin/popo generate -s tests/fixtures/popo-namespace-root.yml",
"bin/popo generate -s tests/fixtures/popo-readme.yml",
Expand All @@ -69,6 +70,15 @@
],
"phpstan": [
"vendor/bin/phpstan analyse -c phpstan.neon"
],
"cs-fix": [
"vendor/bin/phpcbf -q --standard=.phpcs.xml ${1-src}"
],
"cs-check": [
"vendor/bin/phpcs --colors -p -s --report-width=120 --extensions=php --standard=.phpcs.xml ${1-src}"
],
"cs-list": [
"vendor/bin/phpcs -e --standard=.phpcs.xml"
]
},
"scripts-descriptions": {
Expand Down
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ parameters:
- tests/bootstrap.php
excludePaths:
- tests/*
- src/Popo/PopoConfigurator.php
51 changes: 51 additions & 0 deletions popo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
$:
config:
namespace: Popo
outputPath: src/
default:
comment: |
@SuppressWarnings(PHPMD)
@phpcs:ignoreFile
Popo:
PopoConfigurator:
default:
phpFilePluginCollection:
- \Popo\Plugin\PhpFilePlugin\StrictTypesPhpFilePlugin::class
- \Popo\Plugin\PhpFilePlugin\CommentPhpFilePlugin::class
namespacePluginCollection:
- \Popo\Plugin\NamespacePlugin\UseUnexpectedValueExceptionPlugin::class
classPluginCollection:
- \Popo\Plugin\ClassPlugin\DateTimeMethodClassPlugin::class
- \Popo\Plugin\ClassPlugin\ExtendClassPlugin::class
- \Popo\Plugin\ClassPlugin\FromArrayClassPlugin::class
- \Popo\Plugin\ClassPlugin\ImplementClassPlugin::class
- \Popo\Plugin\ClassPlugin\IsNewClassPlugin::class
- \Popo\Plugin\ClassPlugin\ListModifiedPropertiesClassPlugin::class
- \Popo\Plugin\ClassPlugin\MetadataClassPlugin::class
- \Popo\Plugin\ClassPlugin\ModifiedToArrayClassPlugin::class
- \Popo\Plugin\ClassPlugin\PopoMethodClassPlugin::class
- \Popo\Plugin\ClassPlugin\RequireAllClassPlugin::class
- \Popo\Plugin\ClassPlugin\ToArrayClassPlugin::class
- \Popo\Plugin\ClassPlugin\UpdateMapClassPlugin::class
propertyPluginCollection:
- \Popo\Plugin\PropertyPlugin\AddItemPropertyMethodPlugin::class
- \Popo\Plugin\PropertyPlugin\DefinePropertyPlugin::class
- \Popo\Plugin\PropertyPlugin\GetPropertyMethodPlugin::class
- \Popo\Plugin\PropertyPlugin\HasPropertyMethodPlugin::class
- \Popo\Plugin\PropertyPlugin\RequirePropertyMethodPlugin::class
- \Popo\Plugin\PropertyPlugin\SetPropertyMethodPlugin::class
property: [
{name: schemaPath}
{name: namespace}
{name: namespaceRoot}
{name: outputPath}
{name: phpFilePluginCollection, type: array, itemType: string, itemName: phpFilePluginClass}
{name: namespacePluginCollection, type: array, itemType: string, itemName: namespacePluginClass}
{name: classPluginCollection, type: array, itemType: string, itemName: classPluginClass}
{name: propertyPluginCollection, type: array, itemType: string, itemName: propertyPluginClass}
{name: schemaConfigFilename}
{name: schemaPathFilter}
{name: schemaFilenameMask, default: '*.popo.yml'}
{name: shouldIgnoreNonExistingSchemaFolder, type: bool}
]}}
16 changes: 8 additions & 8 deletions src/Popo/Command/CreateSchemaCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,15 @@ protected function executeCommand(InputInterface $input, OutputInterface $output
$output->writeln($generatedSchema);

$question = new ConfirmationQuestion(
'<fg=green>Save schema under: \'' . $configurator->getSchemaPath() . '\'?</> <fg=gray>[y/n]</>', true
'<fg=green>Save schema under: \'' . $configurator->requireSchemaPath() . '\'?</> <fg=gray>[y/n]</>', true
);
if (!$helper->ask($input, $output, $question)) {
return Command::SUCCESS;
}

if (file_exists($configurator->getSchemaPath())) {
if (file_exists($configurator->requireSchemaPath())) {
$question = new ConfirmationQuestion(
'<fg=yellow>Schema file under: \'' . $configurator->getSchemaPath(
'<fg=yellow>Schema file under: \'' . $configurator->requireSchemaPath(
) . '\' already exists. Overwrite?</> <fg=gray>[</><fg=white>y</><fg=gray>/n]</>', true
);
if (!$helper->ask($input, $output, $question)) {
Expand All @@ -124,10 +124,10 @@ protected function executeCommand(InputInterface $input, OutputInterface $output
$this->saveSchemaFile($configurator, $generatedSchema);

$output->writeln('');
$output->writeln('<fg=green>Saved schema file under</>: ' . $configurator->getSchemaPath());
$output->writeln('<fg=green>Saved schema file under</>: ' . $configurator->requireSchemaPath());

$output->writeln('');
$output->writeln('<fg=green>Run</>: vendor/bin/popo generate -s ' . $configurator->getSchemaPath());
$output->writeln('<fg=green>Run</>: vendor/bin/popo generate -s ' . $configurator->requireSchemaPath());

$configurator->setSchemaFilenameMask('*.yml');

Expand All @@ -138,7 +138,7 @@ protected function saveSchemaFile(PopoConfigurator $configurator, string $genera
{
$handle = null;
try {
$handle = fopen($configurator->getSchemaPath(), 'w');
$handle = fopen($configurator->requireSchemaPath(), 'w');
/** @phpstan-ignore-next-line */
fputs($handle, $generatedSchema);
}
Expand All @@ -160,8 +160,8 @@ protected function runCommand(PopoConfigurator $configurator, OutputInterface $o
$command = $this->getApplication()->find(GenerateCommand::COMMAND_NAME);

$arguments = [
'-s' => $configurator->getSchemaPath(),
'-o' => $configurator->getOutputPath(),
'-s' => $configurator->requireSchemaPath(),
'-o' => $configurator->requireOutputPath(),
];

$parameters = new ArrayInput($arguments);
Expand Down
2 changes: 1 addition & 1 deletion src/Popo/Command/GenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ protected function buildConfigurator(InputInterface $input): PopoConfigurator
static::OPTION_SCHEMA_FILENAME_MASK
) : '*.popo.yml'
)
->setIgnoreNonExistingSchemaFolder(
->setShouldIgnoreNonExistingSchemaFolder(
(bool) ($input->hasOption(static::OPTION_IGNORE_NON_EXISTING_SCHEMA_FOLDER) ? $input->getOption(
static::OPTION_IGNORE_NON_EXISTING_SCHEMA_FOLDER
) : false)
Expand Down
8 changes: 4 additions & 4 deletions src/Popo/Loader/SchemaLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ protected function loadSchemaFiles(PopoConfigurator $configurator): array
$this->validate($configurator);

$files = [];
foreach ($this->extractPaths($configurator->getSchemaPath()) as $path) {
foreach ($this->extractPaths($configurator->requireSchemaPath()) as $path) {
if (is_dir($path)) {
$files = array_merge(
$files,
Expand All @@ -108,13 +108,13 @@ protected function loadSchemaFiles(PopoConfigurator $configurator): array

protected function validate(PopoConfigurator $configurator): void
{
foreach ($this->extractPaths($configurator->getSchemaPath()) as $path) {
if ($configurator->isIgnoreNonExistingSchemaFolder() === false) {
foreach ($this->extractPaths($configurator->requireSchemaPath()) as $path) {
if ($configurator->shouldIgnoreNonExistingSchemaFolder() === false) {
$this->validatePath($path);
}
}

if (trim((string) $configurator->getSchemaConfigFilename()) !== '') {
if ($configurator->hasSchemaConfigFilename()) {
$this->validatePath((string)$configurator->getSchemaConfigFilename());
}
}
Expand Down
15 changes: 15 additions & 0 deletions src/Popo/Plugin/ClassPlugin/MetadataClassPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,21 @@ protected function generateMetadataProperties(BuilderPluginInterface $builder):
continue;
}

if ($builder->getSchemaInspector()->isArray($property->getType())) {
$values = [];

foreach ($property->getDefault() ?? [] as $key => $defaultValue) {
if ($builder->getSchemaInspector()->isLiteral($defaultValue)) {
$defaultValue = new Literal($defaultValue);
}

$values[$key] = $defaultValue;
}

$metadata[$property->getName()][PopoDefinesInterface::SCHEMA_PROPERTY_DEFAULT] = $values;
continue;
}

if ($builder->getSchemaInspector()->isDateTimeProperty($property->getType())) {
$metadata[$property->getName()][PopoDefinesInterface::SCHEMA_PROPERTY_DEFAULT] = $property->getDefault();
continue;
Expand Down
20 changes: 1 addition & 19 deletions src/Popo/Plugin/PropertyPlugin/DefinePropertyPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Popo\Plugin\PropertyPlugin;

use Nette\PhpGenerator\Literal;
use Popo\Plugin\BuilderPluginInterface;
use Popo\Plugin\PropertyPluginInterface;
use Popo\Schema\Property\Property;
Expand All @@ -13,24 +12,7 @@ class DefinePropertyPlugin implements PropertyPluginInterface
{
public function run(BuilderPluginInterface $builder, Property $property): void
{
$value = $property->getDefault();
if ($builder->getSchemaInspector()->isPopoProperty($property->getType()) ||
$builder->getSchemaInspector()->isDateTimeProperty($property->getType())) {
$value = null;
}
else {
if ($builder->getSchemaInspector()->isLiteral($property->getDefault())) {
$value = new Literal($property->getDefault());
}
}

if ($builder->getSchemaInspector()->isBool($property->getType())) {
$value = (bool) $value;
}

if ($value === null && $builder->getSchemaInspector()->isArray($property->getType())) {
$value = [];
}
$value = $builder->getSchemaGenerator()->generateDefaultTypeValue($property);

$builder->getClass()
->addProperty($property->getName(), $value)
Expand Down
Loading

0 comments on commit e517998

Please sign in to comment.