Symfony Intro – composer.json – Bundles



Symfony Intro – composer.json – Bundles

0 1


symfony-intro

Presentation from DrupalCamp Stockholm 2013

On Github tobiassjosten / symfony-intro

Symfony Intro

Tobias Sjösten

@tobiassjosten

Smartburk.se

… built on Symfony!

Why Symfony?

Drupal 8

phpBB

Laravel

Zikula

PHPUnit

Silex

Lemon

Doctrine

Propel

phpDocumentor

FLOW3

Cilex

Midgard CMS

Behat

!Symfony2 instanceof Framework

getcomposer.org

composer.json

{
  "require": {
    "symfony/validator": "2.2.0-BETA2"
  },
}
$ composer install

Loading repositories with package information
Installing dependencies
- Installing symfony/translation (dev-master)
Cloning 10164d84c407fd9ac5918172f86a4b94bf2b9

- Installing symfony/validator (dev-master)
Cloning a307f75f9fa64489428b870b7aec7c6469710
Generating autoload files
require 'vendor/autoload.php';

$validator = Validation::createValidator();

$violations = $validator->validateValue(
    'Tobias Sjösten',
    new Length(array('min' => 10))
);

Symfony Standard Edition

Bundles

$ php app/console generate:bundle
$ ls -l SmartburkMainBundle/
Controller/
DependencyInjection/
Entity/
Form/
Listener/
Resources/
Tests/
$ ls -l SmartburkMainBundle/
Controller/
DependencyInjection/
Entity/
Form/
Listener/
Resources/
Tests/
class Newsletter {
  public function distribute() {
    $mailer = new Mailer('smtp.smartburk.se');
    $db = new Database('user', 'secret');
    foreach ($db->getUsers() as $user) {
        $mailer->sendTo($user, 'News!');
    }
  }
}
public function __construct($mailer, $db) {
  $this->mailer = $mailer;
  $this->db = $db;
}
public function distribute() {
  foreach ($this->db->getUsers() as $user) {
    $this->mailer->sendTo($user, 'News!');
  }
}

services.yml

newsletter:
  class: Newsletter
  arguments: mailer, db
mailer:
  class: Mailer
  arguments: $smtpServer
db:
  class: Database
  arguments: $user, $password
$dic->get('newsletter')->distribute();
$mailer = new Mailer($smtpServer);
$db = new Database($user, $password);
return new Newsletter($mailer, $db);
$ ls -l SmartburkMainBundle/
Controller/
DependencyInjection/
Entity/
Form/
Listener/
Resources/
Tests/
$('.some-link').click(function () {
  // …
});
function mymodule_user_login() {
  // …
}

services.yml

newsletter:
  class: Newsletter
  arguments: mailer, db
tags: - name: kernel.event_listener event: smartburk.new_season method: distribute
$ ls -l SmartburkMainBundle/
Controller/
DependencyInjection/
Entity/
Form/
Listener/
Resources/
Tests/

Drupal 7

function example_task_entity_info() {
  $return = array(
    'example_task' => array(
      'label' => t('Task'),
      'entity class' => 'ExampleTask',
      'controller class' => 'ExampleTaskController',
      'base table' => 'example_task',
      'fieldable' => TRUE,
      'entity keys' => array(
        'id' => 'tkid',
        'bundle' => 'type',
      ),
      'bundle keys' => array(
        'bundle' => 'type',
      ),
      'bundles' => array(),
      'load hook' => 'example_task_load',
      'view modes' => array(
        'full' => array(
          'label' => t('Default'),
          'custom settings' => FALSE,
        ),
      ),
      'label callback' => 'entity_class_label',
      'uri callback' => 'entity_class_uri',
      'module' => 'example_task',
      'access callback' => 'example_task_access',
    ),
  );
  $return['example_task_type'] = array(
    'label' => t('Task Type'),
    'entity class' => 'ExampleTaskType',
    'controller class' => 'ExampleTaskTypeController',
    'base table' => 'example_task_type',
    'fieldable' => FALSE,
    'bundle of' => 'example_task',
    'exportable' => TRUE,
    'entity keys' => array(
      'id' => 'id',
      'name' => 'type',
      'label' => 'label',
    ),
    'module' => 'example_task',
    // Enable the entity API's admin UI.
    'admin ui' => array(
      'path' => 'admin/structure/task-types',
      'file' => 'example_task.admin.inc',
      'controller class' => 'ExampleTaskTypeUIController',
    ),
    'access callback' => 'example_task_type_access',
  );
  return $return;
}

