by via Awwwards - Sites of the day
"Mr Branding" is a blog based on RSS for everything related to website branding and website design, it collects its posts from many sites in order to facilitate the updating to the latest technology.
To suggest any source, please contact me: Taha.baba@consultant.com
Thursday, September 14, 2017
Wake It Up
by via Awwwards - Sites of the day
Symfony Console Beyond the Basics – Helpers and Other Tools
It's undeniable how useful console commands can be when developing software. Not too long ago we re-introduced the Symfony Console component.
This component allows us to create structured and testable CLI commands. We created some simple commands and tested them; but when our commands become bigger and more complex, we need a different set of tools.
This is what we are going to look at today: advanced Symfony console tools.
Let's create a command that we can use to show some of these features. Most of the basic functionality was shown in the re-introduction to the Symfony console article, so be sure to check it before advancing - it's a quick but useful read!
Installation
composer require symfony/console
Essential information about Composer can be found here, and if you're not familiar with well designed isolated PHP environments in which to develop your PHP apps like Vagrant, we have a fantastic book explaining it all in depth available for purchase here.
Creating our command
Let's create a command for an all time favorite: Fizzbuzz.
Fizzbuzz is a simple problem often used in programming interviews to assert the programming competence of the interviewee. The definition of Fizzbuzz normally comes in the following form:
Write a program that prints the numbers from 1 to x. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five, print "FizzBuzz".
Our command will receive an argument which will be the top limit for Fizzbuzz.
First of all, let's create our Fizzbuzz class.
<?php
declare(strict_types=1);
namespace FizzBuzz;
class Fizzbuzz{
public function isFizz(int $value): bool{
if($value % 3 === 0){
return true;
}
return false;
}
public function isBuzz(int $value): bool{
if($value % 5 === 0){
return true;
}
return false;
}
public function calculateFizzBuzz(int $number): bool{
if($this->isFizz($number) && $this->isBuzz($number)){
echo "FizzBuzz \n";
return true;
}
if($this->isFizz($number)){
echo "Fizz \n";
return true;
}
if($this->isBuzz($number)){
echo "Buzz \n";
return true;
}
echo $number . "\n";
return true;
}
public function firstNFizzbuzz(int $maxValue): void{
$startValue = 1;
while($startValue <= $maxValue){
$this->calculateFizzBuzz($startValue);
$startValue++;
}
}
}
Pretty straightforward. The firstNFizzbuzz() method prints the results of Fizzbuzz for a $maxValue of numbers. It does this by calling the calculateFizzBuzz() method recursively.
Next, let's write our command. Create a FizzCommand.php file with the following contents:
Continue reading %Symfony Console Beyond the Basics – Helpers and Other Tools%
by Claudio Ribeiro via SitePoint
50 Social Media Video Marketing Stats for 2017 [Infographic]
[ This is a content summary only. Visit our website http://ift.tt/1b4YgHQ for full links, other content, and more! ]
by Web Desk via Digital Information World
Wednesday, September 13, 2017
10 Funny Facts About Your Digital Life
[ This is a content summary only. Visit our website http://ift.tt/1b4YgHQ for full links, other content, and more! ]
by Web Desk via Digital Information World
ScrollDir – Javascript Plugin to Leverage Scroll Direction with CSS
ScrollDir (Scroll Direction) is a zero dependency micro Javascript plugin to easily leverage vertical scroll direction in CSS via a data attribute.
by via jQuery-Plugins.net RSS Feed
Game Development with React and PHP: How Compatible Are They?
"I'd like to make a multiplayer, economy-based game. Something like Stardew Valley, but with none of the befriending aspects and a player-based economy."
I started thinking about this the moment I decided to try and build a game using PHP and React. The trouble is, I knew nothing about the dynamics of multiplayer games, or how to think about and implement player-based economies.
I wasn't even sure I knew enough about React to justify using it. I mean, the initial interface --- where I focus heavily on the server and economic aspects of the game --- is perfectly suited for React. But what about when I start to make the farming /interaction aspects? I love the idea of building an isometric interface around the economic system.
I once watched a talk by dead_lugosi, where she described building a medieval game in PHP. Margaret inspired me, and that talk was one of the things that led to me writing a book about JS game development. I became determined to write about my experience. Perhaps others could learn from my mistakes in this case, too.
The code for this part can be found at: http://ift.tt/2nnMjfD. I've tested it with PHP 7.1 and in a recent version of Google Chrome.
Setting Up the Back End
The first thing I searched for was guidance on building multiplayer economies. I found an excellent Stack Overflow thread in which folks explained various things to think about. I got about halfway through it before realizing I may have been starting from the wrong place.
"First things first: I need a PHP server. I'm going to have a bunch of React clients, so I want something capable of high-concurrency (perhaps even WebSockets). And it needs to be persistent: things must happen even when players aren't around."
I went to work setting up an async PHP server --- to handle high concurrency and support WebSockets. I added my recent work with PHP preprocessors to make things cleaner, and made the first couple of endpoints.
From config.pre:
$host = new Aerys\Host();
$host->expose("*", 8080);
$host->use($router = Aerys\router());
$host->use($root = Aerys\root(.."/public"));
$web = process .."/routes/web.pre";
$web($router);
$api = process .."/routes/api.pre";
$api($router);
I decided to use Aerys for the HTTP and WebSocket portions of the application. This code looked very different from the Aerys docs, but that's because I had a good idea about what I needed.
The usual process for running an Aerys app was to use a command like this:
vendor/bin/aerys -d -c config.php
That's a lot of code to keep repeating, and it didn't handle the fact that I wanted to use PHP preprocessing. I created a loader file.
From loader.php:
return Pre\processAndRequire(__DIR__ . "/config.pre");
I then installed my dependencies. This is from composer.json:
"require": {
"amphp/aerys": "dev-amp_v2",
"amphp/parallel": "dev-master",
"league/container": "^2.2",
"league/plates": "^3.3",
"pre/short-closures": "^0.4.0"
},
"require-dev": {
"phpunit/phpunit": "^6.0"
},
I wanted to use amphp/parallel, to move blocking code out of the async server, but it wouldn't install with a stable tag of amphp/aerys. That's why I went with the dev-amp_v2 branch.
I thought it would be a good idea to include some sort of template engine and service locator. I opted for PHP League versions of each. Finally I added pre/short-closures, both to handle the custom syntax in config.pre and the short closures I planned on using after…
Then I set about creating routes files. From routes/web.pre:
use Aerys\Router;
use App\Action\HomeAction;
return (Router $router) => {
$router->route(
"GET", "/", new HomeAction
);
};
And, from routes/api.pre:
use Aerys\Router;
use App\Action\Api\HomeAction;
return (Router $router) => {
$router->route(
"GET", "/api", new HomeAction
);
};
Though simple routes, these helped me to test the code in config.pre. I decided to make these routes files return closures, so I could pass them a typed $router, to which they could add their own routes. Finally, I created two (similar) actions.
From app/Actions/HomeAction.pre:
namespace App\Action;
use Aerys\Request;
use Aerys\Response;
class HomeAction
{
public function __invoke(Request $request,
Response $response)
{
$response->end("hello world");
}
}
One final touch was to add shortcut scripts, to launch dev and prod versions of the Aerys server.
From composer.json:
"scripts": {
"dev": "vendor/bin/aerys -d -c loader.php",
"prod": "vendor/bin/aerys -c loader.php"
},
"config": {
"process-timeout": 0
},
With all of this done, I could spin up a new server, and visit http://127.0.0.1:8080 just by typing:
composer dev
Continue reading %Game Development with React and PHP: How Compatible Are They?%
by Christopher Pitt via SitePoint
How to Build a Todo App Using React, Redux, and Immutable.js
The way React uses components and a one-way data flow makes it ideal for describing the structure of user interfaces. However, its tools for working with state are kept deliberately simple --- to help remind us that React is just the View in the traditional Model-View-Controller architecture.
There's nothing to stop us from building large applications with just React, but we would quickly discover that to keep our code simple, we'd need to manage our state elsewhere.
Whilst there's no official solution for dealing with application state, there are some libraries that align particularly well with React's paradigm. In this post, we'll pair React with two such libraries and use them to build a simple application.
Redux
Redux is a tiny library that acts as a container for our application state, by combining ideas from Flux and Elm. We can use Redux to manage any kind of application state, providing we stick to the following guidelines:
- our state is kept in a single store
- changes come from actions and not mutations
At the core of a Redux store is a function that takes the current application state and an action and combines them to create a new application state. We call this function a reducer.
Our React components will be responsible for sending actions to our store, and in turn our store will tell the components when they need to re-render.
ImmutableJS
Because Redux doesn't allow us to mutate the application state, it can be helpful to enforce this by modeling application state with immutable data structures.
ImmutableJS offers us a number of immutable data structures with mutative interfaces, and they're implemented in an efficient way, inspired by the implementations in Clojure and Scala.
Demo
We're going to use React with Redux and ImmutableJS to build a simple todo list that allows us to add todos and toggle them between complete and incomplete.
See the Pen React, Redux & Immutable Todo by SitePoint (@SitePoint) on CodePen.
The code is available in a repository on GitHub.
Setup
We'll get started by creating a project folder and initializing a package.json file with npm init. Then we'll install the dependencies we're going to need.
npm install --save react react-dom redux react-redux immutable
npm install --save-dev webpack babel-core babel-loader babel-preset-es2015 babel-preset-react
We'll be using JSX and ES2015, so we'll compile our code with Babel, and we're going to do this as part of the module bundling process with Webpack.
First, we'll create our Webpack configuration in webpack.config.js:
module.exports = {
entry: './src/app.js',
output: {
path: __dirname,
filename: 'bundle.js'
},
module: {
loaders: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader',
query: { presets: [ 'es2015', 'react' ] }
}
]
}
};
Finally, we'll extend our package.json by adding an npm script to compile our code with source maps:
"script": {
"build": "webpack --debug"
}
We'll need to run npm run build each time we want to compile our code.
React and Components
Before we implement any components, it can be helpful to create some dummy data. This helps us get a feel for what we're going to need our components to render:
const dummyTodos = [
{ id: 0, isDone: true, text: 'make components' },
{ id: 1, isDone: false, text: 'design actions' },
{ id: 2, isDone: false, text: 'implement reducer' },
{ id: 3, isDone: false, text: 'connect components' }
];
For this application, we're only going to need two React components, <Todo /> and <TodoList />.
// src/components.js
import React from 'react';
export function Todo(props) {
const { todo } = props;
if(todo.isDone) {
return <strike>{todo.text}</strike>;
} else {
return <span>{todo.text}</span>;
}
}
export function TodoList(props) {
const { todos } = props;
return (
<div className='todo'>
<input type='text' placeholder='Add todo' />
<ul className='todo__list'>
{todos.map(t => (
<li key={t.id} className='todo__item'>
<Todo todo={t} />
</li>
))}
</ul>
</div>
);
}
At this point, we can test these components by creating an index.html file in the project folder and populating it with the following markup. (You can find a simple stylesheet on GitHub):
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
<title>Immutable Todo</title>
</head>
<body>
<div id="app"></div>
<script src="bundle.js"></script>
</body>
</html>
We'll also need an application entry point at src/app.js.
// src/app.js
import React from 'react';
import { render } from 'react-dom';
import { TodoList } from './components';
const dummyTodos = [
{ id: 0, isDone: true, text: 'make components' },
{ id: 1, isDone: false, text: 'design actions' },
{ id: 2, isDone: false, text: 'implement reducer' },
{ id: 3, isDone: false, text: 'connect components' }
];
render(
<TodoList todos={dummyTodos} />,
document.getElementById('app')
);
Compile the code with npm run build, then navigate your browser to the index.html file and make sure that it's working.
Continue reading %How to Build a Todo App Using React, Redux, and Immutable.js%
by Dan Prince via SitePoint