From ad0619748ee54109c144d16eb99bf14de026c7bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Pineau?= Date: Wed, 22 May 2019 17:27:56 +0200 Subject: [PATCH 1/6] [Workflow] Do not trigger extra guard With this patch, guard are executed only on wanted transitions --- .../Component/Workflow/Tests/WorkflowTest.php | 39 +++++++++++++++++++ src/Symfony/Component/Workflow/Workflow.php | 26 +++++-------- 2 files changed, 49 insertions(+), 16 deletions(-) diff --git a/src/Symfony/Component/Workflow/Tests/WorkflowTest.php b/src/Symfony/Component/Workflow/Tests/WorkflowTest.php index 68ebaafcd9..d6f6bae45c 100644 --- a/src/Symfony/Component/Workflow/Tests/WorkflowTest.php +++ b/src/Symfony/Component/Workflow/Tests/WorkflowTest.php @@ -304,6 +304,45 @@ class WorkflowTest extends TestCase $this->assertSame($eventNameExpected, $eventDispatcher->dispatchedEvents); } + public function testApplyDoesNotTriggerExtraGuardWithEventDispatcher() + { + $transitions[] = new Transition('a-b', 'a', 'b'); + $transitions[] = new Transition('a-c', 'a', 'c'); + $definition = new Definition(['a', 'b', 'c'], $transitions); + + $subject = new \stdClass(); + $subject->marking = null; + $eventDispatcher = new EventDispatcherMock(); + $workflow = new Workflow($definition, new MultipleStateMarkingStore(), $eventDispatcher, 'workflow_name'); + + $eventNameExpected = [ + 'workflow.guard', + 'workflow.workflow_name.guard', + 'workflow.workflow_name.guard.a-b', + 'workflow.leave', + 'workflow.workflow_name.leave', + 'workflow.workflow_name.leave.a', + 'workflow.transition', + 'workflow.workflow_name.transition', + 'workflow.workflow_name.transition.a-b', + 'workflow.enter', + 'workflow.workflow_name.enter', + 'workflow.workflow_name.enter.b', + 'workflow.entered', + 'workflow.workflow_name.entered', + 'workflow.workflow_name.entered.b', + 'workflow.completed', + 'workflow.workflow_name.completed', + 'workflow.workflow_name.completed.a-b', + 'workflow.announce', + 'workflow.workflow_name.announce', + ]; + + $marking = $workflow->apply($subject, 'a-b'); + + $this->assertSame($eventNameExpected, $eventDispatcher->dispatchedEvents); + } + public function testEventName() { $definition = $this->createComplexWorkflowDefinition(); diff --git a/src/Symfony/Component/Workflow/Workflow.php b/src/Symfony/Component/Workflow/Workflow.php index a0500b3796..18ca7f7969 100644 --- a/src/Symfony/Component/Workflow/Workflow.php +++ b/src/Symfony/Component/Workflow/Workflow.php @@ -125,22 +125,20 @@ class Workflow */ public function apply($subject, $transitionName) { - $transitions = $this->getEnabledTransitions($subject); + $marking = $this->getMarking($subject); + $transitions = []; - // We can shortcut the getMarking method in order to boost performance, - // since the "getEnabledTransitions" method already checks the Marking - // state - $marking = $this->markingStore->getMarking($subject); + foreach ($this->definition->getTransitions() as $transition) { + if ($transitionName === $transition->getName() && $this->doCan($subject, $marking, $transition)) { + $transitions[] = $transition; + } + } - $applied = false; + if (!$transitions) { + throw new LogicException(sprintf('Unable to apply transition "%s" for workflow "%s".', $transitionName, $this->name)); + } foreach ($transitions as $transition) { - if ($transitionName !== $transition->getName()) { - continue; - } - - $applied = true; - $this->leave($subject, $transition, $marking); $this->transition($subject, $transition, $marking); @@ -156,10 +154,6 @@ class Workflow $this->announce($subject, $transition, $marking); } - if (!$applied) { - throw new LogicException(sprintf('Unable to apply transition "%s" for workflow "%s".', $transitionName, $this->name)); - } - return $marking; } From fbfe2dfa8a573c7a9289951685e9fd3f3baa3b65 Mon Sep 17 00:00:00 2001 From: Robin Chalas Date: Sun, 26 May 2019 22:33:59 +0200 Subject: [PATCH 2/6] [Messenger] Use real memory usage for --memory-limit --- .../Receiver/StopWhenMemoryUsageIsExceededReceiver.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/Messenger/Transport/Receiver/StopWhenMemoryUsageIsExceededReceiver.php b/src/Symfony/Component/Messenger/Transport/Receiver/StopWhenMemoryUsageIsExceededReceiver.php index 41e0df22bc..e61fe5937a 100644 --- a/src/Symfony/Component/Messenger/Transport/Receiver/StopWhenMemoryUsageIsExceededReceiver.php +++ b/src/Symfony/Component/Messenger/Transport/Receiver/StopWhenMemoryUsageIsExceededReceiver.php @@ -32,7 +32,7 @@ class StopWhenMemoryUsageIsExceededReceiver implements ReceiverInterface $this->memoryLimit = $memoryLimit; $this->logger = $logger; $this->memoryResolver = $memoryResolver ?: function () { - return \memory_get_usage(); + return \memory_get_usage(true); }; } From ec098d6c5d5e6092ed91e43f34aa9d8942af4234 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20K=C3=A4fer?= Date: Sun, 26 May 2019 22:15:37 +0200 Subject: [PATCH 3/6] Small grammar mistake in documentation --- .../HttpKernel/Controller/ControllerResolverInterface.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/HttpKernel/Controller/ControllerResolverInterface.php b/src/Symfony/Component/HttpKernel/Controller/ControllerResolverInterface.php index afe9fb7337..95c59036ff 100644 --- a/src/Symfony/Component/HttpKernel/Controller/ControllerResolverInterface.php +++ b/src/Symfony/Component/HttpKernel/Controller/ControllerResolverInterface.php @@ -31,7 +31,7 @@ interface ControllerResolverInterface * As several resolvers can exist for a single application, a resolver must * return false when it is not able to determine the controller. * - * The resolver must only throw an exception when it should be able to load + * The resolver must only throw an exception when it should be able to load a * controller but cannot because of some errors made by the developer. * * @return callable|false A PHP callable representing the Controller, From 34d4fa66e71c3322bf1c5b533375fa0fc3aa9e68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20K=C3=A4fer?= Date: Sun, 26 May 2019 22:50:43 +0200 Subject: [PATCH 4/6] Fixes a small doc blocks syntax error --- src/Symfony/Component/HttpFoundation/Request.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/HttpFoundation/Request.php b/src/Symfony/Component/HttpFoundation/Request.php index ae3f51b172..ea3f460c46 100644 --- a/src/Symfony/Component/HttpFoundation/Request.php +++ b/src/Symfony/Component/HttpFoundation/Request.php @@ -2050,7 +2050,7 @@ class Request } } - /* + /** * Returns the prefix as encoded in the string when the string starts with * the given prefix, false otherwise. * From b6ff836a4925ad2c320b1f0e61f0591696a5d8d7 Mon Sep 17 00:00:00 2001 From: Andrii Popov Date: Mon, 27 May 2019 21:45:58 +0300 Subject: [PATCH 5/6] fix typo --- .../Component/HttpKernel/EventListener/FragmentListener.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/HttpKernel/EventListener/FragmentListener.php b/src/Symfony/Component/HttpKernel/EventListener/FragmentListener.php index 1333120e2d..997f260d33 100644 --- a/src/Symfony/Component/HttpKernel/EventListener/FragmentListener.php +++ b/src/Symfony/Component/HttpKernel/EventListener/FragmentListener.php @@ -24,7 +24,7 @@ use Symfony\Component\HttpKernel\UriSigner; * All URL paths starting with /_fragment are handled as * content fragments by this listener. * - * If throws an AccessDeniedHttpException exception if the request + * Throws an AccessDeniedHttpException exception if the request * is not signed or if it is not an internal sub-request. * * @author Fabien Potencier From 1214609b37e707cba8feaf320a58b981a32c6010 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Mon, 27 May 2019 20:15:39 +0200 Subject: [PATCH 6/6] [HttpClient] make $response->getInfo('debug') return extended logs about the HTTP transaction --- .../Component/HttpClient/NativeHttpClient.php | 12 ++++++++- .../HttpClient/Response/CurlResponse.php | 15 +++++++++++ .../HttpClient/Response/NativeResponse.php | 26 ++++++++++++++++--- .../HttpClient/Response/ResponseTrait.php | 10 +++++-- 4 files changed, 57 insertions(+), 6 deletions(-) diff --git a/src/Symfony/Component/HttpClient/NativeHttpClient.php b/src/Symfony/Component/HttpClient/NativeHttpClient.php index 3442b50ef3..b8e39e9479 100644 --- a/src/Symfony/Component/HttpClient/NativeHttpClient.php +++ b/src/Symfony/Component/HttpClient/NativeHttpClient.php @@ -108,6 +108,7 @@ final class NativeHttpClient implements HttpClientInterface, LoggerAwareInterfac 'size_body' => \strlen($options['body']), 'primary_ip' => '', 'primary_port' => 'http:' === $url['scheme'] ? 80 : 443, + 'debug' => \extension_loaded('curl') ? '' : "* Enable the curl extension for better performance\n", ]; if ($onProgress = $options['on_progress']) { @@ -139,6 +140,8 @@ final class NativeHttpClient implements HttpClientInterface, LoggerAwareInterfac $info['size_download'] = $dlNow; } elseif (STREAM_NOTIFY_CONNECT === $code) { $info['connect_time'] += $now - $info['fopen_time']; + $info['debug'] .= $info['request_header']; + unset($info['request_header']); } else { return; } @@ -160,13 +163,16 @@ final class NativeHttpClient implements HttpClientInterface, LoggerAwareInterfac $options['request_headers'][] = 'host: '.$host.$port; } + if (!isset($options['headers']['user-agent'])) { + $options['request_headers'][] = 'user-agent: Symfony HttpClient/Native'; + } + $context = [ 'http' => [ 'protocol_version' => $options['http_version'] ?: '1.1', 'method' => $method, 'content' => $options['body'], 'ignore_errors' => true, - 'user_agent' => 'Symfony HttpClient/Native', 'curl_verify_ssl_peer' => $options['verify_peer'], 'curl_verify_ssl_host' => $options['verify_host'], 'auto_decode' => false, // Disable dechunk filter, it's incompatible with stream_select() @@ -296,6 +302,7 @@ final class NativeHttpClient implements HttpClientInterface, LoggerAwareInterfac $host = parse_url($url['authority'], PHP_URL_HOST); if (null === $ip = $multi->dnsCache[$host] ?? null) { + $info['debug'] .= "* Hostname was NOT found in DNS cache\n"; $now = microtime(true); if (!$ip = gethostbynamel($host)) { @@ -304,6 +311,9 @@ final class NativeHttpClient implements HttpClientInterface, LoggerAwareInterfac $info['namelookup_time'] += microtime(true) - $now; $multi->dnsCache[$host] = $ip = $ip[0]; + $info['debug'] .= "* Added {$host}:0:{$ip} to DNS cache\n"; + } else { + $info['debug'] .= "* Hostname was found in DNS cache\n"; } $info['primary_ip'] = $ip; diff --git a/src/Symfony/Component/HttpClient/Response/CurlResponse.php b/src/Symfony/Component/HttpClient/Response/CurlResponse.php index a54ab6dc20..6af5f5af3d 100644 --- a/src/Symfony/Component/HttpClient/Response/CurlResponse.php +++ b/src/Symfony/Component/HttpClient/Response/CurlResponse.php @@ -28,6 +28,7 @@ final class CurlResponse implements ResponseInterface private static $performing = false; private $multi; + private $debugBuffer; /** * @internal @@ -38,6 +39,9 @@ final class CurlResponse implements ResponseInterface if (\is_resource($ch)) { $this->handle = $ch; + $this->debugBuffer = fopen('php://temp', 'w+'); + curl_setopt($ch, CURLOPT_VERBOSE, true); + curl_setopt($ch, CURLOPT_STDERR, $this->debugBuffer); } else { $this->info['url'] = $ch; $ch = $this->handle; @@ -143,6 +147,13 @@ final class CurlResponse implements ResponseInterface { if (!$info = $this->finalInfo) { self::perform($this->multi); + + if ('debug' === $type) { + rewind($this->debugBuffer); + + return stream_get_contents($this->debugBuffer); + } + $info = array_merge($this->info, curl_getinfo($this->handle)); $info['url'] = $this->info['url'] ?? $info['url']; $info['redirect_url'] = $this->info['redirect_url'] ?? null; @@ -154,6 +165,10 @@ final class CurlResponse implements ResponseInterface } if (!\in_array(curl_getinfo($this->handle, CURLINFO_PRIVATE), ['headers', 'content'], true)) { + rewind($this->debugBuffer); + $info['debug'] = stream_get_contents($this->debugBuffer); + fclose($this->debugBuffer); + $this->debugBuffer = null; $this->finalInfo = $info; } } diff --git a/src/Symfony/Component/HttpClient/Response/NativeResponse.php b/src/Symfony/Component/HttpClient/Response/NativeResponse.php index 037dd5da88..8be4b04163 100644 --- a/src/Symfony/Component/HttpClient/Response/NativeResponse.php +++ b/src/Symfony/Component/HttpClient/Response/NativeResponse.php @@ -34,6 +34,7 @@ final class NativeResponse implements ResponseInterface private $buffer; private $inflate; private $multi; + private $debugBuffer; /** * @internal @@ -76,12 +77,19 @@ final class NativeResponse implements ResponseInterface { if (!$info = $this->finalInfo) { self::perform($this->multi); + + if ('debug' === $type) { + return $this->info['debug']; + } + $info = $this->info; $info['url'] = implode('', $info['url']); - unset($info['fopen_time'], $info['size_body']); + unset($info['fopen_time'], $info['size_body'], $info['request_header']); if (null === $this->buffer) { $this->finalInfo = $info; + } else { + unset($info['debug']); } } @@ -112,10 +120,23 @@ final class NativeResponse implements ResponseInterface $url = $this->url; while (true) { + $context = stream_context_get_options($this->context); + + if ($proxy = $context['http']['proxy'] ?? null) { + $this->info['debug'] .= "* Establish HTTP proxy tunnel to {$proxy}\n"; + $this->info['request_header'] = $url; + } else { + $this->info['debug'] .= "* Trying {$this->info['primary_ip']}...\n"; + $this->info['request_header'] = $this->info['url']['path'].$this->info['url']['query']; + } + + $this->info['request_header'] = sprintf("> %s %s HTTP/%s \r\n", $context['http']['method'], $this->info['request_header'], $context['http']['protocol_version']); + $this->info['request_header'] .= implode("\r\n", $context['http']['header'])."\r\n\r\n"; + // Send request and follow redirects when needed $this->info['fopen_time'] = microtime(true); $this->handle = $h = fopen($url, 'r', false, $this->context); - self::addResponseHeaders($http_response_header, $this->info, $this->headers); + self::addResponseHeaders($http_response_header, $this->info, $this->headers, $this->info['debug']); $url = ($this->resolveRedirect)($this->multi, $this->headers['location'][0] ?? null, $this->context); if (null === $url) { @@ -136,7 +157,6 @@ final class NativeResponse implements ResponseInterface } stream_set_blocking($h, false); - $context = stream_context_get_options($this->context); $this->context = $this->resolveRedirect = null; if (isset($context['ssl']['peer_certificate_chain'])) { diff --git a/src/Symfony/Component/HttpClient/Response/ResponseTrait.php b/src/Symfony/Component/HttpClient/Response/ResponseTrait.php index 98e96ea0a6..79cc40d5d8 100644 --- a/src/Symfony/Component/HttpClient/Response/ResponseTrait.php +++ b/src/Symfony/Component/HttpClient/Response/ResponseTrait.php @@ -189,19 +189,25 @@ trait ResponseTrait */ abstract protected static function select(ClientState $multi, float $timeout): int; - private static function addResponseHeaders(array $responseHeaders, array &$info, array &$headers): void + private static function addResponseHeaders(array $responseHeaders, array &$info, array &$headers, string &$debug = ''): void { foreach ($responseHeaders as $h) { if (11 <= \strlen($h) && '/' === $h[4] && preg_match('#^HTTP/\d+(?:\.\d+)? ([12345]\d\d) .*#', $h, $m)) { - $headers = []; + if ($headers) { + $debug .= "< \r\n"; + $headers = []; + } $info['http_code'] = (int) $m[1]; } elseif (2 === \count($m = explode(':', $h, 2))) { $headers[strtolower($m[0])][] = ltrim($m[1]); } + $debug .= "< {$h}\r\n"; $info['response_headers'][] = $h; } + $debug .= "< \r\n"; + if (!$info['http_code']) { throw new TransportException('Invalid or missing HTTP status line.'); }