Skip to content
This repository has been archived by the owner on Sep 12, 2021. It is now read-only.

Commit

Permalink
Initial commit 🌮
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukas Alexander Kienzl committed Apr 6, 2018
0 parents commit 9d96819
Show file tree
Hide file tree
Showing 11 changed files with 227 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
; This file is for unifying the coding style for different editors and IDEs.
; More information at http://editorconfig.org

root = true

[*]
charset = utf-8
indent_size = 4
indent_style = space
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false
8 changes: 8 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Path-based git attributes
# https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html

# Ignore all test and documentation with "export-ignore".
/.gitattributes export-ignore
/.gitignore export-ignore
/phpunit.xml.dist export-ignore
/tests export-ignore
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
build
composer.lock
docs
vendor
1 change: 1 addition & 0 deletions .styleci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
preset: laravel
12 changes: 12 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
language: php

php:
- 7.1
- 7.2

before_script:
- travis_retry composer self-update
- travis_retry composer update --no-interaction --prefer-dist

script:
- composer test
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# A Chrome Headless wrapper for Laravel
[![Build Status](https://img.shields.io/travis/helloiamlukas/laravel-chrome/master.svg?style=flat-square)](https://travis-ci.org/helloiamlukas/chrome-php)
[![StyleCI](https://styleci.io/repos/128383656/shield?branch=master)](https://styleci.io/repos/19386515)

Get the DOM of any webpage by using headless Chrome.

# Requirements
This package requires a stable version of Google Chrome (v63 or higher).

If you want to install it on Ubuntu 16.04 you can do it like this:
```
sudo apt-get update
sudo apt-get install -y libappindicator1 fonts-liberation
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
sudo dpkg -i google-chrome*.deb
```
45 changes: 45 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"name": "helloiamlukas/laravel-chrome",
"description": "A Laravel Wrapper for Chrome Headless. Get the DOM of any webpage.",
"homepage": "https://github.com/helloiamlukas/laravel-chrome",
"keywords":
[
"laravel",
"webpage",
"dom",
"chrome",
"headless"
],
"license": "MIT",
"require": {
"php": "^7.1",
"helloiamlukas/chrome-php": "^1.0"
},
"require-dev": {
"orchestra/testbench": "^3.6",
"phpunit/phpunit": "^6.1|^7.0"
},
"autoload": {
"psr-4": {
"ChromeHeadless\\Laravel\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"ChromeHeadless\\Laravel\\Test\\": "tests"
}
},
"scripts": {
"test": "vendor/bin/phpunit"
},
"config": {
"sort-packages": true
},
"extra": {
"laravel": {
"providers": [
"ChromeHeadless\\Laravel\\ChromeHeadlessServiceProvider"
]
}
}
}
32 changes: 32 additions & 0 deletions config/chrome.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

return [
/*
|--------------------------------------------------------------------------
| Chrome Path
|--------------------------------------------------------------------------
|
| Manually set the path where Google Chrome is installed.
|
*/
'exec_path' => 'google-chrome',
/*
|--------------------------------------------------------------------------
| User Agent
|--------------------------------------------------------------------------
|
| Change the user agent that will be used by Google Chrome.
|
*/
'user_agent' => null,
/*
|--------------------------------------------------------------------------
| Timeout
|--------------------------------------------------------------------------
|
| Specify a timeout in seconds.
| (null = no timeout)
|
*/
'timeout' => null,
];
22 changes: 22 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="ChromeHeadless Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
</phpunit>
33 changes: 33 additions & 0 deletions src/ChromeHeadlessServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace ChromeHeadless\Laravel;

use ChromeHeadless\ChromeHeadless;
use Illuminate\Support\ServiceProvider;

class ChromeHeadlessServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*/
public function boot()
{
$this->publishes([
__DIR__.'/../config/chrome.php' => config_path('chrome.php'),
], 'config');
}

/**
* Register the application services.
*/
public function register()
{
$this->mergeConfigFrom(__DIR__.'/../config/chrome.php', 'chrome');

$this->app->resolving(ChromeHeadless::class, function (ChromeHeadless $chrome) {
return $chrome->setChromePath($this->app->config->get('chrome.exec_path'))
->setUserAgent($this->app->config->get('chrome.user_agent'))
->setTimeout($this->app->config->get('chrome.timeout'));
});
}
}
39 changes: 39 additions & 0 deletions tests/ChromeHeadlessServiceProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace ChromeHeadless\Test;

use ChromeHeadless\ChromeHeadless;
use ChromeHeadless\Laravel\ChromeHeadlessServiceProvider;
use Orchestra\Testbench\TestCase;

class ChromeHeadlessServiceProviderTest extends TestCase
{
protected function getPackageProviders($app)
{
return [ChromeHeadlessServiceProvider::class];
}

/** @test */
public function it_can_set_a_custom_user_agent()
{
$user_agent = 'NiceUserAgent/1.0';

$this->app->config->set('chrome.user_agent', $user_agent);

$command = ChromeHeadless::url('https://example.com')->setUserAgent($user_agent)->createCommand();

$this->assertContains('--user-agent="'.$user_agent.'"', $command);
}

/** @test */
public function it_can_set_a_custom_chrome_path()
{
$chrome_path = 'google-chrome-stable';

$this->app->config->set('chrome.exec_path', $chrome_path);

$command = ChromeHeadless::url('https://example.com')->setUserAgent($chrome_path)->createCommand();

$this->assertContains('--user-agent="'.$chrome_path.'"', $command);
}
}

0 comments on commit 9d96819

Please sign in to comment.