Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/autoload test #6

Merged
merged 3 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 0 additions & 18 deletions otel-autoload-example.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,22 @@ protected static function registerAdditionalHooks(): void
return;
}

include_once sprintf('%s/src/Generated/OpenTelemetry/Hooks/%s.php', APPLICATION_ROOT_DIR, HookGenerator::GLOBAL_HOOK_FILE_NAME);
$classmapFileName = APPLICATION_ROOT_DIR . '/src/Generated/OpenTelemetry/Hooks/classmap.php';
if (!file_exists($classmapFileName)) {
return;
}

require_once $classmapFileName;

$autoload = function (string $class) use ($classmap)
{
if (!isset($classmap[$class])) {
return;
}
include_once $classmap[$class];
};

spl_autoload_register($autoload, true, true);
}

/**
Expand Down
33 changes: 20 additions & 13 deletions src/Spryker/Zed/Opentelemetry/Business/Generator/HookGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@
class HookGenerator implements HookGeneratorInterface
{
/**
* @var string
* @var int
*/
public const GLOBAL_HOOK_FILE_NAME = 'GlobalHook';
protected const DIRECTORY_PERMISSIONS = 0755;

/**
* @var int
* @var string
*/
protected const DIRECTORY_PERMISSIONS = 0755;
protected const OUTPUT_FILE_PATH_PLACEHOLDER = '%s%s-%sHook.php';

/**
* @var string
*/
protected const OUTPUT_FILE_PATH_PLACEHOLDER_GLOBAL = '%s/%s.php';
protected const CLASSMAP_FILE_NAME_PATTERN = '%sclassmap.php';

/**
* @var string
Expand Down Expand Up @@ -84,23 +84,31 @@ public function generate(): void
{
$collectedClasses = $this->collector->collectClasses();

$this->ensureDirectoryExists();
$classMapFileName = sprintf(static::CLASSMAP_FILE_NAME_PATTERN, $this->config->getOutputDir());
$classMap = fopen($classMapFileName, 'a');
fwrite($classMap, '<?php' . PHP_EOL . '$classmap = [' . PHP_EOL);
foreach ($collectedClasses as $class) {
$content = PHP_EOL;
$this->ensureDirectoryExists();

if (!file_exists($this->getOutputFilepathByModule($class))) {
$hookFile = $this->getOutputFilepath($class);
if (!file_exists($hookFile)) {
$content = '<?php
if (\Spryker\Service\Opentelemetry\Instrumentation\Sampler\TraceSampleResult::shouldSkipTraceBody()) {
return;
}
' . PHP_EOL;
}
$file = fopen($this->getOutputFilepathByModule($class), 'a');

$file = fopen($hookFile, 'a');

fwrite($file, $content);
fwrite($file, $this->contentCreator->createHookContent($class));
fclose($file);
fwrite($classMap, '\'' . $class[static::NAMESPACE_KEY] . '\\' . $class[static::CLASS_NAME_KEY] . '\' => \'' . $hookFile . '\',' . PHP_EOL);
}
fwrite($classMap, '];' . PHP_EOL);
fclose($classMap);
}

/**
Expand All @@ -120,14 +128,13 @@ protected function ensureDirectoryExists(): void
*
* @return string
*/
protected function getOutputFilepathByModule(array $class): string
protected function getOutputFilepath(array $class): string
{
$namespace = explode('\\', $class[static::NAMESPACE_KEY]);
$moduleNamePattern = $namespace[0] . $namespace[1] . $namespace[2];
return sprintf(
static::OUTPUT_FILE_PATH_PLACEHOLDER_GLOBAL,
static::OUTPUT_FILE_PATH_PLACEHOLDER,
$this->config->getOutputDir(),
static::GLOBAL_HOOK_FILE_NAME,
str_replace('\\', '-', $class[static::NAMESPACE_KEY]),
$class[static::CLASS_NAME_KEY],
);
}
}
Loading