* initial commit
This commit is contained in:
commit
ed29fb7f97
7
.gitignore
vendored
Normal file
7
.gitignore
vendored
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
.idea
|
||||||
|
composer.phar
|
||||||
|
|
||||||
|
clover.xml
|
||||||
|
coveralls-upload.json
|
||||||
|
phpunit.xml
|
||||||
|
vendor/
|
||||||
9
cli-config.php
Normal file
9
cli-config.php
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
$container = require 'config/container.php';
|
||||||
|
|
||||||
|
return new \Symfony\Component\Console\Helper\HelperSet([
|
||||||
|
'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper(
|
||||||
|
$container->get('doctrine.entity_manager.orm_default')
|
||||||
|
),
|
||||||
|
]);
|
||||||
47
composer.json
Normal file
47
composer.json
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
{
|
||||||
|
"name": "zendframework/zend-expressive-skeleton",
|
||||||
|
"description": "Zend expressive skeleton. Begin developing PSR-7 middleware applications in seconds!",
|
||||||
|
"type": "project",
|
||||||
|
"homepage": "https://github.com/zendframework/zend-expressive-skeleton",
|
||||||
|
"license": "BSD-3-Clause",
|
||||||
|
"require": {
|
||||||
|
"php": "^5.6 || ^7.0",
|
||||||
|
"roave/security-advisories": "dev-master",
|
||||||
|
"zendframework/zend-expressive": "^1.0",
|
||||||
|
"zendframework/zend-expressive-helpers": "^2.0",
|
||||||
|
"zendframework/zend-stdlib": "^2.7 || ^3.0",
|
||||||
|
"zendframework/zend-expressive-fastroute": "^1.0",
|
||||||
|
"zendframework/zend-servicemanager": "^2.7.3 || ^3.0",
|
||||||
|
"zendframework/zend-json": "3.0.0",
|
||||||
|
"zendframework/zend-hydrator": "2.2.1",
|
||||||
|
"zendframework/zend-filter": "2.7.1",
|
||||||
|
"dasprid/container-interop-doctrine": "0.2.3"
|
||||||
|
},
|
||||||
|
"require-dev": {
|
||||||
|
"phpunit/phpunit": "^4.8",
|
||||||
|
"squizlabs/php_codesniffer": "^2.3",
|
||||||
|
"filp/whoops": "^1.1 || ^2.0"
|
||||||
|
},
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": {
|
||||||
|
"App\\": "src/App/"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"autoload-dev": {
|
||||||
|
"psr-4": {
|
||||||
|
"AppTest\\": "test/AppTest/"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"check": [
|
||||||
|
"@cs-check",
|
||||||
|
"@test"
|
||||||
|
],
|
||||||
|
"cs-check": "phpcs",
|
||||||
|
"cs-fix": "phpcbf",
|
||||||
|
"serve": "php -S 0.0.0.0:8080 -t public public/index.php",
|
||||||
|
"test": "phpunit --colors=always",
|
||||||
|
"test-coverage": "phpunit --colors=always --coverage-clover clover.xml",
|
||||||
|
"upload-coverage": "coveralls -v"
|
||||||
|
}
|
||||||
|
}
|
||||||
3144
composer.lock
generated
Normal file
3144
composer.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
2
config/autoload/.gitignore
vendored
Normal file
2
config/autoload/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
local.php
|
||||||
|
*.local.php
|
||||||
28
config/autoload/dependencies.global.php
Normal file
28
config/autoload/dependencies.global.php
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Zend\Expressive\Application;
|
||||||
|
use Zend\Expressive\Container\ApplicationFactory;
|
||||||
|
use Zend\Expressive\Helper;
|
||||||
|
|
||||||
|
return [
|
||||||
|
// Provides application-wide services.
|
||||||
|
// We recommend using fully-qualified class names whenever possible as
|
||||||
|
// service names.
|
||||||
|
'dependencies' => [
|
||||||
|
// Use 'invokables' for constructor-less services, or services that do
|
||||||
|
// not require arguments to the constructor. Map a service name to the
|
||||||
|
// class name.
|
||||||
|
'invokables' => [
|
||||||
|
// Fully\Qualified\InterfaceName::class => Fully\Qualified\ClassName::class,
|
||||||
|
Helper\ServerUrlHelper::class => Helper\ServerUrlHelper::class,
|
||||||
|
],
|
||||||
|
// Use 'factories' for services provided by callbacks/factory classes.
|
||||||
|
'factories' => [
|
||||||
|
Application::class => ApplicationFactory::class,
|
||||||
|
Helper\UrlHelper::class => Helper\UrlHelperFactory::class,
|
||||||
|
'doctrine.entity_manager.orm_default' => \ContainerInteropDoctrine\EntityManagerFactory::class,
|
||||||
|
'doctrine.hydrator' => \App\Hydrator\DoctrineObjectFactory::class,
|
||||||
|
\App\Service\CiConfigService::class => \App\Service\CiConfigServiceFactory::class,
|
||||||
|
],
|
||||||
|
],
|
||||||
|
];
|
||||||
68
config/autoload/doctrine.global.php
Normal file
68
config/autoload/doctrine.global.php
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
return [
|
||||||
|
'doctrine' => [
|
||||||
|
'driver' => [
|
||||||
|
'orm_default' => [
|
||||||
|
'class' => \Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain::class,
|
||||||
|
'drivers' => [
|
||||||
|
'App\Entity' => 'my_entity',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
'my_entity' => [
|
||||||
|
'class' => \Doctrine\ORM\Mapping\Driver\AnnotationDriver::class,
|
||||||
|
'cache' => 'array',
|
||||||
|
'paths' => __DIR__ . '/../../src/App/Entity',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
'configuration' => [
|
||||||
|
'orm_default' => [
|
||||||
|
// 'datetime_functions' => [
|
||||||
|
// 'date' => Oro\ORM\Query\AST\Functions\SimpleFunction::class,
|
||||||
|
// 'time' => Oro\ORM\Query\AST\Functions\SimpleFunction::class,
|
||||||
|
// 'timestamp' => Oro\ORM\Query\AST\Functions\SimpleFunction::class,
|
||||||
|
// 'convert_tz' => Oro\ORM\Query\AST\Functions\DateTime\ConvertTz::class,
|
||||||
|
// ],
|
||||||
|
'numeric_functions' => [
|
||||||
|
// 'timestampdiff' => Oro\ORM\Query\AST\Functions\Numeric\TimestampDiff::class,
|
||||||
|
// 'dayofyear' => Oro\ORM\Query\AST\Functions\SimpleFunction::class,
|
||||||
|
// 'dayofmonth' => Oro\ORM\Query\AST\Functions\SimpleFunction::class,
|
||||||
|
// 'dayofweek' => Oro\ORM\Query\AST\Functions\SimpleFunction::class,
|
||||||
|
// 'week' => Oro\ORM\Query\AST\Functions\SimpleFunction::class,
|
||||||
|
// 'day' => Oro\ORM\Query\AST\Functions\SimpleFunction::class,
|
||||||
|
// 'hour' => Oro\ORM\Query\AST\Functions\SimpleFunction::class,
|
||||||
|
// 'minute' => Oro\ORM\Query\AST\Functions\SimpleFunction::class,
|
||||||
|
// 'month' => Oro\ORM\Query\AST\Functions\SimpleFunction::class,
|
||||||
|
// 'quarter' => Oro\ORM\Query\AST\Functions\SimpleFunction::class,
|
||||||
|
// 'second' => Oro\ORM\Query\AST\Functions\SimpleFunction::class,
|
||||||
|
// 'year' => Oro\ORM\Query\AST\Functions\SimpleFunction::class,
|
||||||
|
// 'sign' => Oro\ORM\Query\AST\Functions\Numeric\Sign::class,
|
||||||
|
// 'pow' => Oro\ORM\Query\AST\Functions\Numeric\Pow::class,
|
||||||
|
],
|
||||||
|
// 'string_functions' => [
|
||||||
|
// 'md5' => Oro\ORM\Query\AST\Functions\SimpleFunction::class,
|
||||||
|
// 'group_concat' => Oro\ORM\Query\AST\Functions\String\GroupConcat::class,
|
||||||
|
// 'cast' => Oro\ORM\Query\AST\Functions\Cast::class,
|
||||||
|
// 'concat_ws' => Oro\ORM\Query\AST\Functions\String\ConcatWs::class
|
||||||
|
// ]
|
||||||
|
// 'filters' => [
|
||||||
|
// 'soft-deleteable' => Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter::class,
|
||||||
|
// ],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
'event_manager' => [
|
||||||
|
'orm_default' => [
|
||||||
|
'subscribers' => [
|
||||||
|
// Gedmo\Timestampable\TimestampableListener::class,
|
||||||
|
// 'Gedmo\Tree\TreeListener',
|
||||||
|
// 'Gedmo\SoftDeleteable\SoftDeleteableListener',
|
||||||
|
// 'Gedmo\Translatable\TranslatableListener',
|
||||||
|
// 'Gedmo\Blameable\BlameableListener',
|
||||||
|
// 'Gedmo\Loggable\LoggableListener',
|
||||||
|
// 'Gedmo\Sortable\SortableListener',
|
||||||
|
// 'Gedmo\Sluggable\SluggableListener',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
];
|
||||||
14
config/autoload/doctrine.local.dist.php
Normal file
14
config/autoload/doctrine.local.dist.php
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
return [
|
||||||
|
'doctrine' => [
|
||||||
|
'connection' => [
|
||||||
|
'orm_default' => [
|
||||||
|
'params' => [
|
||||||
|
'url' => 'mysqli://user:passwd@host/database',
|
||||||
|
'charset' => 'UTF8',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
];
|
||||||
7
config/autoload/local.php.dist
Normal file
7
config/autoload/local.php.dist
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
return [
|
||||||
|
'debug' => true,
|
||||||
|
|
||||||
|
'config_cache_enabled' => false,
|
||||||
|
];
|
||||||
70
config/autoload/middleware-pipeline.global.php
Normal file
70
config/autoload/middleware-pipeline.global.php
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
<?php
|
||||||
|
use Zend\Expressive\Container\ApplicationFactory;
|
||||||
|
use Zend\Expressive\Helper;
|
||||||
|
|
||||||
|
return [
|
||||||
|
'dependencies' => [
|
||||||
|
'factories' => [
|
||||||
|
Helper\ServerUrlMiddleware::class => Helper\ServerUrlMiddlewareFactory::class,
|
||||||
|
Helper\UrlHelperMiddleware::class => Helper\UrlHelperMiddlewareFactory::class,
|
||||||
|
],
|
||||||
|
],
|
||||||
|
// This can be used to seed pre- and/or post-routing middleware
|
||||||
|
'middleware_pipeline' => [
|
||||||
|
// An array of middleware to register. Each item is of the following
|
||||||
|
// specification:
|
||||||
|
//
|
||||||
|
// [
|
||||||
|
// Required:
|
||||||
|
// 'middleware' => 'Name or array of names of middleware services and/or callables',
|
||||||
|
// Optional:
|
||||||
|
// 'path' => '/path/to/match', // string; literal path prefix to match
|
||||||
|
// // middleware will not execute
|
||||||
|
// // if path does not match!
|
||||||
|
// 'error' => true, // boolean; true for error middleware
|
||||||
|
// 'priority' => 1, // int; higher values == register early;
|
||||||
|
// // lower/negative == register last;
|
||||||
|
// // default is 1, if none is provided.
|
||||||
|
// ],
|
||||||
|
//
|
||||||
|
// While the ApplicationFactory ignores the keys associated with
|
||||||
|
// specifications, they can be used to allow merging related values
|
||||||
|
// defined in multiple configuration files/locations. This file defines
|
||||||
|
// some conventional keys for middleware to execute early, routing
|
||||||
|
// middleware, and error middleware.
|
||||||
|
'always' => [
|
||||||
|
'middleware' => [
|
||||||
|
// Add more middleware here that you want to execute on
|
||||||
|
// every request:
|
||||||
|
// - bootstrapping
|
||||||
|
// - pre-conditions
|
||||||
|
// - modifications to outgoing responses
|
||||||
|
Helper\ServerUrlMiddleware::class,
|
||||||
|
\App\Middleware\PreFlightMiddleware::class,
|
||||||
|
],
|
||||||
|
'priority' => 10000,
|
||||||
|
],
|
||||||
|
|
||||||
|
'routing' => [
|
||||||
|
'middleware' => [
|
||||||
|
ApplicationFactory::ROUTING_MIDDLEWARE,
|
||||||
|
Helper\UrlHelperMiddleware::class,
|
||||||
|
// Add more middleware here that needs to introspect the routing
|
||||||
|
// results; this might include:
|
||||||
|
// - route-based authentication
|
||||||
|
// - route-based validation
|
||||||
|
// - etc.
|
||||||
|
ApplicationFactory::DISPATCH_MIDDLEWARE,
|
||||||
|
],
|
||||||
|
'priority' => 1,
|
||||||
|
],
|
||||||
|
|
||||||
|
'error' => [
|
||||||
|
'middleware' => [
|
||||||
|
// Add error middleware here.
|
||||||
|
],
|
||||||
|
'error' => true,
|
||||||
|
'priority' => -10000,
|
||||||
|
],
|
||||||
|
],
|
||||||
|
];
|
||||||
55
config/autoload/routes.global.php
Normal file
55
config/autoload/routes.global.php
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
return [
|
||||||
|
'dependencies' => [
|
||||||
|
'invokables' => [
|
||||||
|
Zend\Expressive\Router\RouterInterface::class => Zend\Expressive\Router\FastRouteRouter::class,
|
||||||
|
App\Action\PingAction::class => App\Action\PingAction::class,
|
||||||
|
],
|
||||||
|
'factories' => [
|
||||||
|
App\Action\CiConfigAction::class => App\Action\CiConfigFactory::class,
|
||||||
|
App\Action\CiConfigItemAction::class => App\Action\CiConfigItemFactory::class,
|
||||||
|
App\Action\HomePageAction::class => App\Action\HomePageFactory::class,
|
||||||
|
],
|
||||||
|
],
|
||||||
|
|
||||||
|
'routes' => [
|
||||||
|
[
|
||||||
|
'name' => 'home',
|
||||||
|
'path' => '/',
|
||||||
|
'middleware' => App\Action\HomePageAction::class,
|
||||||
|
'allowed_methods' => ['GET'],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'name' => 'api.ping',
|
||||||
|
'path' => '/api/ping',
|
||||||
|
'middleware' => App\Action\PingAction::class,
|
||||||
|
'allowed_methods' => ['GET'],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'name' => 'api.ci-config',
|
||||||
|
'path' => '/api/ci-config',
|
||||||
|
'middleware' => App\Action\CiConfigAction::class,
|
||||||
|
'allowed_methods' => ['GET','POST'],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'name' => 'api.ci-config-id',
|
||||||
|
'path' => '/api/ci-config/{id:\d+}',
|
||||||
|
'middleware' => App\Action\CiConfigAction::class,
|
||||||
|
'allowed_methods' => ['GET','DELETE'],
|
||||||
|
],
|
||||||
|
// [
|
||||||
|
// 'name' => 'api.ci-config-item',
|
||||||
|
// 'path' => '/api/ci-config-item',
|
||||||
|
// 'middleware' => App\Action\CiConfigItemAction::class,
|
||||||
|
// 'allowed_methods' => ['GET','POST'],
|
||||||
|
// ],
|
||||||
|
[
|
||||||
|
'name' => 'api.ci-config-item-id',
|
||||||
|
'path' => '/api/ci-config-item/{id:\d+}',
|
||||||
|
'middleware' => App\Action\CiConfigItemAction::class,
|
||||||
|
'allowed_methods' => ['DELETE'],
|
||||||
|
// 'allowed_methods' => ['GET','DELETE'],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
];
|
||||||
14
config/autoload/zend-expressive.global.php
Normal file
14
config/autoload/zend-expressive.global.php
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
return [
|
||||||
|
'debug' => false,
|
||||||
|
|
||||||
|
'config_cache_enabled' => false,
|
||||||
|
|
||||||
|
'zend-expressive' => [
|
||||||
|
'error_handler' => [
|
||||||
|
'template_404' => 'error::404',
|
||||||
|
'template_error' => 'error::error',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
];
|
||||||
35
config/config.php
Normal file
35
config/config.php
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Zend\Stdlib\ArrayUtils;
|
||||||
|
use Zend\Stdlib\Glob;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configuration files are loaded in a specific order. First ``global.php``, then ``*.global.php``.
|
||||||
|
* then ``local.php`` and finally ``*.local.php``. This way local settings overwrite global settings.
|
||||||
|
*
|
||||||
|
* The configuration can be cached. This can be done by setting ``config_cache_enabled`` to ``true``.
|
||||||
|
*
|
||||||
|
* Obviously, if you use closures in your config you can't cache it.
|
||||||
|
*/
|
||||||
|
|
||||||
|
$cachedConfigFile = 'data/cache/app_config.php';
|
||||||
|
|
||||||
|
$config = [];
|
||||||
|
if (is_file($cachedConfigFile)) {
|
||||||
|
// Try to load the cached config
|
||||||
|
$config = include $cachedConfigFile;
|
||||||
|
} else {
|
||||||
|
// Load configuration from autoload path
|
||||||
|
foreach (Glob::glob('config/autoload/{{,*.}global,{,*.}local}.php', Glob::GLOB_BRACE) as $file) {
|
||||||
|
$config = ArrayUtils::merge($config, include $file);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cache config if enabled
|
||||||
|
if (isset($config['config_cache_enabled']) && $config['config_cache_enabled'] === true) {
|
||||||
|
file_put_contents($cachedConfigFile, '<?php return ' . var_export($config, true) . ';');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return an ArrayObject so we can inject the config as a service in Aura.Di
|
||||||
|
// and still use array checks like ``is_array``.
|
||||||
|
return new ArrayObject($config, ArrayObject::ARRAY_AS_PROPS);
|
||||||
16
config/container.php
Normal file
16
config/container.php
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Zend\ServiceManager\Config;
|
||||||
|
use Zend\ServiceManager\ServiceManager;
|
||||||
|
|
||||||
|
// Load configuration
|
||||||
|
$config = require __DIR__ . '/config.php';
|
||||||
|
|
||||||
|
// Build container
|
||||||
|
$container = new ServiceManager();
|
||||||
|
(new Config($config['dependencies']))->configureServiceManager($container);
|
||||||
|
|
||||||
|
// Inject config
|
||||||
|
$container->setService('config', $config);
|
||||||
|
|
||||||
|
return $container;
|
||||||
2
data/.gitignore
vendored
Normal file
2
data/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
*
|
||||||
|
!.gitignore
|
||||||
20
phpcs.xml
Normal file
20
phpcs.xml
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
<?xml version="1.0"?>
|
||||||
|
<ruleset name="Zend Framework coding standard">
|
||||||
|
<description>Zend Framework coding standard</description>
|
||||||
|
|
||||||
|
<!-- display progress -->
|
||||||
|
<arg value="p"/>
|
||||||
|
<arg name="colors"/>
|
||||||
|
|
||||||
|
<!-- inherit rules from: -->
|
||||||
|
<rule ref="PSR2"/>
|
||||||
|
<rule ref="Generic.Arrays.DisallowLongArraySyntax"/>
|
||||||
|
<rule ref="Squiz.WhiteSpace.SuperfluousWhitespace">
|
||||||
|
<properties>
|
||||||
|
<property name="ignoreBlankLines" value="false"/>
|
||||||
|
</properties>
|
||||||
|
</rule>
|
||||||
|
|
||||||
|
<!-- Paths to check -->
|
||||||
|
<file>src</file>
|
||||||
|
</ruleset>
|
||||||
17
phpunit.xml.dist
Normal file
17
phpunit.xml.dist
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
|
||||||
|
bootstrap="vendor/autoload.php"
|
||||||
|
colors="true">
|
||||||
|
<testsuites>
|
||||||
|
<testsuite name="App\\Tests">
|
||||||
|
<directory>./test</directory>
|
||||||
|
</testsuite>
|
||||||
|
</testsuites>
|
||||||
|
|
||||||
|
<filter>
|
||||||
|
<whitelist>
|
||||||
|
<directory suffix=".php">./src</directory>
|
||||||
|
</whitelist>
|
||||||
|
</filter>
|
||||||
|
</phpunit>
|
||||||
17
public/.htaccess
Normal file
17
public/.htaccess
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
RewriteEngine On
|
||||||
|
# The following rule tells Apache that if the requested filename
|
||||||
|
# exists, simply serve it.
|
||||||
|
RewriteCond %{REQUEST_FILENAME} -s [OR]
|
||||||
|
RewriteCond %{REQUEST_FILENAME} -l [OR]
|
||||||
|
RewriteCond %{REQUEST_FILENAME} -d
|
||||||
|
RewriteRule ^.*$ - [NC,L]
|
||||||
|
|
||||||
|
# The following rewrites all other queries to index.php. The
|
||||||
|
# condition ensures that if you are using Apache aliases to do
|
||||||
|
# mass virtual hosting, the base path will be prepended to
|
||||||
|
# allow proper resolution of the index.php file; it will work
|
||||||
|
# in non-aliased environments as well, providing a safe, one-size
|
||||||
|
# fits all solution.
|
||||||
|
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
|
||||||
|
RewriteRule ^(.*) - [E=BASE:%1]
|
||||||
|
RewriteRule ^(.*)$ %{ENV:BASE}index.php [NC,L]
|
||||||
BIN
public/favicon.ico
Normal file
BIN
public/favicon.ico
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.4 KiB |
18
public/index.php
Normal file
18
public/index.php
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
// Delegate static file requests back to the PHP built-in webserver
|
||||||
|
if (php_sapi_name() === 'cli-server'
|
||||||
|
&& is_file(__DIR__ . parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH))
|
||||||
|
) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
chdir(dirname(__DIR__));
|
||||||
|
require 'vendor/autoload.php';
|
||||||
|
|
||||||
|
/** @var \Interop\Container\ContainerInterface $container */
|
||||||
|
$container = require 'config/container.php';
|
||||||
|
|
||||||
|
/** @var \Zend\Expressive\Application $app */
|
||||||
|
$app = $container->get(\Zend\Expressive\Application::class);
|
||||||
|
$app->run();
|
||||||
BIN
public/zf-logo.png
Normal file
BIN
public/zf-logo.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 600 B |
158
src/App/Action/AbstractAction.php
Normal file
158
src/App/Action/AbstractAction.php
Normal file
@ -0,0 +1,158 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Action;
|
||||||
|
|
||||||
|
use Psr\Http\Message\ResponseInterface;
|
||||||
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
|
use Zend\Diactoros\Response\JsonResponse;
|
||||||
|
use Zend\Stratigility\MiddlewareInterface;
|
||||||
|
use Zend\Json\Json;
|
||||||
|
|
||||||
|
abstract class AbstractAction implements MiddlewareInterface
|
||||||
|
{
|
||||||
|
const IDENTIFIER_NAME = 'id';
|
||||||
|
|
||||||
|
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||||
|
{
|
||||||
|
$requestMethod = strtoupper($request->getMethod());
|
||||||
|
$id = $request->getAttribute(static::IDENTIFIER_NAME);
|
||||||
|
|
||||||
|
switch ($requestMethod) {
|
||||||
|
case 'GET':
|
||||||
|
return isset($id)
|
||||||
|
? $this->get($request, $response, $next)
|
||||||
|
: $this->getList($request, $response, $next);
|
||||||
|
case 'POST':
|
||||||
|
return $this->create($request, $response, $next);
|
||||||
|
case 'PUT':
|
||||||
|
return $this->update($request, $response, $next);
|
||||||
|
case 'DELETE':
|
||||||
|
return isset($id)
|
||||||
|
? $this->delete($request, $response, $next)
|
||||||
|
: $this->deleteList($request, $response, $next);
|
||||||
|
case 'HEAD':
|
||||||
|
return $this->head($request, $response, $next);
|
||||||
|
case 'OPTIONS':
|
||||||
|
return $this->options($request, $response, $next);
|
||||||
|
case 'PATCH':
|
||||||
|
return $this->patch($request, $response, $next);
|
||||||
|
default:
|
||||||
|
return $next($request, $response);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||||
|
{
|
||||||
|
return $this->createResponse(['content' => 'Method not allowed'], 405);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getList(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||||
|
{
|
||||||
|
return $this->createResponse(['content' => 'Method not allowed'], 405);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function create(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||||
|
{
|
||||||
|
return $this->createResponse(['content' => 'Method not allowed'], 405);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function update(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||||
|
{
|
||||||
|
return $this->createResponse(['content' => 'Method not allowed'], 405);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function delete(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||||
|
{
|
||||||
|
return $this->createResponse(['content' => 'Method not allowed'], 405);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function deleteList(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||||
|
{
|
||||||
|
return $this->createResponse(['content' => 'Method not allowed'], 405);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function head(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||||
|
{
|
||||||
|
return $this->createResponse(['content' => 'Method not allowed'], 405);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function options(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||||
|
{
|
||||||
|
return $this->createResponse(['content' => 'Method not allowed'], 405);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function patch(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||||
|
{
|
||||||
|
return $this->createResponse(['content' => 'Method not allowed'], 405);
|
||||||
|
}
|
||||||
|
|
||||||
|
final protected function createResponse($data, $status = 200)
|
||||||
|
{
|
||||||
|
return new JsonResponse($data, $status);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param ServerRequestInterface $request
|
||||||
|
* @return array|object
|
||||||
|
*/
|
||||||
|
public function getRequestData(ServerRequestInterface $request)
|
||||||
|
{
|
||||||
|
$body = $request->getParsedBody();
|
||||||
|
|
||||||
|
if (!empty($body)) {
|
||||||
|
return $body;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->parseRequestData(
|
||||||
|
$request->getBody()->getContents(),
|
||||||
|
$request->getHeaderLine('content-type')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param string $input
|
||||||
|
* @param string $contentType
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
private function parseRequestData($input, $contentType)
|
||||||
|
{
|
||||||
|
$contentTypeParts = preg_split('/\s*[;,]\s*/', $contentType);
|
||||||
|
$parser = $this->returnParserContentType($contentTypeParts[0]);
|
||||||
|
|
||||||
|
return $parser($input);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param string $contentType
|
||||||
|
* @return callable
|
||||||
|
*/
|
||||||
|
private function returnParserContentType(string $contentType)
|
||||||
|
{
|
||||||
|
if ($contentType === 'application/x-www-form-urlencoded') {
|
||||||
|
return function ($input) {
|
||||||
|
parse_str($input, $data);
|
||||||
|
return $data;
|
||||||
|
};
|
||||||
|
} elseif ($contentType === 'application/json') {
|
||||||
|
return function ($input) {
|
||||||
|
$jsonDecoder = new Json();
|
||||||
|
try {
|
||||||
|
return $jsonDecoder->decode($input, Json::TYPE_ARRAY);
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
} elseif ($contentType === 'multipart/form-data') {
|
||||||
|
return function ($input) {
|
||||||
|
return $input;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return function ($input) {
|
||||||
|
return $input;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
45
src/App/Action/CiConfigAction.php
Normal file
45
src/App/Action/CiConfigAction.php
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Action;
|
||||||
|
|
||||||
|
use App\Response\JsonCorsResponse;
|
||||||
|
use App\Service\CiConfigService;
|
||||||
|
use Psr\Http\Message\ResponseInterface;
|
||||||
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
|
|
||||||
|
class CiConfigAction extends AbstractAction
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var CiConfigService
|
||||||
|
*/
|
||||||
|
private $ciConfigService;
|
||||||
|
|
||||||
|
public function __construct(CiConfigService $ciConfigService)
|
||||||
|
{
|
||||||
|
$this->ciConfigService = $ciConfigService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getList(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||||
|
{
|
||||||
|
return new JsonCorsResponse($this->ciConfigService->getList());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||||
|
{
|
||||||
|
$id = $request->getAttribute('id');
|
||||||
|
return new JsonCorsResponse($this->ciConfigService->get($id));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function create(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||||
|
{
|
||||||
|
$data = $this->getRequestData($request);
|
||||||
|
return new JsonCorsResponse($this->ciConfigService->create($data));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function delete(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||||
|
{
|
||||||
|
$id = $request->getAttribute('id');
|
||||||
|
return new JsonCorsResponse($this->ciConfigService->delete($id));
|
||||||
|
}
|
||||||
|
}
|
||||||
15
src/App/Action/CiConfigFactory.php
Normal file
15
src/App/Action/CiConfigFactory.php
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Action;
|
||||||
|
|
||||||
|
use App\Service\CiConfigService;
|
||||||
|
use Interop\Container\ContainerInterface;
|
||||||
|
|
||||||
|
class CiConfigFactory
|
||||||
|
{
|
||||||
|
public function __invoke(ContainerInterface $container)
|
||||||
|
{
|
||||||
|
$service = $container->get(CiConfigService::class);
|
||||||
|
return new CiConfigAction($service);
|
||||||
|
}
|
||||||
|
}
|
||||||
28
src/App/Action/CiConfigItemAction.php
Normal file
28
src/App/Action/CiConfigItemAction.php
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Action;
|
||||||
|
|
||||||
|
use App\Response\JsonCorsResponse;
|
||||||
|
use App\Service\CiConfigService;
|
||||||
|
use Psr\Http\Message\ResponseInterface;
|
||||||
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
|
|
||||||
|
class CiConfigItemAction extends AbstractAction
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var CiConfigService
|
||||||
|
*/
|
||||||
|
private $ciConfigService;
|
||||||
|
|
||||||
|
public function __construct(CiConfigService $ciConfigService)
|
||||||
|
{
|
||||||
|
$this->ciConfigService = $ciConfigService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function delete(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||||
|
{
|
||||||
|
$id = $request->getAttribute('id');
|
||||||
|
return new JsonCorsResponse($this->ciConfigService->deleteItem($id));
|
||||||
|
}
|
||||||
|
}
|
||||||
15
src/App/Action/CiConfigItemFactory.php
Normal file
15
src/App/Action/CiConfigItemFactory.php
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Action;
|
||||||
|
|
||||||
|
use App\Service\CiConfigService;
|
||||||
|
use Interop\Container\ContainerInterface;
|
||||||
|
|
||||||
|
class CiConfigItemFactory
|
||||||
|
{
|
||||||
|
public function __invoke(ContainerInterface $container)
|
||||||
|
{
|
||||||
|
$service = $container->get(CiConfigService::class);
|
||||||
|
return new CiConfigItemAction($service);
|
||||||
|
}
|
||||||
|
}
|
||||||
62
src/App/Action/HomePageAction.php
Normal file
62
src/App/Action/HomePageAction.php
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Action;
|
||||||
|
|
||||||
|
use Psr\Http\Message\ResponseInterface;
|
||||||
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
|
use Zend\Diactoros\Response\HtmlResponse;
|
||||||
|
use Zend\Diactoros\Response\JsonResponse;
|
||||||
|
use Zend\Expressive\Router;
|
||||||
|
use Zend\Expressive\Template;
|
||||||
|
use Zend\Expressive\Plates\PlatesRenderer;
|
||||||
|
use Zend\Expressive\Twig\TwigRenderer;
|
||||||
|
use Zend\Expressive\ZendView\ZendViewRenderer;
|
||||||
|
|
||||||
|
class HomePageAction
|
||||||
|
{
|
||||||
|
private $router;
|
||||||
|
|
||||||
|
private $template;
|
||||||
|
|
||||||
|
public function __construct(Router\RouterInterface $router, Template\TemplateRendererInterface $template = null)
|
||||||
|
{
|
||||||
|
$this->router = $router;
|
||||||
|
$this->template = $template;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||||
|
{
|
||||||
|
$data = [];
|
||||||
|
|
||||||
|
if ($this->router instanceof Router\AuraRouter) {
|
||||||
|
$data['routerName'] = 'Aura.Router';
|
||||||
|
$data['routerDocs'] = 'http://auraphp.com/packages/2.x/Router.html';
|
||||||
|
} elseif ($this->router instanceof Router\FastRouteRouter) {
|
||||||
|
$data['routerName'] = 'FastRoute';
|
||||||
|
$data['routerDocs'] = 'https://github.com/nikic/FastRoute';
|
||||||
|
} elseif ($this->router instanceof Router\ZendRouter) {
|
||||||
|
$data['routerName'] = 'Zend Router';
|
||||||
|
$data['routerDocs'] = 'http://framework.zend.com/manual/current/en/modules/zend.mvc.routing.html';
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->template instanceof PlatesRenderer) {
|
||||||
|
$data['templateName'] = 'Plates';
|
||||||
|
$data['templateDocs'] = 'http://platesphp.com/';
|
||||||
|
} elseif ($this->template instanceof TwigRenderer) {
|
||||||
|
$data['templateName'] = 'Twig';
|
||||||
|
$data['templateDocs'] = 'http://twig.sensiolabs.org/documentation';
|
||||||
|
} elseif ($this->template instanceof ZendViewRenderer) {
|
||||||
|
$data['templateName'] = 'Zend View';
|
||||||
|
$data['templateDocs'] = 'http://framework.zend.com/manual/current/en/modules/zend.view.quick-start.html';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$this->template) {
|
||||||
|
return new JsonResponse([
|
||||||
|
'welcome' => 'Congratulations! You have installed the zend-expressive skeleton application.',
|
||||||
|
'docsUrl' => 'zend-expressive.readthedocs.org',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new HtmlResponse($this->template->render('app::home-page', $data));
|
||||||
|
}
|
||||||
|
}
|
||||||
20
src/App/Action/HomePageFactory.php
Normal file
20
src/App/Action/HomePageFactory.php
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Action;
|
||||||
|
|
||||||
|
use Interop\Container\ContainerInterface;
|
||||||
|
use Zend\Expressive\Router\RouterInterface;
|
||||||
|
use Zend\Expressive\Template\TemplateRendererInterface;
|
||||||
|
|
||||||
|
class HomePageFactory
|
||||||
|
{
|
||||||
|
public function __invoke(ContainerInterface $container)
|
||||||
|
{
|
||||||
|
$router = $container->get(RouterInterface::class);
|
||||||
|
$template = ($container->has(TemplateRendererInterface::class))
|
||||||
|
? $container->get(TemplateRendererInterface::class)
|
||||||
|
: null;
|
||||||
|
|
||||||
|
return new HomePageAction($router, $template);
|
||||||
|
}
|
||||||
|
}
|
||||||
15
src/App/Action/PingAction.php
Normal file
15
src/App/Action/PingAction.php
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Action;
|
||||||
|
|
||||||
|
use Zend\Diactoros\Response\JsonResponse;
|
||||||
|
use Psr\Http\Message\ResponseInterface;
|
||||||
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
|
|
||||||
|
class PingAction
|
||||||
|
{
|
||||||
|
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||||
|
{
|
||||||
|
return new JsonResponse(['ack' => time()]);
|
||||||
|
}
|
||||||
|
}
|
||||||
130
src/App/Entity/CiConfig.php
Normal file
130
src/App/Entity/CiConfig.php
Normal file
@ -0,0 +1,130 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Entity;
|
||||||
|
|
||||||
|
use Doctrine\Common\Collections\ArrayCollection;
|
||||||
|
use Doctrine\ORM\Mapping as ORM;
|
||||||
|
use JsonSerializable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ORM\Entity
|
||||||
|
* @ORM\Table(name="ci_config")
|
||||||
|
*/
|
||||||
|
class CiConfig implements JsonSerializable
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @ORM\Id
|
||||||
|
* @ORM\Column(name="id", type="integer")
|
||||||
|
* @var int
|
||||||
|
*/
|
||||||
|
private $id;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ORM\OneToMany(targetEntity="CiConfigItem", mappedBy="dailyConfig", cascade={"persist", "remove"}, orphanRemoval=true)
|
||||||
|
*/
|
||||||
|
private $configItems;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* CiConfig constructor.
|
||||||
|
*/
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
$this->configItems = new ArrayCollection();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function getId(): int
|
||||||
|
{
|
||||||
|
return $this->id;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $id
|
||||||
|
* @return CiConfig
|
||||||
|
*/
|
||||||
|
public function setId(int $id): CiConfig
|
||||||
|
{
|
||||||
|
$this->id = $id;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function getConfigItems()
|
||||||
|
{
|
||||||
|
return $this->configItems;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $configItems
|
||||||
|
* @return CiConfig
|
||||||
|
*/
|
||||||
|
public function setConfigItems($configItems): CiConfig
|
||||||
|
{
|
||||||
|
$this->configItems = $configItems;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param CiConfigItem $item
|
||||||
|
* @return CiConfig
|
||||||
|
*/
|
||||||
|
public function addConfigItem(CiConfigItem $item): CiConfig
|
||||||
|
{
|
||||||
|
if(!$this->configItems->contains($item)) {
|
||||||
|
$this->configItems->add($item);
|
||||||
|
$item->setDailyConfig($this);
|
||||||
|
}
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param ArrayCollection $items
|
||||||
|
* @return CiConfig
|
||||||
|
*/
|
||||||
|
public function addConfigItems($items): CiConfig
|
||||||
|
{
|
||||||
|
foreach ($items as $item) {
|
||||||
|
$this->addConfigItem($item);
|
||||||
|
}
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param CiConfigItem $item
|
||||||
|
* @return CiConfig
|
||||||
|
*/
|
||||||
|
public function removeConfigItem(CiConfigItem $item): CiConfig
|
||||||
|
{
|
||||||
|
if($this->configItems->contains($item)) {
|
||||||
|
$this->configItems->removeElement($item);
|
||||||
|
}
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param ArrayCollection $items
|
||||||
|
* @return CiConfig
|
||||||
|
*/
|
||||||
|
public function removeConfigItems($items): CiConfig
|
||||||
|
{
|
||||||
|
foreach ($items as $item) {
|
||||||
|
$this->removeConfigItem($item);
|
||||||
|
}
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function jsonSerialize()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'id' => $this->getId(),
|
||||||
|
'configItems' => $this->getConfigItems()->getValues(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
529
src/App/Entity/CiConfigItem.php
Normal file
529
src/App/Entity/CiConfigItem.php
Normal file
@ -0,0 +1,529 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Entity;
|
||||||
|
|
||||||
|
use Doctrine\ORM\Mapping as ORM;
|
||||||
|
use JsonSerializable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ORM\Entity
|
||||||
|
* @ORM\Table(
|
||||||
|
* name="ci_config_item",
|
||||||
|
* indexes={
|
||||||
|
* @ORM\Index(name="parent_idx", columns={"ci_config_id"}),
|
||||||
|
* @ORM\Index(name="stream_idx", columns={"stream"}),
|
||||||
|
* @ORM\Index(name="test_type_idx", columns={"test_type"}),
|
||||||
|
* @ORM\Index(name="nightly_idx", columns={"is_nightly"})
|
||||||
|
* }
|
||||||
|
* )
|
||||||
|
*/
|
||||||
|
class CiConfigItem implements JsonSerializable
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @ORM\Id
|
||||||
|
* @ORM\Column(name="id", type="integer")
|
||||||
|
* @ORM\GeneratedValue(strategy="AUTO")
|
||||||
|
* @var int
|
||||||
|
*/
|
||||||
|
private $id;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ORM\ManyToOne(targetEntity="CiConfig", inversedBy="configItems")
|
||||||
|
* @ORM\JoinColumn(name="ci_config_id", referencedColumnName="id", nullable=false)
|
||||||
|
*/
|
||||||
|
private $dailyConfig;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ORM\Column(name="stream", type="string", length=200)
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private $stream;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ORM\Column(name="nodes", type="array", length=65535)
|
||||||
|
* @var string[]
|
||||||
|
*/
|
||||||
|
private $nodes = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ORM\Column(name="jcat", type="string", length=100)
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private $jcat;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ORM\Column(name="test_type", type="string", length=50)
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private $testType;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ORM\Column(name="do_rollback_before", type="boolean")
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
private $doRollbackBefore;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ORM\Column(name="do_upgrade", type="boolean")
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
private $doUpgrade;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ORM\Column(name="do_lrt", type="boolean")
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
private $doLrt;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ORM\Column(name="do_dsv", type="boolean")
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
private $doDsv;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ORM\Column(name="do_nmdsv", type="boolean")
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
private $doNmDsv;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ORM\Column(name="do_rollback_after", type="boolean")
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
private $doRollbackAfter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ORM\Column(name="do_ssreg_loop", type="boolean")
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
private $doSubsystemRegressionLoop;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ORM\Column(name="do_upgr_loop", type="boolean")
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
private $doUpgradeabilityLoop;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ORM\Column(name="do_cc_char_loop", type="boolean")
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
private $doClusterControlCharacteristicsLoop;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ORM\Column(name="tests", type="array", length=65535)
|
||||||
|
* @var string[]
|
||||||
|
*/
|
||||||
|
private $tests = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ORM\Column(name="is_nightly", type="boolean")
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
private $nightly = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ORM\Column(name="note", type="text", length=65535)
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private $note;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ORM\Column(name="ran", type="boolean")
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
private $ran = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function getId()
|
||||||
|
{
|
||||||
|
return $this->id;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $id
|
||||||
|
* @return CiConfigItem
|
||||||
|
*/
|
||||||
|
public function setId($id): CiConfigItem
|
||||||
|
{
|
||||||
|
$this->id = $id;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return CiConfig
|
||||||
|
*/
|
||||||
|
public function getDailyConfig(): CiConfig
|
||||||
|
{
|
||||||
|
return $this->dailyConfig;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param CiConfig $dailyConfig
|
||||||
|
* @return CiConfigItem
|
||||||
|
*/
|
||||||
|
public function setDailyConfig(CiConfig $dailyConfig): CiConfigItem
|
||||||
|
{
|
||||||
|
$this->dailyConfig = $dailyConfig;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function getStream()
|
||||||
|
{
|
||||||
|
return $this->stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $stream
|
||||||
|
* @return CiConfigItem
|
||||||
|
*/
|
||||||
|
public function setStream($stream): CiConfigItem
|
||||||
|
{
|
||||||
|
$this->stream = $stream;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return \string[]
|
||||||
|
*/
|
||||||
|
public function getNodes(): array
|
||||||
|
{
|
||||||
|
return $this->nodes;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param \string[] $nodes
|
||||||
|
* @return CiConfigItem
|
||||||
|
*/
|
||||||
|
public function setNodes(array $nodes): CiConfigItem
|
||||||
|
{
|
||||||
|
$this->nodes = $nodes;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $node
|
||||||
|
* @return CiConfigItem
|
||||||
|
*/
|
||||||
|
public function addNode(string $node): CiConfigItem
|
||||||
|
{
|
||||||
|
array_push($this->nodes, $node);
|
||||||
|
$this->nodes = array_unique($this->nodes);
|
||||||
|
sort($this->nodes);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $node
|
||||||
|
* @return CiConfigItem
|
||||||
|
*/
|
||||||
|
public function removeNode(string $node): CiConfigItem
|
||||||
|
{
|
||||||
|
$this->nodes = array_filter($this->nodes, function($item) use ($node) {
|
||||||
|
return $item != $node;
|
||||||
|
});
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getJcat(): string
|
||||||
|
{
|
||||||
|
return $this->jcat;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $jcat
|
||||||
|
* @return CiConfigItem
|
||||||
|
*/
|
||||||
|
public function setJcat(string $jcat): CiConfigItem
|
||||||
|
{
|
||||||
|
$this->jcat = $jcat;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getTestType(): string
|
||||||
|
{
|
||||||
|
return $this->testType;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $testType
|
||||||
|
* @return CiConfigItem
|
||||||
|
*/
|
||||||
|
public function setTestType(string $testType): CiConfigItem
|
||||||
|
{
|
||||||
|
$this->testType = $testType;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function isDoRollbackBefore(): bool
|
||||||
|
{
|
||||||
|
return $this->doRollbackBefore;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param bool $doRollbackBefore
|
||||||
|
* @return CiConfigItem
|
||||||
|
*/
|
||||||
|
public function setDoRollbackBefore(bool $doRollbackBefore): CiConfigItem
|
||||||
|
{
|
||||||
|
$this->doRollbackBefore = $doRollbackBefore;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function isDoUpgrade(): bool
|
||||||
|
{
|
||||||
|
return $this->doUpgrade;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param bool $doUpgrade
|
||||||
|
* @return CiConfigItem
|
||||||
|
*/
|
||||||
|
public function setDoUpgrade(bool $doUpgrade): CiConfigItem
|
||||||
|
{
|
||||||
|
$this->doUpgrade = $doUpgrade;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function isDoLrt(): bool
|
||||||
|
{
|
||||||
|
return $this->doLrt;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param bool $doLrt
|
||||||
|
* @return CiConfigItem
|
||||||
|
*/
|
||||||
|
public function setDoLrt(bool $doLrt): CiConfigItem
|
||||||
|
{
|
||||||
|
$this->doLrt = $doLrt;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function isDoDsv(): bool
|
||||||
|
{
|
||||||
|
return $this->doDsv;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param bool $doDsv
|
||||||
|
* @return CiConfigItem
|
||||||
|
*/
|
||||||
|
public function setDoDsv(bool $doDsv): CiConfigItem
|
||||||
|
{
|
||||||
|
$this->doDsv = $doDsv;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function isDoNmDsv(): bool
|
||||||
|
{
|
||||||
|
return $this->doNmDsv;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param bool $doNmDsv
|
||||||
|
* @return CiConfigItem
|
||||||
|
*/
|
||||||
|
public function setDoNmDsv(bool $doNmDsv): CiConfigItem
|
||||||
|
{
|
||||||
|
$this->doNmDsv = $doNmDsv;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function isDoRollbackAfter(): bool
|
||||||
|
{
|
||||||
|
return $this->doRollbackAfter;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param bool $doRollbackAfter
|
||||||
|
* @return CiConfigItem
|
||||||
|
*/
|
||||||
|
public function setDoRollbackAfter(bool $doRollbackAfter): CiConfigItem
|
||||||
|
{
|
||||||
|
$this->doRollbackAfter = $doRollbackAfter;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function isDoSubsystemRegressionLoop(): bool
|
||||||
|
{
|
||||||
|
return $this->doSubsystemRegressionLoop;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param bool $doSubsystemRegressionLoop
|
||||||
|
* @return CiConfigItem
|
||||||
|
*/
|
||||||
|
public function setDoSubsystemRegressionLoop(bool $doSubsystemRegressionLoop): CiConfigItem
|
||||||
|
{
|
||||||
|
$this->doSubsystemRegressionLoop = $doSubsystemRegressionLoop;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function isDoUpgradeabilityLoop(): bool
|
||||||
|
{
|
||||||
|
return $this->doUpgradeabilityLoop;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param bool $doUpgradeabilityLoop
|
||||||
|
* @return CiConfigItem
|
||||||
|
*/
|
||||||
|
public function setDoUpgradeabilityLoop(bool $doUpgradeabilityLoop): CiConfigItem
|
||||||
|
{
|
||||||
|
$this->doUpgradeabilityLoop = $doUpgradeabilityLoop;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function isDoClusterControlCharacteristicsLoop(): bool
|
||||||
|
{
|
||||||
|
return $this->doClusterControlCharacteristicsLoop;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param bool $doClusterControlCharacteristicsLoop
|
||||||
|
* @return CiConfigItem
|
||||||
|
*/
|
||||||
|
public function setDoClusterControlCharacteristicsLoop(bool $doClusterControlCharacteristicsLoop): CiConfigItem
|
||||||
|
{
|
||||||
|
$this->doClusterControlCharacteristicsLoop = $doClusterControlCharacteristicsLoop;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return \string[]
|
||||||
|
*/
|
||||||
|
public function getTests(): array
|
||||||
|
{
|
||||||
|
return $this->tests;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param \string[] $tests
|
||||||
|
* @return CiConfigItem
|
||||||
|
*/
|
||||||
|
public function setTests(array $tests): CiConfigItem
|
||||||
|
{
|
||||||
|
$this->tests = $tests;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function isNightly(): bool
|
||||||
|
{
|
||||||
|
return $this->nightly;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param bool $nightly
|
||||||
|
* @return CiConfigItem
|
||||||
|
*/
|
||||||
|
public function setNightly(bool $nightly): CiConfigItem
|
||||||
|
{
|
||||||
|
$this->nightly = $nightly;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getNote(): string
|
||||||
|
{
|
||||||
|
return $this->note;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $note
|
||||||
|
* @return CiConfigItem
|
||||||
|
*/
|
||||||
|
public function setNote(string $note): CiConfigItem
|
||||||
|
{
|
||||||
|
$this->note = $note;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function isRan(): bool
|
||||||
|
{
|
||||||
|
return $this->ran;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param bool $ran
|
||||||
|
* @return CiConfigItem
|
||||||
|
*/
|
||||||
|
public function setRan(bool $ran): CiConfigItem
|
||||||
|
{
|
||||||
|
$this->ran = $ran;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function jsonSerialize()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'id' => $this->getId(),
|
||||||
|
'stream' => $this->getStream(),
|
||||||
|
'nodes' => $this->getNodes(),
|
||||||
|
'jcat' => $this->getJcat(),
|
||||||
|
'testType' => $this->getTestType(),
|
||||||
|
'doRollbackBefore' => $this->isDoRollbackBefore(),
|
||||||
|
'doUpgrade' => $this->isDoUpgrade(),
|
||||||
|
'doLrt' => $this->isDoLrt(),
|
||||||
|
'doDsv' => $this->isDoDsv(),
|
||||||
|
'doNmDsv' => $this->isDoNmDsv(),
|
||||||
|
'doRollbackAfter' => $this->isDoRollbackAfter(),
|
||||||
|
'doSubsystemRegressionLoop' => $this->isDoSubsystemRegressionLoop(),
|
||||||
|
'doUpgradeabilityLoop' => $this->isDoUpgradeabilityLoop(),
|
||||||
|
'doClusterControlCharacteristicsLoop' => $this->isDoClusterControlCharacteristicsLoop(),
|
||||||
|
'nightly' => $this->isNightly(),
|
||||||
|
'note' => $this->getNote(),
|
||||||
|
'ran' => $this->isRan(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
594
src/App/Hydrator/DoctrineObject.php
Normal file
594
src/App/Hydrator/DoctrineObject.php
Normal file
@ -0,0 +1,594 @@
|
|||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||||
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||||
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||||
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||||
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||||
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*
|
||||||
|
* This software consists of voluntary contributions made by many individuals
|
||||||
|
* and is licensed under the MIT license. For more information, see
|
||||||
|
* <http://www.doctrine-project.org>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace App\Hydrator;
|
||||||
|
|
||||||
|
use DateTime;
|
||||||
|
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
|
||||||
|
use Doctrine\Common\Persistence\ObjectManager;
|
||||||
|
use Doctrine\Common\Util\Inflector;
|
||||||
|
use InvalidArgumentException;
|
||||||
|
use RuntimeException;
|
||||||
|
use Traversable;
|
||||||
|
use Zend\Stdlib\ArrayUtils;
|
||||||
|
use Zend\Hydrator\AbstractHydrator;
|
||||||
|
use Zend\Hydrator\Filter\FilterProviderInterface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This hydrator has been completely refactored for DoctrineModule 0.7.0. It provides an easy and powerful way
|
||||||
|
* of extracting/hydrator objects in Doctrine, by handling most associations types.
|
||||||
|
*
|
||||||
|
* Starting from DoctrineModule 0.8.0, the hydrator can be used multiple times with different objects
|
||||||
|
*
|
||||||
|
* @license MIT
|
||||||
|
* @link http://www.doctrine-project.org/
|
||||||
|
* @since 0.7.0
|
||||||
|
* @author Michael Gallego <mic.gallego@gmail.com>
|
||||||
|
*/
|
||||||
|
class DoctrineObject extends AbstractHydrator
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var ObjectManager
|
||||||
|
*/
|
||||||
|
protected $objectManager;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var ClassMetadata
|
||||||
|
*/
|
||||||
|
protected $metadata;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
protected $byValue = true;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor
|
||||||
|
*
|
||||||
|
* @param ObjectManager $objectManager The ObjectManager to use
|
||||||
|
* @param bool $byValue If set to true, hydrator will always use entity's public API
|
||||||
|
*/
|
||||||
|
public function __construct(ObjectManager $objectManager, $byValue = true)
|
||||||
|
{
|
||||||
|
parent::__construct();
|
||||||
|
|
||||||
|
$this->objectManager = $objectManager;
|
||||||
|
$this->byValue = (bool) $byValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extract values from an object
|
||||||
|
*
|
||||||
|
* @param object $object
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function extract($object)
|
||||||
|
{
|
||||||
|
$this->prepare($object);
|
||||||
|
|
||||||
|
if ($this->byValue) {
|
||||||
|
return $this->extractByValue($object);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->extractByReference($object);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Hydrate $object with the provided $data.
|
||||||
|
*
|
||||||
|
* @param array $data
|
||||||
|
* @param object $object
|
||||||
|
* @return object
|
||||||
|
*/
|
||||||
|
public function hydrate(array $data, $object)
|
||||||
|
{
|
||||||
|
$this->prepare($object);
|
||||||
|
|
||||||
|
if ($this->byValue) {
|
||||||
|
return $this->hydrateByValue($data, $object);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->hydrateByReference($data, $object);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prepare the hydrator by adding strategies to every collection valued associations
|
||||||
|
*
|
||||||
|
* @param object $object
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
protected function prepare($object)
|
||||||
|
{
|
||||||
|
$this->metadata = $this->objectManager->getClassMetadata(get_class($object));
|
||||||
|
$this->prepareStrategies();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prepare strategies before the hydrator is used
|
||||||
|
*
|
||||||
|
* @throws \InvalidArgumentException
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
protected function prepareStrategies()
|
||||||
|
{
|
||||||
|
$associations = $this->metadata->getAssociationNames();
|
||||||
|
|
||||||
|
foreach ($associations as $association) {
|
||||||
|
if ($this->metadata->isCollectionValuedAssociation($association)) {
|
||||||
|
// Add a strategy if the association has none set by user
|
||||||
|
if (!$this->hasStrategy($association)) {
|
||||||
|
if ($this->byValue) {
|
||||||
|
$this->addStrategy($association, new Strategy\AllowRemoveByValue());
|
||||||
|
} else {
|
||||||
|
$this->addStrategy($association, new Strategy\AllowRemoveByReference());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$strategy = $this->getStrategy($association);
|
||||||
|
|
||||||
|
if (!$strategy instanceof Strategy\AbstractCollectionStrategy) {
|
||||||
|
throw new InvalidArgumentException(
|
||||||
|
sprintf(
|
||||||
|
'Strategies used for collections valued associations must inherit from '
|
||||||
|
. 'Strategy\AbstractCollectionStrategy, %s given',
|
||||||
|
get_class($strategy)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
$strategy->setCollectionName($association)
|
||||||
|
->setClassMetadata($this->metadata);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extract values from an object using a by-value logic (this means that it uses the entity
|
||||||
|
* API, in this case, getters)
|
||||||
|
*
|
||||||
|
* @param object $object
|
||||||
|
* @throws RuntimeException
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function extractByValue($object)
|
||||||
|
{
|
||||||
|
$fieldNames = array_merge($this->metadata->getFieldNames(), $this->metadata->getAssociationNames());
|
||||||
|
$methods = get_class_methods($object);
|
||||||
|
$filter = $object instanceof FilterProviderInterface
|
||||||
|
? $object->getFilter()
|
||||||
|
: $this->filterComposite;
|
||||||
|
|
||||||
|
$data = [];
|
||||||
|
foreach ($fieldNames as $fieldName) {
|
||||||
|
if ($filter && !$filter->filter($fieldName)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$getter = 'get' . Inflector::classify($fieldName);
|
||||||
|
$isser = 'is' . Inflector::classify($fieldName);
|
||||||
|
|
||||||
|
$dataFieldName = $this->computeExtractFieldName($fieldName);
|
||||||
|
if (in_array($getter, $methods)) {
|
||||||
|
$data[$dataFieldName] = $this->extractValue($fieldName, $object->$getter(), $object);
|
||||||
|
} elseif (in_array($isser, $methods)) {
|
||||||
|
$data[$dataFieldName] = $this->extractValue($fieldName, $object->$isser(), $object);
|
||||||
|
} elseif (substr($fieldName, 0, 2) === 'is'
|
||||||
|
&& ctype_upper(substr($fieldName, 2, 1))
|
||||||
|
&& in_array($fieldName, $methods)) {
|
||||||
|
$data[$dataFieldName] = $this->extractValue($fieldName, $object->$fieldName(), $object);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unknown fields are ignored
|
||||||
|
}
|
||||||
|
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extract values from an object using a by-reference logic (this means that values are
|
||||||
|
* directly fetched without using the public API of the entity, in this case, getters)
|
||||||
|
*
|
||||||
|
* @param object $object
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function extractByReference($object)
|
||||||
|
{
|
||||||
|
$fieldNames = array_merge($this->metadata->getFieldNames(), $this->metadata->getAssociationNames());
|
||||||
|
$refl = $this->metadata->getReflectionClass();
|
||||||
|
$filter = $object instanceof FilterProviderInterface
|
||||||
|
? $object->getFilter()
|
||||||
|
: $this->filterComposite;
|
||||||
|
|
||||||
|
$data = [];
|
||||||
|
foreach ($fieldNames as $fieldName) {
|
||||||
|
if ($filter && !$filter->filter($fieldName)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$reflProperty = $refl->getProperty($fieldName);
|
||||||
|
$reflProperty->setAccessible(true);
|
||||||
|
|
||||||
|
$dataFieldName = $this->computeExtractFieldName($fieldName);
|
||||||
|
$data[$dataFieldName] = $this->extractValue($fieldName, $reflProperty->getValue($object), $object);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Hydrate the object using a by-value logic (this means that it uses the entity API, in this
|
||||||
|
* case, setters)
|
||||||
|
*
|
||||||
|
* @param array $data
|
||||||
|
* @param object $object
|
||||||
|
* @throws RuntimeException
|
||||||
|
* @return object
|
||||||
|
*/
|
||||||
|
protected function hydrateByValue(array $data, $object)
|
||||||
|
{
|
||||||
|
$tryObject = $this->tryConvertArrayToObject($data, $object);
|
||||||
|
$metadata = $this->metadata;
|
||||||
|
|
||||||
|
if (is_object($tryObject)) {
|
||||||
|
$object = $tryObject;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($data as $field => $value) {
|
||||||
|
$field = $this->computeHydrateFieldName($field);
|
||||||
|
$value = $this->handleTypeConversions($value, $metadata->getTypeOfField($field));
|
||||||
|
$setter = 'set' . Inflector::classify($field);
|
||||||
|
|
||||||
|
if ($metadata->hasAssociation($field)) {
|
||||||
|
$target = $metadata->getAssociationTargetClass($field);
|
||||||
|
|
||||||
|
if ($metadata->isSingleValuedAssociation($field)) {
|
||||||
|
if (! method_exists($object, $setter)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$value = $this->toOne($target, $this->hydrateValue($field, $value, $data));
|
||||||
|
|
||||||
|
if (null === $value
|
||||||
|
&& !current($metadata->getReflectionClass()->getMethod($setter)->getParameters())->allowsNull()
|
||||||
|
) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$object->$setter($value);
|
||||||
|
} elseif ($metadata->isCollectionValuedAssociation($field)) {
|
||||||
|
$this->toMany($object, $field, $target, $value);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (! method_exists($object, $setter)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$object->$setter($this->hydrateValue($field, $value, $data));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $object;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Hydrate the object using a by-reference logic (this means that values are modified directly without
|
||||||
|
* using the public API, in this case setters, and hence override any logic that could be done in those
|
||||||
|
* setters)
|
||||||
|
*
|
||||||
|
* @param array $data
|
||||||
|
* @param object $object
|
||||||
|
* @return object
|
||||||
|
*/
|
||||||
|
protected function hydrateByReference(array $data, $object)
|
||||||
|
{
|
||||||
|
$tryObject = $this->tryConvertArrayToObject($data, $object);
|
||||||
|
$metadata = $this->metadata;
|
||||||
|
$refl = $metadata->getReflectionClass();
|
||||||
|
|
||||||
|
if (is_object($tryObject)) {
|
||||||
|
$object = $tryObject;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($data as $field => $value) {
|
||||||
|
$field = $this->computeHydrateFieldName($field);
|
||||||
|
|
||||||
|
// Ignore unknown fields
|
||||||
|
if (!$refl->hasProperty($field)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$value = $this->handleTypeConversions($value, $metadata->getTypeOfField($field));
|
||||||
|
$reflProperty = $refl->getProperty($field);
|
||||||
|
$reflProperty->setAccessible(true);
|
||||||
|
|
||||||
|
if ($metadata->hasAssociation($field)) {
|
||||||
|
$target = $metadata->getAssociationTargetClass($field);
|
||||||
|
|
||||||
|
if ($metadata->isSingleValuedAssociation($field)) {
|
||||||
|
$value = $this->toOne($target, $this->hydrateValue($field, $value, $data));
|
||||||
|
$reflProperty->setValue($object, $value);
|
||||||
|
} elseif ($metadata->isCollectionValuedAssociation($field)) {
|
||||||
|
$this->toMany($object, $field, $target, $value);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$reflProperty->setValue($object, $this->hydrateValue($field, $value, $data));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $object;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This function tries, given an array of data, to convert it to an object if the given array contains
|
||||||
|
* an identifier for the object. This is useful in a context of updating existing entities, without ugly
|
||||||
|
* tricks like setting manually the existing id directly into the entity
|
||||||
|
*
|
||||||
|
* @param array $data The data that may contain identifiers keys
|
||||||
|
* @param object $object
|
||||||
|
* @return object
|
||||||
|
*/
|
||||||
|
protected function tryConvertArrayToObject($data, $object)
|
||||||
|
{
|
||||||
|
$metadata = $this->metadata;
|
||||||
|
$identifierNames = $metadata->getIdentifierFieldNames($object);
|
||||||
|
$identifierValues = [];
|
||||||
|
|
||||||
|
if (empty($identifierNames)) {
|
||||||
|
return $object;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($identifierNames as $identifierName) {
|
||||||
|
if (!isset($data[$identifierName])) {
|
||||||
|
return $object;
|
||||||
|
}
|
||||||
|
|
||||||
|
$identifierValues[$identifierName] = $data[$identifierName];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->find($identifierValues, $metadata->getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle ToOne associations
|
||||||
|
*
|
||||||
|
* When $value is an array but is not the $target's identifiers, $value is
|
||||||
|
* most likely an array of fieldset data. The identifiers will be determined
|
||||||
|
* and a target instance will be initialized and then hydrated. The hydrated
|
||||||
|
* target will be returned.
|
||||||
|
*
|
||||||
|
* @param string $target
|
||||||
|
* @param mixed $value
|
||||||
|
* @return object
|
||||||
|
*/
|
||||||
|
protected function toOne($target, $value)
|
||||||
|
{
|
||||||
|
$metadata = $this->objectManager->getClassMetadata($target);
|
||||||
|
|
||||||
|
if (is_array($value) && array_keys($value) != $metadata->getIdentifier()) {
|
||||||
|
// $value is most likely an array of fieldset data
|
||||||
|
$identifiers = array_intersect_key(
|
||||||
|
$value,
|
||||||
|
array_flip($metadata->getIdentifier())
|
||||||
|
);
|
||||||
|
$object = $this->find($identifiers, $target) ?: new $target;
|
||||||
|
return $this->hydrate($value, $object);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->find($value, $target);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle ToMany associations. In proper Doctrine design, Collections should not be swapped, so
|
||||||
|
* collections are always handled by reference. Internally, every collection is handled using specials
|
||||||
|
* strategies that inherit from AbstractCollectionStrategy class, and that add or remove elements but without
|
||||||
|
* changing the collection of the object
|
||||||
|
*
|
||||||
|
* @param object $object
|
||||||
|
* @param mixed $collectionName
|
||||||
|
* @param string $target
|
||||||
|
* @param mixed $values
|
||||||
|
*
|
||||||
|
* @throws \InvalidArgumentException
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
protected function toMany($object, $collectionName, $target, $values)
|
||||||
|
{
|
||||||
|
$metadata = $this->objectManager->getClassMetadata(ltrim($target, '\\'));
|
||||||
|
$identifier = $metadata->getIdentifier();
|
||||||
|
|
||||||
|
if (!is_array($values) && !$values instanceof Traversable) {
|
||||||
|
$values = (array)$values;
|
||||||
|
}
|
||||||
|
|
||||||
|
$collection = [];
|
||||||
|
|
||||||
|
// If the collection contains identifiers, fetch the objects from database
|
||||||
|
foreach ($values as $value) {
|
||||||
|
if ($value instanceof $target) {
|
||||||
|
// assumes modifications have already taken place in object
|
||||||
|
$collection[] = $value;
|
||||||
|
continue;
|
||||||
|
} elseif (empty($value)) {
|
||||||
|
// assumes no id and retrieves new $target
|
||||||
|
$collection[] = $this->find($value, $target);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$find = [];
|
||||||
|
if (is_array($identifier)) {
|
||||||
|
foreach ($identifier as $field) {
|
||||||
|
switch (gettype($value)) {
|
||||||
|
case 'object':
|
||||||
|
$getter = 'get' . ucfirst($field);
|
||||||
|
if (method_exists($value, $getter)) {
|
||||||
|
$find[$field] = $value->$getter();
|
||||||
|
} elseif (property_exists($value, $field)) {
|
||||||
|
$find[$field] = $value->$field;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'array':
|
||||||
|
if (array_key_exists($field, $value) && $value[$field] != null) {
|
||||||
|
$find[$field] = $value[$field];
|
||||||
|
unset($value[$field]); // removed identifier from persistable data
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
$find[$field] = $value;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($find) && $found = $this->find($find, $target)) {
|
||||||
|
$collection[] = (is_array($value)) ? $this->hydrate($value, $found) : $found;
|
||||||
|
} else {
|
||||||
|
$collection[] = (is_array($value)) ? $this->hydrate($value, new $target) : new $target;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$collection = array_filter(
|
||||||
|
$collection,
|
||||||
|
function ($item) {
|
||||||
|
return null !== $item;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
// Set the object so that the strategy can extract the Collection from it
|
||||||
|
|
||||||
|
/** @var \DoctrineModule\Stdlib\Hydrator\Strategy\AbstractCollectionStrategy $collectionStrategy */
|
||||||
|
$collectionStrategy = $this->getStrategy($collectionName);
|
||||||
|
$collectionStrategy->setObject($object);
|
||||||
|
|
||||||
|
// We could directly call hydrate method from the strategy, but if people want to override
|
||||||
|
// hydrateValue function, they can do it and do their own stuff
|
||||||
|
$this->hydrateValue($collectionName, $collection, $values);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle various type conversions that should be supported natively by Doctrine (like DateTime)
|
||||||
|
*
|
||||||
|
* @param mixed $value
|
||||||
|
* @param string $typeOfField
|
||||||
|
* @return DateTime
|
||||||
|
*/
|
||||||
|
protected function handleTypeConversions($value, $typeOfField)
|
||||||
|
{
|
||||||
|
switch ($typeOfField) {
|
||||||
|
case 'datetimetz':
|
||||||
|
case 'datetime':
|
||||||
|
case 'time':
|
||||||
|
case 'date':
|
||||||
|
if ('' === $value) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_int($value)) {
|
||||||
|
$dateTime = new DateTime();
|
||||||
|
$dateTime->setTimestamp($value);
|
||||||
|
$value = $dateTime;
|
||||||
|
} elseif (is_string($value)) {
|
||||||
|
$value = new DateTime($value);
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find an object by a given target class and identifier
|
||||||
|
*
|
||||||
|
* @param mixed $identifiers
|
||||||
|
* @param string $targetClass
|
||||||
|
*
|
||||||
|
* @return object|null
|
||||||
|
*/
|
||||||
|
protected function find($identifiers, $targetClass)
|
||||||
|
{
|
||||||
|
if ($identifiers instanceof $targetClass) {
|
||||||
|
return $identifiers;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->isNullIdentifier($identifiers)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->objectManager->find($targetClass, $identifiers);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Verifies if a provided identifier is to be considered null
|
||||||
|
*
|
||||||
|
* @param mixed $identifier
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
private function isNullIdentifier($identifier)
|
||||||
|
{
|
||||||
|
if (null === $identifier) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($identifier instanceof Traversable || is_array($identifier)) {
|
||||||
|
$nonNullIdentifiers = array_filter(
|
||||||
|
ArrayUtils::iteratorToArray($identifier),
|
||||||
|
function ($value) {
|
||||||
|
return null !== $value;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
return empty($nonNullIdentifiers);
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Applies the naming strategy if there is one set
|
||||||
|
*
|
||||||
|
* @param string $field
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected function computeHydrateFieldName($field)
|
||||||
|
{
|
||||||
|
if ($this->hasNamingStrategy()) {
|
||||||
|
$field = $this->getNamingStrategy()->hydrate($field);
|
||||||
|
}
|
||||||
|
return $field;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Applies the naming strategy if there is one set
|
||||||
|
*
|
||||||
|
* @param string $field
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected function computeExtractFieldName($field)
|
||||||
|
{
|
||||||
|
if ($this->hasNamingStrategy()) {
|
||||||
|
$field = $this->getNamingStrategy()->extract($field);
|
||||||
|
}
|
||||||
|
return $field;
|
||||||
|
}
|
||||||
|
}
|
||||||
15
src/App/Hydrator/DoctrineObjectFactory.php
Normal file
15
src/App/Hydrator/DoctrineObjectFactory.php
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Hydrator;
|
||||||
|
|
||||||
|
use Interop\Container\ContainerInterface;
|
||||||
|
|
||||||
|
class DoctrineObjectFactory
|
||||||
|
{
|
||||||
|
|
||||||
|
public function __invoke(ContainerInterface $container)
|
||||||
|
{
|
||||||
|
$em = $container->get('doctrine.entity_manager.orm_default');
|
||||||
|
return new DoctrineObject($em);
|
||||||
|
}
|
||||||
|
}
|
||||||
66
src/App/Hydrator/Filter/PropertyName.php
Normal file
66
src/App/Hydrator/Filter/PropertyName.php
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||||
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||||
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||||
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||||
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||||
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*
|
||||||
|
* This software consists of voluntary contributions made by many individuals
|
||||||
|
* and is licensed under the MIT license. For more information, see
|
||||||
|
* <http://www.doctrine-project.org>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace App\Hydrator\Filter;
|
||||||
|
|
||||||
|
use Zend\Hydrator\Filter\FilterInterface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provides a filter to restrict returned fields by whitelisting or
|
||||||
|
* blacklisting property names.
|
||||||
|
*
|
||||||
|
* @license MIT
|
||||||
|
* @link http://www.doctrine-project.org/
|
||||||
|
* @author Liam O'Boyle <liam@ontheroad.net.nz>
|
||||||
|
*/
|
||||||
|
class PropertyName implements FilterInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The propteries to exclude.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $properties = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Either an exclude or an include.
|
||||||
|
*
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
protected $exclude = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param [ string | array ] $properties The properties to exclude or include.
|
||||||
|
* @param bool $exclude If the method should be excluded
|
||||||
|
*/
|
||||||
|
public function __construct($properties, $exclude = true)
|
||||||
|
{
|
||||||
|
$this->exclude = $exclude;
|
||||||
|
$this->properties = is_array($properties)
|
||||||
|
? $properties
|
||||||
|
: [$properties];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function filter($property)
|
||||||
|
{
|
||||||
|
return in_array($property, $this->properties)
|
||||||
|
? !$this->exclude
|
||||||
|
: $this->exclude;
|
||||||
|
}
|
||||||
|
}
|
||||||
190
src/App/Hydrator/Strategy/AbstractCollectionStrategy.php
Normal file
190
src/App/Hydrator/Strategy/AbstractCollectionStrategy.php
Normal file
@ -0,0 +1,190 @@
|
|||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||||
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||||
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||||
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||||
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||||
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*
|
||||||
|
* This software consists of voluntary contributions made by many individuals
|
||||||
|
* and is licensed under the MIT license. For more information, see
|
||||||
|
* <http://www.doctrine-project.org>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace App\Hydrator\Strategy;
|
||||||
|
|
||||||
|
use InvalidArgumentException;
|
||||||
|
use Doctrine\Common\Collections\Collection;
|
||||||
|
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
|
||||||
|
use Zend\Hydrator\Strategy\StrategyInterface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @license MIT
|
||||||
|
* @link http://www.doctrine-project.org/
|
||||||
|
* @since 0.7.0
|
||||||
|
* @author Michael Gallego <mic.gallego@gmail.com>
|
||||||
|
*/
|
||||||
|
abstract class AbstractCollectionStrategy implements StrategyInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $collectionName;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var ClassMetadata
|
||||||
|
*/
|
||||||
|
protected $metadata;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var object
|
||||||
|
*/
|
||||||
|
protected $object;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the name of the collection
|
||||||
|
*
|
||||||
|
* @param string $collectionName
|
||||||
|
* @return AbstractCollectionStrategy
|
||||||
|
*/
|
||||||
|
public function setCollectionName($collectionName)
|
||||||
|
{
|
||||||
|
$this->collectionName = (string) $collectionName;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the name of the collection
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getCollectionName()
|
||||||
|
{
|
||||||
|
return $this->collectionName;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the class metadata
|
||||||
|
*
|
||||||
|
* @param ClassMetadata $classMetadata
|
||||||
|
* @return AbstractCollectionStrategy
|
||||||
|
*/
|
||||||
|
public function setClassMetadata(ClassMetadata $classMetadata)
|
||||||
|
{
|
||||||
|
$this->metadata = $classMetadata;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the class metadata
|
||||||
|
*
|
||||||
|
* @return ClassMetadata
|
||||||
|
*/
|
||||||
|
public function getClassMetadata()
|
||||||
|
{
|
||||||
|
return $this->metadata;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the object
|
||||||
|
*
|
||||||
|
* @param object $object
|
||||||
|
*
|
||||||
|
* @throws \InvalidArgumentException
|
||||||
|
*
|
||||||
|
* @return AbstractCollectionStrategy
|
||||||
|
*/
|
||||||
|
public function setObject($object)
|
||||||
|
{
|
||||||
|
if (!is_object($object)) {
|
||||||
|
throw new InvalidArgumentException(
|
||||||
|
sprintf('The parameter given to setObject method of %s class is not an object', get_called_class())
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->object = $object;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the object
|
||||||
|
*
|
||||||
|
* @return object
|
||||||
|
*/
|
||||||
|
public function getObject()
|
||||||
|
{
|
||||||
|
return $this->object;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function extract($value)
|
||||||
|
{
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the collection by value (using the public API)
|
||||||
|
*
|
||||||
|
* @throws \InvalidArgumentException
|
||||||
|
*
|
||||||
|
* @return Collection
|
||||||
|
*/
|
||||||
|
protected function getCollectionFromObjectByValue()
|
||||||
|
{
|
||||||
|
$object = $this->getObject();
|
||||||
|
$getter = 'get' . ucfirst($this->getCollectionName());
|
||||||
|
|
||||||
|
if (!method_exists($object, $getter)) {
|
||||||
|
throw new InvalidArgumentException(
|
||||||
|
sprintf(
|
||||||
|
'The getter %s to access collection %s in object %s does not exist',
|
||||||
|
$getter,
|
||||||
|
$this->getCollectionName(),
|
||||||
|
get_class($object)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $object->$getter();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the collection by reference (not using the public API)
|
||||||
|
*
|
||||||
|
* @return Collection
|
||||||
|
*/
|
||||||
|
protected function getCollectionFromObjectByReference()
|
||||||
|
{
|
||||||
|
$object = $this->getObject();
|
||||||
|
$refl = $this->getClassMetadata()->getReflectionClass();
|
||||||
|
$reflProperty = $refl->getProperty($this->getCollectionName());
|
||||||
|
|
||||||
|
$reflProperty->setAccessible(true);
|
||||||
|
|
||||||
|
return $reflProperty->getValue($object);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method is used internally by array_udiff to check if two objects are equal, according to their
|
||||||
|
* SPL hash. This is needed because the native array_diff only compare strings
|
||||||
|
*
|
||||||
|
* @param object $a
|
||||||
|
* @param object $b
|
||||||
|
*
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
protected function compareObjects($a, $b)
|
||||||
|
{
|
||||||
|
return strcmp(spl_object_hash($a), spl_object_hash($b));
|
||||||
|
}
|
||||||
|
}
|
||||||
58
src/App/Hydrator/Strategy/AllowRemoveByReference.php
Normal file
58
src/App/Hydrator/Strategy/AllowRemoveByReference.php
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||||
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||||
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||||
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||||
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||||
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*
|
||||||
|
* This software consists of voluntary contributions made by many individuals
|
||||||
|
* and is licensed under the MIT license. For more information, see
|
||||||
|
* <http://www.doctrine-project.org>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace App\Hydrator\Strategy;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* When this strategy is used for Collections, if the new collection does not contain elements that are present in
|
||||||
|
* the original collection, then this strategy remove elements from the original collection. For instance, if the
|
||||||
|
* collection initially contains elements A and B, and that the new collection contains elements B and C, then the
|
||||||
|
* final collection will contain elements B and C (while element A will be asked to be removed).
|
||||||
|
*
|
||||||
|
* This strategy is by reference, this means it won't use public API to add/remove elements to the collection
|
||||||
|
*
|
||||||
|
* @license MIT
|
||||||
|
* @link http://www.doctrine-project.org/
|
||||||
|
* @since 0.7.0
|
||||||
|
* @author Michael Gallego <mic.gallego@gmail.com>
|
||||||
|
*/
|
||||||
|
class AllowRemoveByReference extends AbstractCollectionStrategy
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function hydrate($value)
|
||||||
|
{
|
||||||
|
$collection = $this->getCollectionFromObjectByReference();
|
||||||
|
$collectionArray = $collection->toArray();
|
||||||
|
|
||||||
|
$toAdd = array_udiff($value, $collectionArray, [$this, 'compareObjects']);
|
||||||
|
$toRemove = array_udiff($collectionArray, $value, [$this, 'compareObjects']);
|
||||||
|
|
||||||
|
foreach ($toAdd as $element) {
|
||||||
|
$collection->add($element);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($toRemove as $element) {
|
||||||
|
$collection->removeElement($element);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $collection;
|
||||||
|
}
|
||||||
|
}
|
||||||
76
src/App/Hydrator/Strategy/AllowRemoveByValue.php
Normal file
76
src/App/Hydrator/Strategy/AllowRemoveByValue.php
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||||
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||||
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||||
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||||
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||||
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*
|
||||||
|
* This software consists of voluntary contributions made by many individuals
|
||||||
|
* and is licensed under the MIT license. For more information, see
|
||||||
|
* <http://www.doctrine-project.org>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace App\Hydrator\Strategy;
|
||||||
|
|
||||||
|
use Doctrine\Common\Collections\Collection;
|
||||||
|
use LogicException;
|
||||||
|
use Doctrine\Common\Collections\ArrayCollection;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* When this strategy is used for Collections, if the new collection does not contain elements that are present in
|
||||||
|
* the original collection, then this strategy remove elements from the original collection. For instance, if the
|
||||||
|
* collection initially contains elements A and B, and that the new collection contains elements B and C, then the
|
||||||
|
* final collection will contain elements B and C (while element A will be asked to be removed).
|
||||||
|
*
|
||||||
|
* This strategy is by value, this means it will use the public API (in this case, adder and remover)
|
||||||
|
*
|
||||||
|
* @license MIT
|
||||||
|
* @link http://www.doctrine-project.org/
|
||||||
|
* @since 0.7.0
|
||||||
|
* @author Michael Gallego <mic.gallego@gmail.com>
|
||||||
|
*/
|
||||||
|
class AllowRemoveByValue extends AbstractCollectionStrategy
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function hydrate($value)
|
||||||
|
{
|
||||||
|
// AllowRemove strategy need "adder" and "remover"
|
||||||
|
$adder = 'add' . ucfirst($this->collectionName);
|
||||||
|
$remover = 'remove' . ucfirst($this->collectionName);
|
||||||
|
|
||||||
|
if (!method_exists($this->object, $adder) || !method_exists($this->object, $remover)) {
|
||||||
|
throw new LogicException(
|
||||||
|
sprintf(
|
||||||
|
'AllowRemove strategy for DoctrineModule hydrator requires both %s and %s to be defined in %s
|
||||||
|
entity domain code, but one or both seem to be missing',
|
||||||
|
$adder,
|
||||||
|
$remover,
|
||||||
|
get_class($this->object)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
$collection = $this->getCollectionFromObjectByValue();
|
||||||
|
|
||||||
|
if ($collection instanceof Collection) {
|
||||||
|
$collection = $collection->toArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
$toAdd = new ArrayCollection(array_udiff($value, $collection, [$this, 'compareObjects']));
|
||||||
|
$toRemove = new ArrayCollection(array_udiff($collection, $value, [$this, 'compareObjects']));
|
||||||
|
|
||||||
|
$this->object->$adder($toAdd);
|
||||||
|
$this->object->$remover($toRemove);
|
||||||
|
|
||||||
|
return $collection;
|
||||||
|
}
|
||||||
|
}
|
||||||
53
src/App/Hydrator/Strategy/DisallowRemoveByReference.php
Normal file
53
src/App/Hydrator/Strategy/DisallowRemoveByReference.php
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||||
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||||
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||||
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||||
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||||
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*
|
||||||
|
* This software consists of voluntary contributions made by many individuals
|
||||||
|
* and is licensed under the MIT license. For more information, see
|
||||||
|
* <http://www.doctrine-project.org>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace App\Hydrator\Strategy;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* When this strategy is used for Collections, if the new collection does not contain elements that are present in
|
||||||
|
* the original collection, then this strategy will not remove those elements. At most, it will add new elements. For
|
||||||
|
* instance, if the collection initially contains elements A and B, and that the new collection contains elements B
|
||||||
|
* and C, then the final collection will contain elements A, B and C.
|
||||||
|
*
|
||||||
|
* This strategy is by reference, this means it won't use the public API to remove elements
|
||||||
|
*
|
||||||
|
* @license MIT
|
||||||
|
* @link http://www.doctrine-project.org/
|
||||||
|
* @since 0.7.0
|
||||||
|
* @author Michael Gallego <mic.gallego@gmail.com>
|
||||||
|
*/
|
||||||
|
class DisallowRemoveByReference extends AbstractCollectionStrategy
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function hydrate($value)
|
||||||
|
{
|
||||||
|
$collection = $this->getCollectionFromObjectByReference();
|
||||||
|
$collectionArray = $collection->toArray();
|
||||||
|
|
||||||
|
$toAdd = array_udiff($value, $collectionArray, [$this, 'compareObjects']);
|
||||||
|
|
||||||
|
foreach ($toAdd as $element) {
|
||||||
|
$collection->add($element);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $collection;
|
||||||
|
}
|
||||||
|
}
|
||||||
72
src/App/Hydrator/Strategy/DisallowRemoveByValue.php
Normal file
72
src/App/Hydrator/Strategy/DisallowRemoveByValue.php
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||||
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||||
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||||
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||||
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||||
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*
|
||||||
|
* This software consists of voluntary contributions made by many individuals
|
||||||
|
* and is licensed under the MIT license. For more information, see
|
||||||
|
* <http://www.doctrine-project.org>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace App\Hydrator\Strategy;
|
||||||
|
|
||||||
|
use Doctrine\Common\Collections\Collection;
|
||||||
|
use LogicException;
|
||||||
|
use Doctrine\Common\Collections\ArrayCollection;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* When this strategy is used for Collections, if the new collection does not contain elements that are present in
|
||||||
|
* the original collection, then this strategy will not remove those elements. At most, it will add new elements. For
|
||||||
|
* instance, if the collection initially contains elements A and B, and that the new collection contains elements B
|
||||||
|
* and C, then the final collection will contain elements A, B and C.
|
||||||
|
*
|
||||||
|
* This strategy is by value, this means it will use the public API (in this case, remover)
|
||||||
|
*
|
||||||
|
* @license MIT
|
||||||
|
* @link http://www.doctrine-project.org/
|
||||||
|
* @since 0.7.0
|
||||||
|
* @author Michael Gallego <mic.gallego@gmail.com>
|
||||||
|
*/
|
||||||
|
class DisallowRemoveByValue extends AbstractCollectionStrategy
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function hydrate($value)
|
||||||
|
{
|
||||||
|
// AllowRemove strategy need "adder"
|
||||||
|
$adder = 'add' . ucfirst($this->collectionName);
|
||||||
|
|
||||||
|
if (!method_exists($this->object, $adder)) {
|
||||||
|
throw new LogicException(
|
||||||
|
sprintf(
|
||||||
|
'DisallowRemove strategy for DoctrineModule hydrator requires %s to
|
||||||
|
be defined in %s entity domain code, but it seems to be missing',
|
||||||
|
$adder,
|
||||||
|
get_class($this->object)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
$collection = $this->getCollectionFromObjectByValue();
|
||||||
|
|
||||||
|
if ($collection instanceof Collection) {
|
||||||
|
$collection = $collection->toArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
$toAdd = new ArrayCollection(array_udiff($value, $collection, [$this, 'compareObjects']));
|
||||||
|
|
||||||
|
$this->object->$adder($toAdd);
|
||||||
|
|
||||||
|
return $collection;
|
||||||
|
}
|
||||||
|
}
|
||||||
22
src/App/Middleware/PreFlightMiddleware.php
Normal file
22
src/App/Middleware/PreFlightMiddleware.php
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Middleware;
|
||||||
|
|
||||||
|
use Psr\Http\Message\RequestInterface;
|
||||||
|
use Psr\Http\Message\ResponseInterface;
|
||||||
|
|
||||||
|
class PreFlightMiddleware
|
||||||
|
{
|
||||||
|
public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next)
|
||||||
|
{
|
||||||
|
$requestMethod = strtoupper($request->getMethod());
|
||||||
|
if ($requestMethod == 'OPTIONS') {
|
||||||
|
return $response
|
||||||
|
->withHeader('Accept', 'OPTIONS,GET,POST,PUT,PATCH,DELETE')
|
||||||
|
->withHeader('Access-Control-Allow-Origin', '*')
|
||||||
|
->withHeader('Access-Control-Allow-Methods', 'OPTIONS,GET,POST,PUT,PATCH,DELETE')
|
||||||
|
->withHeader('Access-Control-Allow-Headers', 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization');
|
||||||
|
}
|
||||||
|
return $next($request, $response);
|
||||||
|
}
|
||||||
|
}
|
||||||
19
src/App/Response/JsonCorsResponse.php
Normal file
19
src/App/Response/JsonCorsResponse.php
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Response;
|
||||||
|
|
||||||
|
use Zend\Diactoros\Response\JsonResponse;
|
||||||
|
|
||||||
|
class JsonCorsResponse extends JsonResponse
|
||||||
|
{
|
||||||
|
public function __construct($data,
|
||||||
|
$status = 200,
|
||||||
|
array $headers = [],
|
||||||
|
$encodingOptions = self::DEFAULT_JSON_FLAGS)
|
||||||
|
{
|
||||||
|
$headers['Access-Control-Allow-Origin'] = '*';
|
||||||
|
$headers['Access-Control-Allow-Methods'] = 'OPTIONS,GET,POST,PUT,PATCH,DELETE';
|
||||||
|
$headers['Access-Control-Allow-Headers'] = 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
|
||||||
|
parent::__construct($data, $status, $headers, $encodingOptions);
|
||||||
|
}
|
||||||
|
}
|
||||||
120
src/App/Service/CiConfigService.php
Normal file
120
src/App/Service/CiConfigService.php
Normal file
@ -0,0 +1,120 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Service;
|
||||||
|
|
||||||
|
use App\Entity\CiConfig;
|
||||||
|
use App\Entity\CiConfigItem;
|
||||||
|
use App\Hydrator\DoctrineObject;
|
||||||
|
use Doctrine\ORM\EntityManager;
|
||||||
|
use Doctrine\ORM\NoResultException;
|
||||||
|
|
||||||
|
class CiConfigService
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var EntityManager
|
||||||
|
*/
|
||||||
|
private $em;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var DoctrineObject
|
||||||
|
*/
|
||||||
|
private $hydrator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* CiConfigService constructor.
|
||||||
|
* @param EntityManager $em
|
||||||
|
* @param DoctrineObject $hydrator
|
||||||
|
*/
|
||||||
|
public function __construct(EntityManager $em, DoctrineObject $hydrator)
|
||||||
|
{
|
||||||
|
$this->em = $em;
|
||||||
|
$this->hydrator = $hydrator;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getList()
|
||||||
|
{
|
||||||
|
$qb = $this->em->createQueryBuilder();
|
||||||
|
$result = $qb->select('c', 'i')
|
||||||
|
->from(CiConfig::class, 'c')
|
||||||
|
->leftJoin('c.configItems', 'i')
|
||||||
|
->getQuery()
|
||||||
|
->getArrayResult();
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $id
|
||||||
|
* @return bool|object
|
||||||
|
*/
|
||||||
|
public function get(int $id)
|
||||||
|
{
|
||||||
|
$qb = $this->em->createQueryBuilder();
|
||||||
|
try {
|
||||||
|
$result = $qb->select('c', 'i')
|
||||||
|
->from(CiConfig::class, 'c')
|
||||||
|
->leftJoin('c.configItems', 'i')
|
||||||
|
->where('c.id = :id')
|
||||||
|
->setParameter('id', $id)
|
||||||
|
->getQuery()
|
||||||
|
->getSingleResult();
|
||||||
|
} catch (NoResultException $ex) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $data
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function create(array $data)
|
||||||
|
{
|
||||||
|
$entity = new CiConfig();
|
||||||
|
$entity = $this->hydrator->hydrate($data, $entity);
|
||||||
|
$this->em->persist($entity);
|
||||||
|
$this->em->flush();
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $id
|
||||||
|
* @param array $data
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function update(int $id, array $data)
|
||||||
|
{
|
||||||
|
if(null === ($entity = $this->em->find(CiConfig::class, $id))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
$this->hydrator->hydrate($data, $entity);
|
||||||
|
$this->em->flush();
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $id
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function delete(int $id)
|
||||||
|
{
|
||||||
|
$entity = $this->em->find(CiConfig::class, $id);
|
||||||
|
$this->em->remove($entity);
|
||||||
|
$this->em->flush();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $id
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function deleteItem(int $id)
|
||||||
|
{
|
||||||
|
$entity = $this->em->find(CiConfigItem::class, $id);
|
||||||
|
$this->em->remove($entity);
|
||||||
|
$this->em->flush();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
15
src/App/Service/CiConfigServiceFactory.php
Normal file
15
src/App/Service/CiConfigServiceFactory.php
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Service;
|
||||||
|
|
||||||
|
use Interop\Container\ContainerInterface;
|
||||||
|
|
||||||
|
class CiConfigServiceFactory
|
||||||
|
{
|
||||||
|
public function __invoke(ContainerInterface $container)
|
||||||
|
{
|
||||||
|
$em = $container->get('doctrine.entity_manager.orm_default');
|
||||||
|
$hydrator = $container->get('doctrine.hydrator');
|
||||||
|
return new CiConfigService($em, $hydrator);
|
||||||
|
}
|
||||||
|
}
|
||||||
0
templates/.gitkeep
Normal file
0
templates/.gitkeep
Normal file
28
test/AppTest/Action/HomePageActionTest.php
Normal file
28
test/AppTest/Action/HomePageActionTest.php
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace AppTest\Action;
|
||||||
|
|
||||||
|
use App\Action\HomePageAction;
|
||||||
|
use Zend\Diactoros\Response;
|
||||||
|
use Zend\Diactoros\ServerRequest;
|
||||||
|
use Zend\Expressive\Router\RouterInterface;
|
||||||
|
|
||||||
|
class HomePageActionTest extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
/** @var RouterInterface */
|
||||||
|
protected $router;
|
||||||
|
|
||||||
|
protected function setUp()
|
||||||
|
{
|
||||||
|
$this->router = $this->prophesize(RouterInterface::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testResponse()
|
||||||
|
{
|
||||||
|
$homePage = new HomePageAction($this->router->reveal(), null);
|
||||||
|
$response = $homePage(new ServerRequest(['/']), new Response(), function () {
|
||||||
|
});
|
||||||
|
|
||||||
|
$this->assertTrue($response instanceof Response);
|
||||||
|
}
|
||||||
|
}
|
||||||
50
test/AppTest/Action/HomePageFactoryTest.php
Normal file
50
test/AppTest/Action/HomePageFactoryTest.php
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace AppTest\Action;
|
||||||
|
|
||||||
|
use App\Action\HomePageAction;
|
||||||
|
use App\Action\HomePageFactory;
|
||||||
|
use Interop\Container\ContainerInterface;
|
||||||
|
use Zend\Expressive\Router\RouterInterface;
|
||||||
|
use Zend\Expressive\Template\TemplateRendererInterface;
|
||||||
|
|
||||||
|
class HomePageFactoryTest extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
/** @var ContainerInterface */
|
||||||
|
protected $container;
|
||||||
|
|
||||||
|
protected function setUp()
|
||||||
|
{
|
||||||
|
$this->container = $this->prophesize(ContainerInterface::class);
|
||||||
|
$router = $this->prophesize(RouterInterface::class);
|
||||||
|
|
||||||
|
$this->container->get(RouterInterface::class)->willReturn($router);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testFactoryWithoutTemplate()
|
||||||
|
{
|
||||||
|
$factory = new HomePageFactory();
|
||||||
|
$this->container->has(TemplateRendererInterface::class)->willReturn(false);
|
||||||
|
|
||||||
|
$this->assertTrue($factory instanceof HomePageFactory);
|
||||||
|
|
||||||
|
$homePage = $factory($this->container->reveal());
|
||||||
|
|
||||||
|
$this->assertTrue($homePage instanceof HomePageAction);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testFactoryWithTemplate()
|
||||||
|
{
|
||||||
|
$factory = new HomePageFactory();
|
||||||
|
$this->container->has(TemplateRendererInterface::class)->willReturn(true);
|
||||||
|
$this->container
|
||||||
|
->get(TemplateRendererInterface::class)
|
||||||
|
->willReturn($this->prophesize(TemplateRendererInterface::class));
|
||||||
|
|
||||||
|
$this->assertTrue($factory instanceof HomePageFactory);
|
||||||
|
|
||||||
|
$homePage = $factory($this->container->reveal());
|
||||||
|
|
||||||
|
$this->assertTrue($homePage instanceof HomePageAction);
|
||||||
|
}
|
||||||
|
}
|
||||||
22
test/AppTest/Action/PingActionTest.php
Normal file
22
test/AppTest/Action/PingActionTest.php
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace AppTest\Action;
|
||||||
|
|
||||||
|
use App\Action\PingAction;
|
||||||
|
use Zend\Diactoros\Response;
|
||||||
|
use Zend\Diactoros\ServerRequest;
|
||||||
|
|
||||||
|
class PingActionTest extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
public function testResponse()
|
||||||
|
{
|
||||||
|
$pingAction = new PingAction();
|
||||||
|
$response = $pingAction(new ServerRequest(['/']), new Response(), function () {
|
||||||
|
});
|
||||||
|
$json = json_decode((string) $response->getBody());
|
||||||
|
|
||||||
|
$this->assertTrue($response instanceof Response);
|
||||||
|
$this->assertTrue($response instanceof Response\JsonResponse);
|
||||||
|
$this->assertTrue(isset($json->ack));
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user