Symfony Standard Edition

/**
 * @ORM\Table("series")
 */
class Series {
  /**
   * @ORM\Column(type="string", length=128)
   */
  private $title;
}
$ php app/console doctrine:schema:update
class SeriesRepository extends Repository {
  public function findCoolSeries() {
    // …
  }

  public function findTopSeries() {
    // …
  }
}
$ ls -l SmartburkMainBundle/
Controller/
DependencyInjection/
Entity/
Form/
Listener/
Resources/
Tests/

Drupal 7

function mymodule_menu() {
  $items['abc/%1'] = array(
    'page callback' => 'mymodule_abc_view',
    'page arguments' => array(1),
  );
  return $items;
}
function mymodule_abc_view($word) {
  // …
}

Symfony Standard Edition

/**
 * @Route("/abc/{word}")
 */
public function abcAction($word = 'def') {
  // …
}

Symfony Standard Edition

/**
 * @Route(
 *   "/abc/{word1}/{word2}",
 *   requirements={"word1" = "\d+"}
 * )
 * @Method("POST")
 */
public function abcAction($word2, $word1 = 'def') {
  // …
}
$ ls -l SmartburkMainBundle/
Controller/
DependencyInjection/
Entity/
Form/
Listener/
Resources/
├── public
├── translations
└── views
Tests/
{% for episode in episodes %}
    * S01E{{ episode.number }}
{% else %}
    No episodes in season 1
{% endfor %}
<script type="javascript">
  Twig.render(episodeList, {episodes: s01eps});
</script>
$ ls -l SmartburkMainBundle/
Controller/
DependencyInjection/
Entity/
Form/
Listener/
Resources/
├── public
├── translations
└── views
Tests/

CSS

{% stylesheets
  'css/normalize.css'
  'css/*'
  filter='cssrewrite cssembed cssmin'
%}
    <link rel="stylesheet" href="{{ asset_url }}">
{% endstylesheets %}

JavaScript

{% javascripts
  'js/jquery.min.js'
  'js/*'
  filter='yui_js coffeescript'
%}
    <script src="{{ asset_url }}"></script>
{% endjavascripts %}

Images

{% image
  'img/logo.png'
  filter='optipng'
  output='logo-optimized.png'
%}
    <img src="{{ asset_url }}" alt="Our logo">
{% endimage %}

CoffeeScriptFilter

CompassFilter

CssEmbedFilter

CssImportFilter

CssMinFilter

CssRewriteFilter

DartFilter

CompilerApiFilter

CompilerJarFilter

GssFilter

HandlebarsFilter

JpegoptimFilter

JpegtranFilter

JSMinFilter

JSMinPlusFilter

LessFilter

LessphpFilter

OptiPngFilter

PackagerFilter

PackagerFilter

PhpCssEmbedFilter

PngoutFilter

SassFilter

ScssFilter

ScssphpFilter

SprocketsFilter

StylusFilter

TypeScriptFilter

UglifyCssFilter

UglifyJs2Filter

UglifyJsFilter

CssCompressorFilter

JsCompressorFilter

$ ls -l SmartburkMainBundle/
Controller/
DependencyInjection/
Entity/
Form/
Listener/
Resources/
├── public
├── translations
└── views
Tests/
<p>{{ 'series.follow.action'|trans }}<p>
series:
  follow:
    action: Följ serien
<p>Följ serien<p>
$ ls -l FOS/UserBundle
Command/
Controller/
Document/
DependencyInjection/
Doctrine/
Document/
Entity/
Event/
EventListener/
Form/
Mailer/
Model/
Propel/
Resources/
Security/
Tests/
Util/
Validator/

Browser Kit

ClassLoader

Config

Console

CSS Selector

Dependency Injection

DOM Crawler

Event Dispatcher

Filesystem

Finder

Form

HTTP Foundation

HTTP Kernel

Locale

Options Resolver

Process

Property Access

Routing

Security

Serializer

Stopwatch

Templating

Translation

Validator

Yaml

Thank you!

@tobiassjosten

smartburk.se