Created
June 28, 2018 15:03
-
-
Save thepsion5/e62b62aff1083e6fa550c7ce7609258a to your computer and use it in GitHub Desktop.
Example of a Small PHP Application Built Without A Framework
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//FILENAME: /spaghetti-monster/src/Application.php | |
namespace SpaghettiMonster; | |
use SpaghettiMonster\Domain\EntityRepository; | |
use SpaghettiMonster\Domain\EntitySearch; | |
use SpaghettiMonster\Domain\EntityUpdater; | |
use SpaghettiMonster\Domain\Twitter\ApiClient; | |
use SpaghettiMonster\Domain\Twitter\TweetCreator; | |
use SpaghettiMonster\Http\ReCaptchaGuard; | |
use SpaghettiMonster\Infrastructure\EntityPdoRepository; | |
use SpaghettiMonster\Infrastructure\Twitter\DefaultApiClient; | |
use PDO; | |
class Application | |
{ | |
/** | |
* @var array | |
*/ | |
private $config; | |
public function __construct(array $config) | |
{ | |
$this->config = $config; | |
} | |
/** | |
* @return EntityRepository | |
*/ | |
protected function repository() | |
{ | |
$dbConfig = $this->config['database']; | |
$dsn = isset($dbConfig['dsn']) ? $dbConfig['dsn'] : "mysql:dbname={$dbConfig['db']};host={$dbConfig['host']}"; | |
$pdo = new PDO($dsn, $dbConfig['user'], $dbConfig['pass']); | |
return new EntityPdoRepository($pdo, $dbConfig['table']); | |
} | |
/** | |
* @return ApiClient | |
*/ | |
protected function twitterApi() | |
{ | |
return new DefaultApiClient($this->config['twitter']['consumer_key'], $this->config['twitter']['consumer_secret']); | |
} | |
/** | |
* @return EntitySearch | |
*/ | |
public function search() | |
{ | |
return new EntitySearch( $this->repository() ); | |
} | |
/** | |
* @return EntityUpdater | |
*/ | |
public function updater() | |
{ | |
return new EntityUpdater( $this->repository() ); | |
} | |
/** | |
* @return TweetCreator | |
*/ | |
public function tweetCreator() | |
{ | |
return new TweetCreator( | |
$this->twitterApi(), | |
$this->config['twitter']['access_token'], | |
$this->config['twitter']['access_token_secret'] | |
); | |
} | |
/** | |
* @return bool | |
*/ | |
public function hasReCaptcha() | |
{ | |
return $this->config['recaptcha']['enabled']; | |
} | |
/** | |
* @return ReCaptchaGuard|null | |
*/ | |
public function reCaptchaGuard() | |
{ | |
if (!$this->config['recaptcha']['enabled']) { | |
return null; | |
} | |
$reCaptcha = new \ReCaptcha\ReCaptcha($this->config['recaptcha']['secret_key']); | |
return new ReCaptchaGuard($reCaptcha, $this->config['recaptcha']['site_key']); | |
} | |
/** | |
* @return array | |
*/ | |
public function config() | |
{ | |
return $this->config; | |
} | |
/** | |
* @return string[] | |
*/ | |
public function namePrefixes() | |
{ | |
return $this->config['prefixes']; | |
} | |
/** | |
* Requires a template and extracts specified template data along with the | |
* application instance as $app | |
* | |
* @param string $templateName | |
* @param array $templateData | |
* @return string | |
*/ | |
public function template($templateName, array $templateData = []) | |
{ | |
$templateData['app'] = $this; | |
extract($templateData, EXTR_SKIP); | |
ob_start(); | |
require rtrim($this->config['template_dir'], '/') . "/$templateName"; | |
return ob_get_clean(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//FILENAME: /spaghetti-monster/setup/bootstrap.php | |
error_reporting(E_ALL | E_NOTICE); | |
require __DIR__ . '/../vendor/autoload.php'; | |
$config = require_once __DIR__ . '/settings.php'; | |
error_reporting(E_ALL | E_NOTICE); | |
ini_set('display_errors', $config['debug']); | |
session_start(); | |
$app = new \HonorVote\Application($config); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//FILENAME: /spaghetti-monster/public/index.php | |
require_once __DIR__ . '/../setup/bootstrap.php'; | |
use SpaghettiMonster\Http\Formatter; | |
use SpaghettiMonster\Domain\EntitySpec; | |
$search = $app->search(); | |
$page = isset($_GET['page']) ? $_GET['page'] : 1; | |
$searchValue = isset($_GET['search']) ? $_GET['search'] : ''; | |
$searchField = isset($_GET['field']) ? $_GET['field'] : ''; | |
$spec = new EntitySpec('', '', 'approved'); | |
if($searchValue) { | |
if ($searchField === 'FooLast') { | |
$spec->fooLastName($searchValue); | |
} else { | |
$spec->barLastName($searchValue); | |
} | |
} | |
$spec->currentPage($page); | |
$count = $search->totalForSpecification($spec); | |
$total = $search->totalForSpecification( EntitySpec::all() ); | |
$results = $search->searchBySpecification($spec); | |
?> | |
<?= $app->template('header.php') ?> | |
<!-- insert lots of HTML code here --> | |
<?= $app->template('footer.php') ?> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//FILENAME: /spaghetti-monster/setup/settings.php | |
return [ | |
'debug' => true, | |
'recaptcha' => [ | |
'enabled' => true, | |
'site_key' => '', | |
'secret_key' => '', | |
], | |
'template_dir' => __DIR__ . '/../includes', | |
'database' => [ | |
'host' => 'localhost', | |
'user' => '', | |
'pass' => '', | |
'db' => 'spaghetti_monster_local', | |
'options' => [ | |
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, | |
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC | |
], | |
'table' => 'honor_vote2016', | |
], | |
'twitter' => [ | |
'consumer_key' => '', | |
'consumer_secret' => '', | |
'access_token' => '', | |
'access_token_secret' => '' | |
], | |
'prefixes' => [ | |
'Ms', | |
'Miss', | |
'Mrs', | |
'Mr', | |
'Other' | |
] | |
]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment