Skip to content

Commit

Permalink
Merge pull request #112 from shadoWalker89/main
Browse files Browse the repository at this point in the history
Add AbstractCliBasedDriver::launchProcess()
  • Loading branch information
pyrech authored Sep 30, 2024
2 parents b34dac1 + a4ed372 commit 1320ed1
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 10 deletions.
2 changes: 1 addition & 1 deletion example/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@

$driver = $notifier->getDriver();

echo 'Notification ', $result ? 'successfully sent' : 'failed', ' with ', str_replace('Joli\\JoliNotif\\Driver\\', '', $driver::class), \PHP_EOL;
echo 'Notification ', $result ? 'successfully sent' : 'failed', ' with ', str_replace('Joli\JoliNotif\Driver\\', '', $driver::class), \PHP_EOL;
2 changes: 1 addition & 1 deletion src/DefaultNotifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function __construct(
?LoggerInterface $logger = null,
/** @var list<DriverInterface> $additionalDrivers */
private readonly array $additionalDrivers = [],
private readonly bool $useOnlyAdditionalDrivers = false
private readonly bool $useOnlyAdditionalDrivers = false,
) {
$this->logger = $logger ?? new NullLogger();
}
Expand Down
7 changes: 6 additions & 1 deletion src/Driver/AbstractCliBasedDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function send(Notification $notification): bool
}

$process = new Process(array_merge([$binary], $arguments));
$process->run();
$this->launchProcess($process);

return $this->handleExitCode($process);
}
Expand Down Expand Up @@ -125,6 +125,11 @@ protected function isBinaryAvailable(): bool
return $process->isSuccessful();
}

protected function launchProcess(Process $process): void
{
$process->run();
}

/**
* Return whether the process executed successfully.
*/
Expand Down
8 changes: 4 additions & 4 deletions src/Driver/AppleScriptDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,18 @@ public function getPriority(): int

protected function getCommandLineArguments(Notification $notification): array
{
$script = 'display notification "' . str_replace('"', '\\"', $notification->getBody() ?? '') . '"';
$script = 'display notification "' . str_replace('"', '\"', $notification->getBody() ?? '') . '"';

if ($notification->getTitle()) {
$script .= ' with title "' . str_replace('"', '\\"', $notification->getTitle()) . '"';
$script .= ' with title "' . str_replace('"', '\"', $notification->getTitle()) . '"';
}

if ($notification->getOption('subtitle')) {
$script .= ' subtitle "' . str_replace('"', '\\"', (string) $notification->getOption('subtitle')) . '"';
$script .= ' subtitle "' . str_replace('"', '\"', (string) $notification->getOption('subtitle')) . '"';
}

if ($notification->getOption('sound')) {
$script .= ' sound name "' . str_replace('"', '\\"', (string) $notification->getOption('sound')) . '"';
$script .= ' sound name "' . str_replace('"', '\"', (string) $notification->getOption('sound')) . '"';
}

return [
Expand Down
11 changes: 11 additions & 0 deletions src/Driver/NotifuDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Joli\JoliNotif\Notification;
use JoliCode\PhpOsHelper\OsHelper;
use Symfony\Component\Process\Process;

/**
* This driver can be used on Windows Seven and provides its own binaries if
Expand Down Expand Up @@ -71,4 +72,14 @@ protected function getCommandLineArguments(Notification $notification): array

return $arguments;
}

protected function launchProcess(Process $process): void
{
$process->start();
}

protected function handleExitCode(Process $process): bool
{
return true;
}
}
7 changes: 6 additions & 1 deletion src/Driver/SnoreToastDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,13 @@ protected function getCommandLineArguments(Notification $notification): array
return $arguments;
}

protected function launchProcess(Process $process): void
{
$process->start();
}

protected function handleExitCode(Process $process): bool
{
return 0 < $process->getExitCode();
return true;
}
}
2 changes: 1 addition & 1 deletion src/Exception/InvalidNotificationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct(
Notification $notification,
string $message = '',
int $code = 0,
?\Throwable $previous = null
?\Throwable $previous = null,
) {
$this->notification = $notification;

Expand Down
2 changes: 1 addition & 1 deletion src/Exception/NoSupportedNotifierException.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class NoSupportedNotifierException extends \RuntimeException implements Exceptio
public function __construct(
string $message = 'No supported notifier',
int $code = 0,
?\Throwable $previous = null
?\Throwable $previous = null,
) {
parent::__construct($message, $code, $previous);
}
Expand Down

0 comments on commit 1320ed1

Please sign in to comment.