From 99c221966a83c7c8f37c4796dbe60aec6318924b Mon Sep 17 00:00:00 2001 From: Ruben van der Linde Date: Wed, 18 Jan 2023 21:29:05 +0100 Subject: [PATCH 1/3] Added loger to services --- Service/CacheService.php | 14 ++++++++++++++ Service/CallService.php | 5 ++++- Service/ComposerService.php | 9 +++++++++ Service/InstallationService.php | 17 +++++++++++++++++ Service/RequestService.php | 4 +++- 5 files changed, 47 insertions(+), 2 deletions(-) diff --git a/Service/CacheService.php b/Service/CacheService.php index d33f1e7a..637a5fb9 100644 --- a/Service/CacheService.php +++ b/Service/CacheService.php @@ -10,6 +10,7 @@ use Doctrine\ORM\NonUniqueResultException; use Exception; use MongoDB\Client; +use Monolog\Logger; use Ramsey\Uuid\Uuid; use Symfony\Component\Cache\Adapter\AdapterInterface as CacheInterface; use Symfony\Component\Console\Command\Command; @@ -37,6 +38,7 @@ class CacheService private SymfonyStyle $io; private ParameterBagInterface $parameters; private SerializerInterface $serializer; + private Logger $logger; /** * @param AuthenticationService $authenticationService @@ -56,6 +58,7 @@ public function __construct( if ($this->parameters->get('cache_url', false)) { $this->client = new Client($this->parameters->get('cache_url')); } + $this->logger = New Logger('cache'); } /** @@ -239,8 +242,12 @@ public function cacheObject(ObjectEntity $objectEntity): ObjectEntity ['upsert'=>true] )) { (isset($this->io) ? $this->io->writeln('Updated object '.$objectEntity->getId()->toString().' of type '.$objectEntity->getEntity()->getName().' to cache') : ''); + + $this->logger->debug("updated object to cache"); } else { (isset($this->io) ? $this->io->writeln('Wrote object '.$objectEntity->getId()->toString().' of type '.$objectEntity->getEntity()->getName().' to cache') : ''); + + $this->logger->debug("Added object to cache"); } return $objectEntity; @@ -264,6 +271,8 @@ public function removeObject(ObjectEntity $object): void $collection = $this->client->objects->json; $collection->findOneAndDelete(['_id'=>$id]); + + $this->logger->debug("Removed object from cache"); } /** @@ -284,11 +293,13 @@ public function getObject(string $id) // Check if object is in the cache ???? if ($object = $collection->findOne(['_id'=>$id])) { + $this->logger->debug("Retrieved cached object from cache"); return $object; } // Fall back tot the entity manager if ($object = $this->entityManager->getRepository('App:ObjectEntity')->findOneBy(['id'=>$id])) { + $this->logger->debug("Retrieved uncached object from cache"); return $this->cacheObject($object)->toArray(['embedded' => true]); } @@ -365,6 +376,9 @@ public function searchObjects(string $search = null, array $filter = [], array $ $results = $collection->find($filter, ['limit' => $limit, 'skip' => $start, 'sort' => $order])->toArray(); $total = $collection->count($filter); + + $this->logger->debug("Searched cache for objects"); + // Make sure to add the pagination properties in response return $this->handleResultPagination($completeFilter, $results, $total); } diff --git a/Service/CallService.php b/Service/CallService.php index 19a07018..266b7d07 100644 --- a/Service/CallService.php +++ b/Service/CallService.php @@ -12,7 +12,8 @@ use GuzzleHttp\Exception\ServerException; use GuzzleHttp\Psr7\Response; use Symfony\Component\HttpKernel\Exception\HttpException; -use Symfony\Component\Serializer\Encoder\XmlEncoder; +use Symfony\Component\Serializer\Encoder\XmlEncoder: +use Monolog\Logger; /** * Service to call external sources. @@ -32,6 +33,7 @@ class CallService private Client $client; private EntityManagerInterface $entityManager; private FileService $fileService; + private Logger $logger; /** * @param AuthenticationService $authenticationService @@ -44,6 +46,7 @@ public function __construct(AuthenticationService $authenticationService, Entity $this->client = new Client([]); $this->entityManager = $entityManager; $this->fileService = $fileService; + $this->logger = New Logger('call'); } /** diff --git a/Service/ComposerService.php b/Service/ComposerService.php index 22242e4d..6de1ef93 100644 --- a/Service/ComposerService.php +++ b/Service/ComposerService.php @@ -6,9 +6,18 @@ use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Process\Exception\ProcessFailedException; use Symfony\Component\Process\Process; +use Monolog\Logger; class ComposerService { + + private Logger $logger; + + public function __construct(){ + + $this->logger = New Logger('installation'); + } + private function arrayEnum(array $array, array $enum): bool { // Lets see if the values in the array arry pressent in the enum diff --git a/Service/InstallationService.php b/Service/InstallationService.php index e7179b3a..f7c69b21 100644 --- a/Service/InstallationService.php +++ b/Service/InstallationService.php @@ -11,6 +11,7 @@ use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Finder\Finder; +use Monolog\Logger; class InstallationService { @@ -18,6 +19,7 @@ class InstallationService private EntityManagerInterface $em; private SymfonyStyle $io; private $container; + private Logger $logger; public function __construct( ComposerService $composerService, @@ -28,6 +30,8 @@ public function __construct( $this->em = $em; $this->container = $kernel->getContainer(); $this->collection = null; + $this->logger = New Logger('installation'); + } /** @@ -59,7 +63,10 @@ public function composerupdate(): int ]); } + $this->logger->debug("Running plugin installer"); + foreach ($plugins as $plugin) { + $this->install($plugin['name']); } @@ -84,6 +91,9 @@ public function install(string $bundle, bool $noSchema = false): int ]); } + + $this->logger->debug('Trying to install: '.$bundle; + $packages = $this->composerService->getAll(); $found = array_filter($packages, function ($v, $k) use ($bundle) { @@ -123,6 +133,8 @@ public function install(string $bundle, bool $noSchema = false): int // We want each plugin to also be a collection (if it contains schema's that is) if (count($schemas) > 0) { if (!$this->collection = $this->em->getRepository('App:CollectionEntity')->findOneBy(['plugin' => $package['name']])) { + + $this->logger->debug('Created a collection for plugin '.$bundle); $this->io->writeln(['Created a collection for this plugin', '']); $this->collection = new CollectionEntity(); $this->collection->setName($package['name']); @@ -130,6 +142,7 @@ public function install(string $bundle, bool $noSchema = false): int isset($package['description']) && $this->collection->setDescription($package['description']); } else { $this->io->writeln(['Found a collection for this plugin', '']); + $this->logger->debug('Found a collection for plugin '.$bundle); } } @@ -145,6 +158,7 @@ public function install(string $bundle, bool $noSchema = false): int //$progressBar->finish(); } else { $this->io->writeln('No schema folder found'); + $this->logger->debug('No schema folder found for plugin '.$bundle); } // Handling the data @@ -163,6 +177,7 @@ public function install(string $bundle, bool $noSchema = false): int // We need to clear the finder } else { + $this->logger->debug('No data folder found for plugin '.$bundle); $this->io->writeln('No data folder found'); } @@ -179,10 +194,12 @@ public function install(string $bundle, bool $noSchema = false): int $this->handleInstaller($installer); } } else { + $this->logger->debug('No Installation folder found for plugin '.$bundle); $this->io->writeln('No Installation folder found'); } $this->io->success('All Done'); + $this->logger->debug('All Done installing plugin '.$bundle); return Command::SUCCESS; } diff --git a/Service/RequestService.php b/Service/RequestService.php index f66cb90f..6a125a28 100644 --- a/Service/RequestService.php +++ b/Service/RequestService.php @@ -20,6 +20,7 @@ use Symfony\Component\HttpFoundation\Session\Session; use Symfony\Component\Security\Core\Security; use Symfony\Component\Serializer\SerializerInterface; +use Monolog\Logger; /** * Handles incomming request from endpoints or controllers that relate to the gateways object structure (eav). @@ -36,11 +37,11 @@ class RequestService // todo: we might want to move or rewrite code instead of using these services here: private ResponseService $responseService; private ObjectEntityService $objectEntityService; - private LogService $logService; private CallService $callService; private Security $security; private EventDispatcherInterface $eventDispatcher; private SerializerInterface $serializer; + private Logger $logger; /** * @param EntityManagerInterface $entityManager @@ -73,6 +74,7 @@ public function __construct( $this->security = $security; $this->eventDispatcher = $eventDispatcher; $this->serializer = $serializer; + $this->logger = New Logger('request'); } /** From 922b8260f23c49713b89dc249543952a8573141a Mon Sep 17 00:00:00 2001 From: Ruben van der Linde Date: Wed, 18 Jan 2023 21:45:30 +0100 Subject: [PATCH 2/3] Typo fix --- Service/InstallationService.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Service/InstallationService.php b/Service/InstallationService.php index f7c69b21..84a1583a 100644 --- a/Service/InstallationService.php +++ b/Service/InstallationService.php @@ -92,7 +92,7 @@ public function install(string $bundle, bool $noSchema = false): int } - $this->logger->debug('Trying to install: '.$bundle; + $this->logger->debug('Trying to install: '.$bundle); $packages = $this->composerService->getAll(); From 8d46845a560e93589dcc388da5eb4e2c1fad97e9 Mon Sep 17 00:00:00 2001 From: Ruben van der Linde Date: Thu, 19 Jan 2023 11:48:28 +0100 Subject: [PATCH 3/3] Small style fixes --- Service/InstallationService.php | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/Service/InstallationService.php b/Service/InstallationService.php index 84a1583a..26afd68a 100644 --- a/Service/InstallationService.php +++ b/Service/InstallationService.php @@ -352,18 +352,17 @@ public function handleInstaller($file) } /** - * Handles forced id's on object entities. + * Handles forced id's on object entities * * @param ObjectEntity $objectEntity - * * @return ObjectEntity */ - private function saveOnFixedId(ObjectEntity $objectEntity): ObjectEntity - { + private function saveOnFixedId(ObjectEntity $objectEntity): ObjectEntity{ // This savetey dosn't make sense but we need it - if (!$objectEntity->getEntity()) { - $this->io->writeln(['', 'Object can\'t be persisted due to missing schema']); + if(!$objectEntity->getEntity()){ + $this->logger->error('Object can\'t be persisted due to missing schema'); + $this->io->writeln(['', 'Object can\'t be persisted due to missing schema']); return $objectEntity; } @@ -372,7 +371,8 @@ private function saveOnFixedId(ObjectEntity $objectEntity): ObjectEntity $objectEntity->clearAllValues(); // We have an object entity with a fixed id that isn't in the database, so we need to act - if ($objectEntity->getId() && !$this->em->contains($objectEntity)) { + if($objectEntity->getId() && !$this->em->contains($objectEntity)){ + $this->io->writeln(['', 'Creating new object ('.$objectEntity->getEntity()->getName().') on a fixed id ('.$objectEntity->getId().')']); // Sve the id @@ -386,24 +386,26 @@ private function saveOnFixedId(ObjectEntity $objectEntity): ObjectEntity $this->em->persist($objectEntity); $this->em->flush(); $objectEntity = $this->em->getRepository('App:ObjectEntity')->findOneBy(['id' => $id]); - } else { + } + else{ $this->io->writeln(['', 'Creating new object ('.$objectEntity->getEntity()->getName().') on a generated id']); } // Loop trough the values - foreach ($values as $objectValue) { + foreach ($values as $objectValue){ $objectEntity->addObjectValue($objectValue); // If the value itsself is an object it might also contain fixed id's - foreach ($objectValue->getObjects() as $subobject) { + foreach ($objectValue->getObjects() as $subobject){ $this->io->writeln(['', 'Found sub object ('.$subobject->getEntity()->getName().')']); $subobject = $this->saveOnFixedId($subobject); // This savetey dosn't make sense but we need it - if (!$subobject->getEntity()) { + if(!$subobject->getEntity()){ // todo: Throw error $objectEntity->removeObjectValue($objectValue); } } + } $this->em->persist($objectEntity); @@ -411,4 +413,7 @@ private function saveOnFixedId(ObjectEntity $objectEntity): ObjectEntity return $objectEntity; } + + + }